From c55500baa61e6d39e2d06b1f555329c15d95e097 Mon Sep 17 00:00:00 2001 From: Nikolay Bachiyski Date: Fri, 10 Jun 2022 15:25:34 +0300 Subject: [PATCH] Add App.open() method Opens a file with the default associated app, instead of launching an app. Useful to open files/directories via keyboard shortcuts. Since opening an .app file by default launches it, the `App.launch()` method uses this one internally. --- Phoenix/PHApp.h | 1 + Phoenix/PHApp.m | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Phoenix/PHApp.h b/Phoenix/PHApp.h index fd6c33fc..364554af 100644 --- a/Phoenix/PHApp.h +++ b/Phoenix/PHApp.h @@ -19,6 +19,7 @@ static NSString * const PHAppFocusOptionKey = @"focus"; + (instancetype) get:(NSString *)appName; JSExportAs(launch, + (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary *)optionals); +JSExportAs(open, + (instancetype) open:(NSString *)filePath withOptionals:(NSDictionary *)optionals); + (instancetype) focused; + (NSArray *) all; diff --git a/Phoenix/PHApp.m b/Phoenix/PHApp.m index ac385152..a16099ad 100644 --- a/Phoenix/PHApp.m +++ b/Phoenix/PHApp.m @@ -42,17 +42,9 @@ + (instancetype) get:(NSString *)appName { return nil; } -+ (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary *)optionals { - ++ (instancetype) open:(NSString *)filePath withOptionals:(NSDictionary *)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 @@ -61,18 +53,25 @@ + (instancetype) launch:(NSString *)appName withOptionals:(NSDictionary *)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];