Skip to content

Add App.open() method #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Phoenix/PHApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static NSString * const PHAppFocusOptionKey = @"focus";

+ (instancetype) get:(NSString *)appName;
JSExportAs(launch, + (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary<NSString *, id> *)optionals);
JSExportAs(open, + (instancetype) open:(NSString *)filePath withOptionals:(NSDictionary<NSString *, id> *)optionals);
+ (instancetype) focused;
+ (NSArray<PHApp *> *) all;

Expand Down
25 changes: 12 additions & 13 deletions Phoenix/PHApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@ + (instancetype) get:(NSString *)appName {
return nil;
}

+ (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary<NSString *, id> *)optionals {

+ (instancetype) open:(NSString *)filePath withOptionals:(NSDictionary<NSString *, id> *)optionals {
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];
NSWorkspaceLaunchOptions launchOptions = NSWorkspaceLaunchWithoutActivation;

if (!appPath) {
NSLog(@"Error: Could not find an app with the name “%@”.", appName);
return nil;
}

NSNumber *focusOption = optionals[PHAppFocusOptionKey];

// Focus on launch
Expand All @@ -61,18 +53,25 @@ + (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary<NSString
}

NSError *error;
NSRunningApplication *app = [sharedWorkspace launchApplicationAtURL:[NSURL fileURLWithPath:appPath]
NSRunningApplication *app = [sharedWorkspace openURL:[NSURL fileURLWithPath:filePath]
options:launchOptions
configuration:@{}
error:&error];
configuration:@{}
error:&error];
if (error) {
NSLog(@"Error: Could not launch app “%@”. (%@)", appName, error);
NSLog(@"Error: Could not open file “%@”. (%@)", filePath, error);
return nil;
}

return [[self alloc] initWithApp:app];
}

+ (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary<NSString *, id> *)optionals {
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];

return [self open:appPath withOptionals:optionals];
}

+ (instancetype) focused {

return [[self alloc] initWithApp:[NSWorkspace sharedWorkspace].frontmostApplication];
Expand Down