From eb79defa9a4604dc6eebf533172db9f21cadb693 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sun, 6 Nov 2016 04:50:16 +0800 Subject: [PATCH] Refactor file-launching method in cmd Previously the cmd util uses different strategies to open files based on whether MacDown is currently running. This results in various problems, the most prominent being that NSWorkspace cannot be used to pass a path for files that do not exist. This change unifies the opening strategy to always be based on cross-application NSUserDefaults instead, and moves the open file check from during application launch to application activation. This makes the implementation simpler, and also solves the problem aforementioned. Fix #672. --- MacDown/Code/Application/MPMainController.m | 10 ++++++--- MacDown/Code/Utility/MPGlobals.h | 2 ++ macdown-cmd/main.m | 23 +++------------------ 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/MacDown/Code/Application/MPMainController.m b/MacDown/Code/Application/MPMainController.m index da9bb887..0d67201b 100644 --- a/MacDown/Code/Application/MPMainController.m +++ b/MacDown/Code/Application/MPMainController.m @@ -147,7 +147,6 @@ - (instancetype)init name:MPDidDetectFreshInstallationNotification object:self.prefereces]; [self copyFiles]; - [self openPendingFiles]; return self; } @@ -159,6 +158,11 @@ - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender return !self.prefereces.supressesUntitledDocumentOnLaunch; } +- (void)applicationDidBecomeActive:(NSNotification *)notification +{ + [self openPendingFiles]; +} + #pragma mark - SUUpdaterDelegate @@ -214,7 +218,7 @@ - (void)openPendingFiles NSDocumentController *c = [NSDocumentController sharedDocumentController]; NSUserDefaults *defaults = [[NSUserDefaults alloc] init]; - NSArray *paths = [defaults objectForKey:@"filesToOpenOnNextLaunch" + NSArray *paths = [defaults objectForKey:kMPFilesToOpenKey inSuiteNamed:kMPApplicationSuiteName]; for (NSString *path in paths) @@ -231,7 +235,7 @@ - (void)openPendingFiles } } - [defaults setObject:@[] forKey:@"filesToOpenOnNextLaunch" + [defaults setObject:@[] forKey:kMPFilesToOpenKey inSuiteNamed:kMPApplicationSuiteName]; [defaults synchronize]; treat(); diff --git a/MacDown/Code/Utility/MPGlobals.h b/MacDown/Code/Utility/MPGlobals.h index 2457958e..9fb7e157 100644 --- a/MacDown/Code/Utility/MPGlobals.h +++ b/MacDown/Code/Utility/MPGlobals.h @@ -16,3 +16,5 @@ static NSString * const kMPCommandName = @"macdown"; static NSString * const kMPHelpKey = @"help"; static NSString * const kMPVersionKey = @"version"; + +static NSString * const kMPFilesToOpenKey = @"filesToOpenOnNextLaunch"; diff --git a/macdown-cmd/main.m b/macdown-cmd/main.m index abcf14e2..1140753f 100644 --- a/macdown-cmd/main.m +++ b/macdown-cmd/main.m @@ -23,16 +23,7 @@ return runningInstances.firstObject; } - -void MPCollectForRunningMacDown(NSOrderedSet *urls) -{ - NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; - for (NSURL *url in urls) - [workspace openFile:url.path withApplication:kMPApplicationName]; -} - - -void MPCollectForUnlaunchedMacDown(NSOrderedSet *urls) +void MPCollectForMacDown(NSOrderedSet *urls) { NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteNamed:kMPApplicationSuiteName]; @@ -40,7 +31,7 @@ void MPCollectForUnlaunchedMacDown(NSOrderedSet *urls) [[NSMutableArray alloc] initWithCapacity:urls.count]; for (NSURL *url in urls) [urlStrings addObject:url.path]; - [defaults setObject:urlStrings forKey:@"filesToOpenOnNextLaunch" + [defaults setObject:urlStrings forKey:kMPFilesToOpenKey inSuiteNamed:kMPApplicationSuiteName]; [defaults synchronize]; } @@ -70,15 +61,7 @@ int main(int argc, const char * argv[]) NSURL *url = [NSURL URLWithString:escaped relativeToURL:pwdUrl]; [urls addObject:url]; } - - // If the application is running, open all files with the first running - // instance. Otherwise save the file URLs, and start the app (saved URLs - // will be opened when the app launches). - NSRunningApplication *instance = MPRunningMacDownInstance(); - if (instance) - MPCollectForRunningMacDown(urls); - else - MPCollectForUnlaunchedMacDown(urls); + MPCollectForMacDown(urls); // Launch MacDown. [[NSWorkspace sharedWorkspace] launchApplication:kMPApplicationName];