Skip to content

Commit

Permalink
Add hs.application.defaultAppForUTI()
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsj committed Apr 30, 2019
1 parent 96a2f85 commit aaeb3ed
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions extensions/application/internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ static int application_infoForBundlePath(lua_State* L) {
return 1;
}

/// hs.application.defaultAppForUTI(uti) -> string or nil
/// Function
/// Returns the bundle ID of the default application for a given UTI
///
/// Parameters:
/// * uti - A string containing a UTI
///
/// Returns:
/// * A string containing a bundle ID, or nil if none could be found
static int application_bundleForUTI(lua_State *L) {
LuaSkin *skin = [LuaSkin shared];
[skin checkArgs:LS_TSTRING, LS_TBREAK];

NSString *uti = [skin toNSObjectAtIndex:1];

CFStringRef cfhandler;
if (( cfhandler = LSCopyDefaultRoleHandlerForContentType(
(__bridge CFStringRef)uti, kLSRolesAll )) == NULL ) {
if (( cfhandler = LSCopyDefaultHandlerForURLScheme(
(__bridge CFStringRef)uti )) == NULL ) {
lua_pushnil(L);
return 1;
}
}

[skin pushNSObject:(__bridge NSString *)cfhandler];
if (cfhandler) CFRelease(cfhandler);

return 1;
}

/// hs.application:allWindows() -> list of hs.window objects
/// Method
/// Returns all open windows owned by the given app.
Expand Down Expand Up @@ -1162,6 +1193,7 @@ static int userdata_tostring(lua_State* L) {
{"pathForBundleID", application_pathForBundleID},
{"infoForBundleID", application_infoForBundleID},
{"infoForBundlePath", application_infoForBundlePath},
{"defaultAppForUTI", application_bundleForUTI},

{"allWindows", application_allWindows},
{"mainWindow", application_mainWindow},
Expand Down

0 comments on commit aaeb3ed

Please sign in to comment.