Skip to content

Commit

Permalink
Refactor file-launching method in cmd
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
uranusjr committed Nov 5, 2016
1 parent 3aacb43 commit eb79def
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
10 changes: 7 additions & 3 deletions MacDown/Code/Application/MPMainController.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ - (instancetype)init
name:MPDidDetectFreshInstallationNotification
object:self.prefereces];
[self copyFiles];
[self openPendingFiles];
return self;
}

Expand All @@ -159,6 +158,11 @@ - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
return !self.prefereces.supressesUntitledDocumentOnLaunch;
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
[self openPendingFiles];
}


#pragma mark - SUUpdaterDelegate

Expand Down Expand Up @@ -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)
Expand All @@ -231,7 +235,7 @@ - (void)openPendingFiles
}
}

[defaults setObject:@[] forKey:@"filesToOpenOnNextLaunch"
[defaults setObject:@[] forKey:kMPFilesToOpenKey
inSuiteNamed:kMPApplicationSuiteName];
[defaults synchronize];
treat();
Expand Down
2 changes: 2 additions & 0 deletions MacDown/Code/Utility/MPGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ static NSString * const kMPCommandName = @"macdown";

static NSString * const kMPHelpKey = @"help";
static NSString * const kMPVersionKey = @"version";

static NSString * const kMPFilesToOpenKey = @"filesToOpenOnNextLaunch";
23 changes: 3 additions & 20 deletions macdown-cmd/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,15 @@
return runningInstances.firstObject;
}


void MPCollectForRunningMacDown(NSOrderedSet<NSURL *> *urls)
{
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
for (NSURL *url in urls)
[workspace openFile:url.path withApplication:kMPApplicationName];
}


void MPCollectForUnlaunchedMacDown(NSOrderedSet<NSURL *> *urls)
void MPCollectForMacDown(NSOrderedSet<NSURL *> *urls)
{
NSUserDefaults *defaults =
[[NSUserDefaults alloc] initWithSuiteNamed:kMPApplicationSuiteName];
NSMutableArray<NSString *> *urlStrings =
[[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];
}
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit eb79def

Please sign in to comment.