PFMoveApplication.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //
  2. // PFMoveApplication.m, version 1.22
  3. // LetsMove
  4. //
  5. // Created by Andy Kim at Potion Factory LLC on 9/17/09
  6. //
  7. // The contents of this file are dedicated to the public domain.
  8. #import "PFMoveApplication.h"
  9. #import <AppKit/AppKit.h>
  10. #import <Foundation/Foundation.h>
  11. #import <Security/Security.h>
  12. #import <dlfcn.h>
  13. #import <sys/param.h>
  14. #import <sys/mount.h>
  15. // Strings
  16. // These are macros to be able to use custom i18n tools
  17. #define _I10NS(nsstr) NSLocalizedStringFromTable(nsstr, @"MoveApplication", nil)
  18. #define kStrMoveApplicationCouldNotMove _I10NS(@"Could not move to Applications folder")
  19. #define kStrMoveApplicationQuestionTitle _I10NS(@"Move to Applications folder?")
  20. #define kStrMoveApplicationQuestionTitleHome _I10NS(@"Move to Applications folder in your Home folder?")
  21. #define kStrMoveApplicationQuestionMessage _I10NS(@"I can move myself to the Applications folder if you'd like.")
  22. #define kStrMoveApplicationButtonMove _I10NS(@"Move to Applications Folder")
  23. #define kStrMoveApplicationButtonDoNotMove _I10NS(@"Do Not Move")
  24. #define kStrMoveApplicationQuestionInfoWillRequirePasswd _I10NS(@"Note that this will require an administrator password.")
  25. #define kStrMoveApplicationQuestionInfoInDownloadsFolder _I10NS(@"This will keep your Downloads folder uncluttered.")
  26. // Needs to be defined for compiling under 10.5 SDK
  27. #ifndef NSAppKitVersionNumber10_5
  28. #define NSAppKitVersionNumber10_5 949
  29. #endif
  30. // By default, we use a small control/font for the suppression button.
  31. // If you prefer to use the system default (to match your other alerts),
  32. // set this to 0.
  33. #define PFUseSmallAlertSuppressCheckbox 1
  34. static NSString *AlertSuppressKey = @"moveToApplicationsFolderAlertSuppress";
  35. // Helper functions
  36. static NSString *PreferredInstallLocation(BOOL *isUserDirectory);
  37. static BOOL IsInApplicationsFolder(NSString *path);
  38. static BOOL IsInDownloadsFolder(NSString *path);
  39. static BOOL IsApplicationAtPathRunning(NSString *path);
  40. static BOOL IsApplicationAtPathNested(NSString *path);
  41. static NSString *ContainingDiskImageDevice(NSString *path);
  42. static BOOL Trash(NSString *path);
  43. static BOOL DeleteOrTrash(NSString *path);
  44. static BOOL AuthorizedInstall(NSString *srcPath, NSString *dstPath, BOOL *canceled);
  45. static BOOL CopyBundle(NSString *srcPath, NSString *dstPath);
  46. static NSString *ShellQuotedString(NSString *string);
  47. static void Relaunch(NSString *destinationPath);
  48. // Main worker function
  49. void PFMoveToApplicationsFolderIfNecessary(void) {
  50. // Skip if user suppressed the alert before
  51. if ([[NSUserDefaults standardUserDefaults] boolForKey:AlertSuppressKey]) return;
  52. // Path of the bundle
  53. NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
  54. // Check if the bundle is embedded in another application
  55. BOOL isNestedApplication = IsApplicationAtPathNested(bundlePath);
  56. // Skip if the application is already in some Applications folder,
  57. // unless it's inside another app's bundle.
  58. if (IsInApplicationsFolder(bundlePath) && !isNestedApplication) return;
  59. // File Manager
  60. NSFileManager *fm = [NSFileManager defaultManager];
  61. // Are we on a disk image?
  62. NSString *diskImageDevice = ContainingDiskImageDevice(bundlePath);
  63. // Since we are good to go, get the preferred installation directory.
  64. BOOL installToUserApplications = NO;
  65. NSString *applicationsDirectory = PreferredInstallLocation(&installToUserApplications);
  66. NSString *bundleName = [bundlePath lastPathComponent];
  67. NSString *destinationPath = [applicationsDirectory stringByAppendingPathComponent:bundleName];
  68. // Check if we need admin password to write to the Applications directory
  69. BOOL needAuthorization = ([fm isWritableFileAtPath:applicationsDirectory] == NO);
  70. // Check if the destination bundle is already there but not writable
  71. needAuthorization |= ([fm fileExistsAtPath:destinationPath] && ![fm isWritableFileAtPath:destinationPath]);
  72. // Setup the alert
  73. NSAlert *alert = [[[NSAlert alloc] init] autorelease];
  74. {
  75. NSString *informativeText = nil;
  76. [alert setMessageText:(installToUserApplications ? kStrMoveApplicationQuestionTitleHome : kStrMoveApplicationQuestionTitle)];
  77. informativeText = kStrMoveApplicationQuestionMessage;
  78. if (needAuthorization) {
  79. informativeText = [informativeText stringByAppendingString:@" "];
  80. informativeText = [informativeText stringByAppendingString:kStrMoveApplicationQuestionInfoWillRequirePasswd];
  81. }
  82. else if (IsInDownloadsFolder(bundlePath)) {
  83. // Don't mention this stuff if we need authentication. The informative text is long enough as it is in that case.
  84. informativeText = [informativeText stringByAppendingString:@" "];
  85. informativeText = [informativeText stringByAppendingString:kStrMoveApplicationQuestionInfoInDownloadsFolder];
  86. }
  87. [alert setInformativeText:informativeText];
  88. // Add accept button
  89. [alert addButtonWithTitle:kStrMoveApplicationButtonMove];
  90. // Add deny button
  91. NSButton *cancelButton = [alert addButtonWithTitle:kStrMoveApplicationButtonDoNotMove];
  92. [cancelButton setKeyEquivalent:[NSString stringWithFormat:@"%C", 0x1b]]; // Escape key
  93. // Setup suppression button
  94. [alert setShowsSuppressionButton:YES];
  95. if (PFUseSmallAlertSuppressCheckbox) {
  96. NSCell *cell = [[alert suppressionButton] cell];
  97. [cell setControlSize:NSSmallControlSize];
  98. [cell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
  99. }
  100. }
  101. // Activate app -- work-around for focus issues related to "scary file from internet" OS dialog.
  102. if (![NSApp isActive]) {
  103. [NSApp activateIgnoringOtherApps:YES];
  104. }
  105. if ([alert runModal] == NSAlertFirstButtonReturn) {
  106. NSLog(@"INFO -- Moving myself to the Applications folder");
  107. // Move
  108. if (needAuthorization) {
  109. BOOL authorizationCanceled;
  110. if (!AuthorizedInstall(bundlePath, destinationPath, &authorizationCanceled)) {
  111. if (authorizationCanceled) {
  112. NSLog(@"INFO -- Not moving because user canceled authorization");
  113. return;
  114. }
  115. else {
  116. NSLog(@"ERROR -- Could not copy myself to /Applications with authorization");
  117. goto fail;
  118. }
  119. }
  120. }
  121. else {
  122. // If a copy already exists in the Applications folder, put it in the Trash
  123. if ([fm fileExistsAtPath:destinationPath]) {
  124. // But first, make sure that it's not running
  125. if (IsApplicationAtPathRunning(destinationPath)) {
  126. // Give the running app focus and terminate myself
  127. NSLog(@"INFO -- Switching to an already running version");
  128. [[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject:destinationPath]] waitUntilExit];
  129. exit(0);
  130. }
  131. else {
  132. if (!Trash([applicationsDirectory stringByAppendingPathComponent:bundleName]))
  133. goto fail;
  134. }
  135. }
  136. if (!CopyBundle(bundlePath, destinationPath)) {
  137. NSLog(@"ERROR -- Could not copy myself to %@", destinationPath);
  138. goto fail;
  139. }
  140. }
  141. // Trash the original app. It's okay if this fails.
  142. // NOTE: This final delete does not work if the source bundle is in a network mounted volume.
  143. // Calling rm or file manager's delete method doesn't work either. It's unlikely to happen
  144. // but it'd be great if someone could fix this.
  145. if (!isNestedApplication && diskImageDevice == nil && !DeleteOrTrash(bundlePath)) {
  146. NSLog(@"WARNING -- Could not delete application after moving it to Applications folder");
  147. }
  148. // Relaunch.
  149. Relaunch(destinationPath);
  150. // Launched from within a disk image? -- unmount (if no files are open after 5 seconds,
  151. // otherwise leave it mounted).
  152. if (diskImageDevice && !isNestedApplication) {
  153. NSString *script = [NSString stringWithFormat:@"(/bin/sleep 5 && /usr/bin/hdiutil detach %@) &", ShellQuotedString(diskImageDevice)];
  154. [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
  155. }
  156. exit(0);
  157. }
  158. // Save the alert suppress preference if checked
  159. else if ([[alert suppressionButton] state] == NSOnState) {
  160. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:AlertSuppressKey];
  161. }
  162. return;
  163. fail:
  164. {
  165. // Show failure message
  166. alert = [[[NSAlert alloc] init] autorelease];
  167. [alert setMessageText:kStrMoveApplicationCouldNotMove];
  168. [alert runModal];
  169. }
  170. }
  171. #pragma mark -
  172. #pragma mark Helper Functions
  173. static NSString *PreferredInstallLocation(BOOL *isUserDirectory) {
  174. // Return the preferred install location.
  175. // Assume that if the user has a ~/Applications folder, they'd prefer their
  176. // applications to go there.
  177. #if 0
  178. NSFileManager *fm = [NSFileManager defaultManager];
  179. NSArray *userApplicationsDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
  180. if ([userApplicationsDirs count] > 0) {
  181. NSString *userApplicationsDir = [userApplicationsDirs objectAtIndex:0];
  182. BOOL isDirectory;
  183. if ([fm fileExistsAtPath:userApplicationsDir isDirectory:&isDirectory] && isDirectory) {
  184. // User Applications directory exists. Get the directory contents.
  185. NSArray *contents = [fm contentsOfDirectoryAtPath:userApplicationsDir error:NULL];
  186. // Check if there is at least one ".app" inside the directory.
  187. for (NSString *contentsPath in contents) {
  188. if ([[contentsPath pathExtension] isEqualToString:@"app"]) {
  189. if (isUserDirectory) *isUserDirectory = YES;
  190. return [userApplicationsDir stringByResolvingSymlinksInPath];
  191. }
  192. }
  193. }
  194. }
  195. #endif
  196. // No user Applications directory in use. Return the machine local Applications directory
  197. if (isUserDirectory) *isUserDirectory = NO;
  198. return [[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSLocalDomainMask, YES) lastObject] stringByResolvingSymlinksInPath];
  199. }
  200. static BOOL IsInApplicationsFolder(NSString *path) {
  201. // Check all the normal Application directories
  202. NSArray *applicationDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
  203. for (NSString *appDir in applicationDirs) {
  204. if ([path hasPrefix:appDir]) return YES;
  205. }
  206. // Also, handle the case that the user has some other Application directory (perhaps on a separate data partition).
  207. if ([[path pathComponents] containsObject:@"Applications"]) return YES;
  208. return NO;
  209. }
  210. static BOOL IsInDownloadsFolder(NSString *path) {
  211. NSArray *downloadDirs = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSAllDomainsMask, YES);
  212. for (NSString *downloadsDirPath in downloadDirs) {
  213. if ([path hasPrefix:downloadsDirPath]) return YES;
  214. }
  215. return NO;
  216. }
  217. static BOOL IsApplicationAtPathRunning(NSString *bundlePath) {
  218. bundlePath = [bundlePath stringByStandardizingPath];
  219. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  220. // Use the new API on 10.6 or higher to determine if the app is already running
  221. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  222. for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) {
  223. NSString *runningAppBundlePath = [[[runningApplication bundleURL] path] stringByStandardizingPath];
  224. if ([runningAppBundlePath isEqualToString:bundlePath]) {
  225. return YES;
  226. }
  227. }
  228. return NO;
  229. }
  230. #endif
  231. // Use the shell to determine if the app is already running on systems 10.5 or lower
  232. NSString *script = [NSString stringWithFormat:@"/bin/ps ax -o comm | /usr/bin/grep %@/ | /usr/bin/grep -v grep >/dev/null", ShellQuotedString(bundlePath)];
  233. NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
  234. [task waitUntilExit];
  235. // If the task terminated with status 0, it means that the final grep produced 1 or more lines of output.
  236. // Which means that the app is already running
  237. return [task terminationStatus] == 0;
  238. }
  239. static BOOL IsApplicationAtPathNested(NSString *path) {
  240. NSString *containingPath = [path stringByDeletingLastPathComponent];
  241. NSArray *components = [containingPath pathComponents];
  242. for (NSString *component in components) {
  243. if ([[component pathExtension] isEqualToString:@"app"]) {
  244. return YES;
  245. }
  246. }
  247. return NO;
  248. }
  249. static NSString *ContainingDiskImageDevice(NSString *path) {
  250. NSString *containingPath = [path stringByDeletingLastPathComponent];
  251. struct statfs fs;
  252. if (statfs([containingPath fileSystemRepresentation], &fs) || (fs.f_flags & MNT_ROOTFS))
  253. return nil;
  254. NSString *device = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:fs.f_mntfromname length:strlen(fs.f_mntfromname)];
  255. NSTask *hdiutil = [[[NSTask alloc] init] autorelease];
  256. [hdiutil setLaunchPath:@"/usr/bin/hdiutil"];
  257. [hdiutil setArguments:[NSArray arrayWithObjects:@"info", @"-plist", nil]];
  258. [hdiutil setStandardOutput:[NSPipe pipe]];
  259. [hdiutil launch];
  260. [hdiutil waitUntilExit];
  261. NSData *data = [[[hdiutil standardOutput] fileHandleForReading] readDataToEndOfFile];
  262. NSDictionary *info = nil;
  263. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  264. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  265. info = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
  266. }
  267. else {
  268. #endif
  269. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
  270. info = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
  271. #endif
  272. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  273. }
  274. #endif
  275. if (![info isKindOfClass:[NSDictionary class]]) return nil;
  276. NSArray *images = (NSArray *)[info objectForKey:@"images"];
  277. if (![images isKindOfClass:[NSArray class]]) return nil;
  278. for (NSDictionary *image in images) {
  279. if (![image isKindOfClass:[NSDictionary class]]) return nil;
  280. id systemEntities = [image objectForKey:@"system-entities"];
  281. if (![systemEntities isKindOfClass:[NSArray class]]) return nil;
  282. for (NSDictionary *systemEntity in systemEntities) {
  283. if (![systemEntity isKindOfClass:[NSDictionary class]]) return nil;
  284. NSString *devEntry = [systemEntity objectForKey:@"dev-entry"];
  285. if (![devEntry isKindOfClass:[NSString class]]) return nil;
  286. if ([devEntry isEqualToString:device])
  287. return device;
  288. }
  289. }
  290. return nil;
  291. }
  292. static BOOL Trash(NSString *path) {
  293. BOOL result = NO;
  294. #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
  295. if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_8) {
  296. result = [[NSFileManager defaultManager] trashItemAtURL:[NSURL fileURLWithPath:path] resultingItemURL:NULL error:NULL];
  297. }
  298. #endif
  299. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_11
  300. if (!result) {
  301. result = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
  302. source:[path stringByDeletingLastPathComponent]
  303. destination:@""
  304. files:[NSArray arrayWithObject:[path lastPathComponent]]
  305. tag:NULL];
  306. }
  307. #endif
  308. // As a last resort try trashing with AppleScript.
  309. // This allows us to trash the app in macOS Sierra even when the app is running inside
  310. // an app translocation image.
  311. if (!result) {
  312. NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:
  313. [NSString stringWithFormat:@"\
  314. set theFile to POSIX file \"%@\" \n\
  315. tell application \"Finder\" \n\
  316. move theFile to trash \n\
  317. end tell", path]] autorelease];
  318. NSDictionary *errorDict = nil;
  319. NSAppleEventDescriptor *scriptResult = [appleScript executeAndReturnError:&errorDict];
  320. if (scriptResult == nil) {
  321. NSLog(@"Trash AppleScript error: %@", errorDict);
  322. }
  323. result = (scriptResult != nil);
  324. }
  325. if (!result) {
  326. NSLog(@"ERROR -- Could not trash '%@'", path);
  327. }
  328. return result;
  329. }
  330. static BOOL DeleteOrTrash(NSString *path) {
  331. NSError *error;
  332. if ([[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
  333. return YES;
  334. }
  335. else {
  336. // Don't log warning if on Sierra and running inside App Translocation path
  337. if (![path containsString:@"/AppTranslocation/"])
  338. NSLog(@"WARNING -- Could not delete '%@': %@", path, [error localizedDescription]);
  339. return Trash(path);
  340. }
  341. }
  342. static BOOL AuthorizedInstall(NSString *srcPath, NSString *dstPath, BOOL *canceled) {
  343. if (canceled) *canceled = NO;
  344. // Make sure that the destination path is an app bundle. We're essentially running 'sudo rm -rf'
  345. // so we really don't want to fuck this up.
  346. if (![[dstPath pathExtension] isEqualToString:@"app"]) return NO;
  347. // Do some more checks
  348. if ([[dstPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
  349. if ([[srcPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
  350. int pid, status;
  351. AuthorizationRef myAuthorizationRef;
  352. // Get the authorization
  353. OSStatus err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &myAuthorizationRef);
  354. if (err != errAuthorizationSuccess) return NO;
  355. AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
  356. AuthorizationRights myRights = {1, &myItems};
  357. AuthorizationFlags myFlags = (AuthorizationFlags)(kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize);
  358. err = AuthorizationCopyRights(myAuthorizationRef, &myRights, NULL, myFlags, NULL);
  359. if (err != errAuthorizationSuccess) {
  360. if (err == errAuthorizationCanceled && canceled)
  361. *canceled = YES;
  362. goto fail;
  363. }
  364. static OSStatus (*security_AuthorizationExecuteWithPrivileges)(AuthorizationRef authorization, const char *pathToTool,
  365. AuthorizationFlags options, char * const *arguments,
  366. FILE **communicationsPipe) = NULL;
  367. if (!security_AuthorizationExecuteWithPrivileges) {
  368. // On 10.7, AuthorizationExecuteWithPrivileges is deprecated. We want to still use it since there's no
  369. // good alternative (without requiring code signing). We'll look up the function through dyld and fail
  370. // if it is no longer accessible. If Apple removes the function entirely this will fail gracefully. If
  371. // they keep the function and throw some sort of exception, this won't fail gracefully, but that's a
  372. // risk we'll have to take for now.
  373. security_AuthorizationExecuteWithPrivileges = (OSStatus (*)(AuthorizationRef, const char*,
  374. AuthorizationFlags, char* const*,
  375. FILE **)) dlsym(RTLD_DEFAULT, "AuthorizationExecuteWithPrivileges");
  376. }
  377. if (!security_AuthorizationExecuteWithPrivileges) goto fail;
  378. // Delete the destination
  379. {
  380. char *args[] = {"-rf", (char *)[dstPath fileSystemRepresentation], NULL};
  381. err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/rm", kAuthorizationFlagDefaults, args, NULL);
  382. if (err != errAuthorizationSuccess) goto fail;
  383. // Wait until it's done
  384. pid = wait(&status);
  385. if (pid == -1 || !WIFEXITED(status)) goto fail; // We don't care about exit status as the destination most likely does not exist
  386. }
  387. // Copy
  388. {
  389. char *args[] = {"-pR", (char *)[srcPath fileSystemRepresentation], (char *)[dstPath fileSystemRepresentation], NULL};
  390. err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/cp", kAuthorizationFlagDefaults, args, NULL);
  391. if (err != errAuthorizationSuccess) goto fail;
  392. // Wait until it's done
  393. pid = wait(&status);
  394. if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) goto fail;
  395. }
  396. AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
  397. return YES;
  398. fail:
  399. AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
  400. return NO;
  401. }
  402. static BOOL CopyBundle(NSString *srcPath, NSString *dstPath) {
  403. NSFileManager *fm = [NSFileManager defaultManager];
  404. NSError *error = nil;
  405. if ([fm copyItemAtPath:srcPath toPath:dstPath error:&error]) {
  406. return YES;
  407. }
  408. else {
  409. NSLog(@"ERROR -- Could not copy '%@' to '%@' (%@)", srcPath, dstPath, error);
  410. return NO;
  411. }
  412. }
  413. static NSString *ShellQuotedString(NSString *string) {
  414. return [NSString stringWithFormat:@"'%@'", [string stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]];
  415. }
  416. static void Relaunch(NSString *destinationPath) {
  417. // The shell script waits until the original app process terminates.
  418. // This is done so that the relaunched app opens as the front-most app.
  419. int pid = [[NSProcessInfo processInfo] processIdentifier];
  420. // Command run just before running open /final/path
  421. NSString *preOpenCmd = @"";
  422. NSString *quotedDestinationPath = ShellQuotedString(destinationPath);
  423. // OS X >=10.5:
  424. // Before we launch the new app, clear xattr:com.apple.quarantine to avoid
  425. // duplicate "scary file from the internet" dialog.
  426. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  427. // Add the -r flag on 10.6
  428. preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d -r com.apple.quarantine %@", quotedDestinationPath];
  429. }
  430. else {
  431. preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d com.apple.quarantine %@", quotedDestinationPath];
  432. }
  433. NSString *script = [NSString stringWithFormat:@"(while /bin/kill -0 %d >&/dev/null; do /bin/sleep 0.1; done; %@; /usr/bin/open %@) &", pid, preOpenCmd, quotedDestinationPath];
  434. [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
  435. }