diff --git a/Phoenix/PHApp.h b/Phoenix/PHApp.h index b80eacee..f01213a1 100644 --- a/Phoenix/PHApp.h +++ b/Phoenix/PHApp.h @@ -16,7 +16,7 @@ #pragma mark - Apps + (instancetype) get:(NSString *)appName; -+ (instancetype) launch:(NSString *)appName; ++ (instancetype) launch:(NSString *)appName :(NSDictionary *)optionals; + (instancetype) focused; + (NSArray *) all; diff --git a/Phoenix/PHApp.m b/Phoenix/PHApp.m index a979b401..6db88271 100644 --- a/Phoenix/PHApp.m +++ b/Phoenix/PHApp.m @@ -15,6 +15,7 @@ @interface PHApp () @implementation PHApp static NSString * const PHAppForceOptionKey = @"force"; +static NSString * const PHAppLaunchFocusOptionKey = @"focus"; #pragma mark - Initialising @@ -41,7 +42,7 @@ + (instancetype) get:(NSString *)appName { return nil; } -+ (instancetype) launch:(NSString *)appName { ++ (instancetype) launch:(NSString *)appName :(NSDictionary *)optionals { NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace]; NSString *appPath = [sharedWorkspace fullPathForApplication:appName]; @@ -51,9 +52,16 @@ + (instancetype) launch:(NSString *)appName { return nil; } + NSNumber *focusOption = optionals[PHAppLaunchFocusOptionKey]; + + NSWorkspaceLaunchOptions launchOption = NSWorkspaceLaunchWithoutActivation; + if (focusOption && focusOption.boolValue) { + launchOption = NSWorkspaceLaunchDefault; + } + NSError *error; NSRunningApplication *app = [sharedWorkspace launchApplicationAtURL:[NSURL fileURLWithPath:appPath] - options:NSWorkspaceLaunchWithoutActivation + options:launchOption configuration:@{} error:&error]; if (error) { diff --git a/docs/API.md b/docs/API.md index e8407713..494f2231 100644 --- a/docs/API.md +++ b/docs/API.md @@ -564,7 +564,7 @@ Use the `App`-object to control apps. Beware that an app can get stale if you ke class App implements Identifiable static App get(String appName) - static App launch(String appName) + static App launch(String appName, Map optionals) static App focused() static Array all() @@ -587,7 +587,7 @@ end ``` - `get(String appName)` returns the running app with the given name, returns `undefined` if the app is not currently running -- `launch(String appName)` launches to the background and returns the app with the given name, returns `undefined` if unsuccessful +- `launch(String appName, Map optionals)` launches and returns the app with the given name, returns `undefined` if unsuccessful - `focused()` returns the focused app - `all()` returns all running apps - `processIdentifier()` returns the process identifier (PID) for the app, returns `-1` if the app does not have a PID @@ -613,6 +613,10 @@ end - `force` (boolean): if set `true` force terminates the app +### Launch Optionals + +- `focus` (boolean): if set `true`, the launched app will be automatically focused. You don't need to call app.focus() to bring it forward. + ## 21. Window Use the `Window`-object to control windows. Every screen (i.e. display) combines to form a large rectangle. Every window lives within this rectangle and their position can be altered by giving coordinates inside this rectangle. To position a window to a specific display, you need to calculate its position within the large rectangle. To figure out the coordinates for a given screen, use the functions in `Screen`. Beware that a window can get stale if you keep a reference to it and it is for instance closed while you do so.