Skip to content

Commit

Permalink
Added functions to get application localization information
Browse files Browse the repository at this point in the history
- Added `hs.application.preferredLocalizationsForBundleID()`
- Added `hs.application.preferredLocalizationsForBundlePath()`
- Added `hs.application.localizationsForBundleID()`
- Added `hs.application.localizationsForBundlePath()`
  • Loading branch information
latenitefilms authored and cmsj committed Apr 23, 2022
1 parent 68a514e commit b432a3e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Hammerspoon Tests/HSapplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ - (void)testMenusAsync {
- (void)testUTI {
RUN_LUA_TEST()
}

- (void)testLocalizationFunctions {
RUN_LUA_TEST()
}

@end
4 changes: 4 additions & 0 deletions Hammerspoon/HSuicore.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
+(NSString *)pathForBundleID:(NSString *)bundleID;
+(NSDictionary *)infoForBundleID:(NSString *)bundleID;
+(NSDictionary *)infoForBundlePath:(NSString *)bundlePath;
+(NSArray *)preferredLocalizationsForBundleID:(NSString *)bundleID;
+(NSArray *)preferredLocalizationsForBundlePath:(NSString *)bundlePath;
+(NSArray *)localizationsForBundlePath:(NSString *)bundlePath;
+(NSArray *)localizationsForBundleID:(NSString *)bundleID;

// Class methods that return arrays of applications
+(NSArray<HSapplication *>*)runningApplicationsWithState:(lua_State *)L;
Expand Down
32 changes: 32 additions & 0 deletions Hammerspoon/HSuicore.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ +(NSDictionary *)infoForBundlePath:(NSString *)bundlePath {
return appInfo;
}

+(NSArray *)preferredLocalizationsForBundleID:(NSString *)bundleID {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *appPath = [ws absolutePathForAppBundleWithIdentifier:bundleID];
return [HSapplication preferredLocalizationsForBundlePath:appPath];
}

+(NSArray *)preferredLocalizationsForBundlePath:(NSString *)bundlePath {
NSArray *preferredLocalizations = nil;

NSBundle *app = [NSBundle bundleWithPath:bundlePath];
if (app) {
preferredLocalizations = app.preferredLocalizations;
}
return preferredLocalizations;
}

+(NSArray *)localizationsForBundleID:(NSString *)bundleID {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *appPath = [ws absolutePathForAppBundleWithIdentifier:bundleID];
return [HSapplication localizationsForBundlePath:appPath];
}

+(NSArray *)localizationsForBundlePath:(NSString *)bundlePath {
NSArray *localizations = nil;

NSBundle *app = [NSBundle bundleWithPath:bundlePath];
if (app) {
localizations = app.localizations;
}
return localizations;
}

+(NSArray<HSapplication *>*)runningApplicationsWithState:(lua_State *)L {
NSMutableArray<HSapplication *> *apps = [[NSMutableArray alloc] init];

Expand Down
68 changes: 68 additions & 0 deletions extensions/application/libapplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,70 @@ static int application_infoForBundleID(lua_State* L) {
return 1;
}

/// hs.application.preferredLocalizationsForBundleID(bundleID) -> table or nil
/// Function
/// Gets an ordered list of preferred localizations contained in a bundle
///
/// Parameters:
/// * bundleID - A string containing an application bundle identifier (e.g. "com.apple.Safari")
///
/// Returns:
/// * A table containing language IDs for localizations in the bundle. The strings are ordered according to the user's language preferences and available localizations.
static int application_preferredLocalizationsForBundleID(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TSTRING, LS_TBREAK];
[skin pushNSObject:[HSapplication preferredLocalizationsForBundleID:[skin toNSObjectAtIndex:1]]];
return 1;
}

/// hs.application.preferredLocalizationsForBundlePath(bundlePath) -> table or nil
/// Function
/// Gets an ordered list of preferred localizations contained in a bundle
///
/// Parameters:
/// * bundlePath - A string containing the path to an application bundle (e.g. "/Applications/Safari.app")
///
/// Returns:
/// * A table containing language IDs for localizations in the bundle. The strings are ordered according to the user's language preferences and available localizations.
static int application_preferredLocalizationsForBundlePath(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TSTRING, LS_TBREAK];
[skin pushNSObject:[HSapplication preferredLocalizationsForBundlePath:[skin toNSObjectAtIndex:1]]];
return 1;
}

/// hs.application.localizationsForBundleID(bundleID) -> table or nil
/// Function
/// Gets a list of all the localizations contained in the bundle.
///
/// Parameters:
/// * bundleID - A string containing an application bundle identifier (e.g. "com.apple.Safari")
///
/// Returns:
/// * A table containing containing language IDs for all the localizations contained in the bundle.
static int application_localizationsForBundleID(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TSTRING, LS_TBREAK];
[skin pushNSObject:[HSapplication localizationsForBundleID:[skin toNSObjectAtIndex:1]]];
return 1;
}

/// hs.application.localizationsForBundlePath(bundlePath) -> table or nil
/// Function
/// Gets a list of all the localizations contained in the bundle.
///
/// Parameters:
/// * bundlePath - A string containing the path to an application bundle (e.g. "/Applications/Safari.app")
///
/// Returns:
/// * A table containing containing language IDs for all the localizations contained in the bundle.
static int application_localizationsForBundlePath(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TSTRING, LS_TBREAK];
[skin pushNSObject:[HSapplication localizationsForBundlePath:[skin toNSObjectAtIndex:1]]];
return 1;
}

/// hs.application.infoForBundlePath(bundlePath) -> table or nil
/// Function
/// Gets the metadata of an application from its path on disk
Expand Down Expand Up @@ -1196,6 +1260,10 @@ static int userdata_gc(lua_State *L) {
{"pathForBundleID", application_pathForBundleID},
{"infoForBundleID", application_infoForBundleID},
{"infoForBundlePath", application_infoForBundlePath},
{"preferredLocalizationsForBundleID", application_preferredLocalizationsForBundleID},
{"preferredLocalizationsForBundlePath", application_preferredLocalizationsForBundlePath},
{"localizationsForBundleID", application_localizationsForBundleID},
{"localizationsForBundlePath", application_localizationsForBundlePath},
{"defaultAppForUTI", application_bundleForUTI},
{"launchOrFocus", application_launchorfocus},
{"launchOrFocusByBundleID", application_launchorfocusbybundleID},
Expand Down
19 changes: 19 additions & 0 deletions extensions/application/test_application.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,22 @@ function testUTI()
assertIsEqual("com.apple.Preview", bundle)
return success()
end

function testLocalizationFunctions()
local appPath = "/Applications/Safari.app"
local bundleID = "com.apple.Safari"

local localizationsForBundleID = hs.application.localizationsForBundleID(bundleID)
assertIsTable(localizationsForBundleID)

local preferredLocalizationsForBundleID = hs.application.preferredLocalizationsForBundleID(bundleID)
assertIsTable(preferredLocalizationsForBundleID)

local localizationsForBundlePath = hs.application.localizationsForBundlePath(appPath)
assertIsTable(localizationsForBundlePath)

local preferredLocalizationsForBundlePath = hs.application.preferredLocalizationsForBundlePath(appPath)
assertIsTable(preferredLocalizationsForBundlePath)

return success()
end

0 comments on commit b432a3e

Please sign in to comment.