PFMoveApplication.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. NSFileManager *fm = [NSFileManager defaultManager];
  178. NSArray *userApplicationsDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
  179. if ([userApplicationsDirs count] > 0) {
  180. NSString *userApplicationsDir = [userApplicationsDirs objectAtIndex:0];
  181. BOOL isDirectory;
  182. if ([fm fileExistsAtPath:userApplicationsDir isDirectory:&isDirectory] && isDirectory) {
  183. // User Applications directory exists. Get the directory contents.
  184. NSArray *contents = [fm contentsOfDirectoryAtPath:userApplicationsDir error:NULL];
  185. // Check if there is at least one ".app" inside the directory.
  186. for (NSString *contentsPath in contents) {
  187. if ([[contentsPath pathExtension] isEqualToString:@"app"]) {
  188. if (isUserDirectory) *isUserDirectory = YES;
  189. return [userApplicationsDir stringByResolvingSymlinksInPath];
  190. }
  191. }
  192. }
  193. }
  194. // No user Applications directory in use. Return the machine local Applications directory
  195. if (isUserDirectory) *isUserDirectory = NO;
  196. return [[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSLocalDomainMask, YES) lastObject] stringByResolvingSymlinksInPath];
  197. }
  198. static BOOL IsInApplicationsFolder(NSString *path) {
  199. // Check all the normal Application directories
  200. NSArray *applicationDirs = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
  201. for (NSString *appDir in applicationDirs) {
  202. if ([path hasPrefix:appDir]) return YES;
  203. }
  204. // Also, handle the case that the user has some other Application directory (perhaps on a separate data partition).
  205. if ([[path pathComponents] containsObject:@"Applications"]) return YES;
  206. return NO;
  207. }
  208. static BOOL IsInDownloadsFolder(NSString *path) {
  209. NSArray *downloadDirs = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSAllDomainsMask, YES);
  210. for (NSString *downloadsDirPath in downloadDirs) {
  211. if ([path hasPrefix:downloadsDirPath]) return YES;
  212. }
  213. return NO;
  214. }
  215. static BOOL IsApplicationAtPathRunning(NSString *bundlePath) {
  216. bundlePath = [bundlePath stringByStandardizingPath];
  217. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  218. // Use the new API on 10.6 or higher to determine if the app is already running
  219. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  220. for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) {
  221. NSString *runningAppBundlePath = [[[runningApplication bundleURL] path] stringByStandardizingPath];
  222. if ([runningAppBundlePath isEqualToString:bundlePath]) {
  223. return YES;
  224. }
  225. }
  226. return NO;
  227. }
  228. #endif
  229. // Use the shell to determine if the app is already running on systems 10.5 or lower
  230. NSString *script = [NSString stringWithFormat:@"/bin/ps ax -o comm | /usr/bin/grep %@/ | /usr/bin/grep -v grep >/dev/null", ShellQuotedString(bundlePath)];
  231. NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
  232. [task waitUntilExit];
  233. // If the task terminated with status 0, it means that the final grep produced 1 or more lines of output.
  234. // Which means that the app is already running
  235. return [task terminationStatus] == 0;
  236. }
  237. static BOOL IsApplicationAtPathNested(NSString *path) {
  238. NSString *containingPath = [path stringByDeletingLastPathComponent];
  239. NSArray *components = [containingPath pathComponents];
  240. for (NSString *component in components) {
  241. if ([[component pathExtension] isEqualToString:@"app"]) {
  242. return YES;
  243. }
  244. }
  245. return NO;
  246. }
  247. static NSString *ContainingDiskImageDevice(NSString *path) {
  248. NSString *containingPath = [path stringByDeletingLastPathComponent];
  249. struct statfs fs;
  250. if (statfs([containingPath fileSystemRepresentation], &fs) || (fs.f_flags & MNT_ROOTFS))
  251. return nil;
  252. NSString *device = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:fs.f_mntfromname length:strlen(fs.f_mntfromname)];
  253. NSTask *hdiutil = [[[NSTask alloc] init] autorelease];
  254. [hdiutil setLaunchPath:@"/usr/bin/hdiutil"];
  255. [hdiutil setArguments:[NSArray arrayWithObjects:@"info", @"-plist", nil]];
  256. [hdiutil setStandardOutput:[NSPipe pipe]];
  257. [hdiutil launch];
  258. [hdiutil waitUntilExit];
  259. NSData *data = [[[hdiutil standardOutput] fileHandleForReading] readDataToEndOfFile];
  260. NSDictionary *info = nil;
  261. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  262. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  263. info = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
  264. }
  265. else {
  266. #endif
  267. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
  268. info = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
  269. #endif
  270. #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
  271. }
  272. #endif
  273. if (![info isKindOfClass:[NSDictionary class]]) return nil;
  274. NSArray *images = (NSArray *)[info objectForKey:@"images"];
  275. if (![images isKindOfClass:[NSArray class]]) return nil;
  276. for (NSDictionary *image in images) {
  277. if (![image isKindOfClass:[NSDictionary class]]) return nil;
  278. id systemEntities = [image objectForKey:@"system-entities"];
  279. if (![systemEntities isKindOfClass:[NSArray class]]) return nil;
  280. for (NSDictionary *systemEntity in systemEntities) {
  281. if (![systemEntity isKindOfClass:[NSDictionary class]]) return nil;
  282. NSString *devEntry = [systemEntity objectForKey:@"dev-entry"];
  283. if (![devEntry isKindOfClass:[NSString class]]) return nil;
  284. if ([devEntry isEqualToString:device])
  285. return device;
  286. }
  287. }
  288. return nil;
  289. }
  290. static BOOL Trash(NSString *path) {
  291. BOOL result = NO;
  292. #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
  293. if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_8) {
  294. result = [[NSFileManager defaultManager] trashItemAtURL:[NSURL fileURLWithPath:path] resultingItemURL:NULL error:NULL];
  295. }
  296. #endif
  297. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_11
  298. if (!result) {
  299. result = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
  300. source:[path stringByDeletingLastPathComponent]
  301. destination:@""
  302. files:[NSArray arrayWithObject:[path lastPathComponent]]
  303. tag:NULL];
  304. }
  305. #endif
  306. // As a last resort try trashing with AppleScript.
  307. // This allows us to trash the app in macOS Sierra even when the app is running inside
  308. // an app translocation image.
  309. if (!result) {
  310. NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:
  311. [NSString stringWithFormat:@"\
  312. set theFile to POSIX file \"%@\" \n\
  313. tell application \"Finder\" \n\
  314. move theFile to trash \n\
  315. end tell", path]] autorelease];
  316. NSDictionary *errorDict = nil;
  317. NSAppleEventDescriptor *scriptResult = [appleScript executeAndReturnError:&errorDict];
  318. if (scriptResult == nil) {
  319. NSLog(@"Trash AppleScript error: %@", errorDict);
  320. }
  321. result = (scriptResult != nil);
  322. }
  323. if (!result) {
  324. NSLog(@"ERROR -- Could not trash '%@'", path);
  325. }
  326. return result;
  327. }
  328. static BOOL DeleteOrTrash(NSString *path) {
  329. NSError *error;
  330. if ([[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
  331. return YES;
  332. }
  333. else {
  334. // Don't log warning if on Sierra and running inside App Translocation path
  335. if (![path containsString:@"/AppTranslocation/"])
  336. NSLog(@"WARNING -- Could not delete '%@': %@", path, [error localizedDescription]);
  337. return Trash(path);
  338. }
  339. }
  340. static BOOL AuthorizedInstall(NSString *srcPath, NSString *dstPath, BOOL *canceled) {
  341. if (canceled) *canceled = NO;
  342. // Make sure that the destination path is an app bundle. We're essentially running 'sudo rm -rf'
  343. // so we really don't want to fuck this up.
  344. if (![[dstPath pathExtension] isEqualToString:@"app"]) return NO;
  345. // Do some more checks
  346. if ([[dstPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
  347. if ([[srcPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) return NO;
  348. int pid, status;
  349. AuthorizationRef myAuthorizationRef;
  350. // Get the authorization
  351. OSStatus err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &myAuthorizationRef);
  352. if (err != errAuthorizationSuccess) return NO;
  353. AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
  354. AuthorizationRights myRights = {1, &myItems};
  355. AuthorizationFlags myFlags = (AuthorizationFlags)(kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize);
  356. err = AuthorizationCopyRights(myAuthorizationRef, &myRights, NULL, myFlags, NULL);
  357. if (err != errAuthorizationSuccess) {
  358. if (err == errAuthorizationCanceled && canceled)
  359. *canceled = YES;
  360. goto fail;
  361. }
  362. static OSStatus (*security_AuthorizationExecuteWithPrivileges)(AuthorizationRef authorization, const char *pathToTool,
  363. AuthorizationFlags options, char * const *arguments,
  364. FILE **communicationsPipe) = NULL;
  365. if (!security_AuthorizationExecuteWithPrivileges) {
  366. // On 10.7, AuthorizationExecuteWithPrivileges is deprecated. We want to still use it since there's no
  367. // good alternative (without requiring code signing). We'll look up the function through dyld and fail
  368. // if it is no longer accessible. If Apple removes the function entirely this will fail gracefully. If
  369. // they keep the function and throw some sort of exception, this won't fail gracefully, but that's a
  370. // risk we'll have to take for now.
  371. security_AuthorizationExecuteWithPrivileges = (OSStatus (*)(AuthorizationRef, const char*,
  372. AuthorizationFlags, char* const*,
  373. FILE **)) dlsym(RTLD_DEFAULT, "AuthorizationExecuteWithPrivileges");
  374. }
  375. if (!security_AuthorizationExecuteWithPrivileges) goto fail;
  376. // Delete the destination
  377. {
  378. char *args[] = {"-rf", (char *)[dstPath fileSystemRepresentation], NULL};
  379. err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/rm", kAuthorizationFlagDefaults, args, NULL);
  380. if (err != errAuthorizationSuccess) goto fail;
  381. // Wait until it's done
  382. pid = wait(&status);
  383. if (pid == -1 || !WIFEXITED(status)) goto fail; // We don't care about exit status as the destination most likely does not exist
  384. }
  385. // Copy
  386. {
  387. char *args[] = {"-pR", (char *)[srcPath fileSystemRepresentation], (char *)[dstPath fileSystemRepresentation], NULL};
  388. err = security_AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/bin/cp", kAuthorizationFlagDefaults, args, NULL);
  389. if (err != errAuthorizationSuccess) goto fail;
  390. // Wait until it's done
  391. pid = wait(&status);
  392. if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) goto fail;
  393. }
  394. AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
  395. return YES;
  396. fail:
  397. AuthorizationFree(myAuthorizationRef, kAuthorizationFlagDefaults);
  398. return NO;
  399. }
  400. static BOOL CopyBundle(NSString *srcPath, NSString *dstPath) {
  401. NSFileManager *fm = [NSFileManager defaultManager];
  402. NSError *error = nil;
  403. if ([fm copyItemAtPath:srcPath toPath:dstPath error:&error]) {
  404. return YES;
  405. }
  406. else {
  407. NSLog(@"ERROR -- Could not copy '%@' to '%@' (%@)", srcPath, dstPath, error);
  408. return NO;
  409. }
  410. }
  411. static NSString *ShellQuotedString(NSString *string) {
  412. return [NSString stringWithFormat:@"'%@'", [string stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]];
  413. }
  414. static void Relaunch(NSString *destinationPath) {
  415. // The shell script waits until the original app process terminates.
  416. // This is done so that the relaunched app opens as the front-most app.
  417. int pid = [[NSProcessInfo processInfo] processIdentifier];
  418. // Command run just before running open /final/path
  419. NSString *preOpenCmd = @"";
  420. NSString *quotedDestinationPath = ShellQuotedString(destinationPath);
  421. // OS X >=10.5:
  422. // Before we launch the new app, clear xattr:com.apple.quarantine to avoid
  423. // duplicate "scary file from the internet" dialog.
  424. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
  425. // Add the -r flag on 10.6
  426. preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d -r com.apple.quarantine %@", quotedDestinationPath];
  427. }
  428. else {
  429. preOpenCmd = [NSString stringWithFormat:@"/usr/bin/xattr -d com.apple.quarantine %@", quotedDestinationPath];
  430. }
  431. NSString *script = [NSString stringWithFormat:@"(while /bin/kill -0 %d >&/dev/null; do /bin/sleep 0.1; done; %@; /usr/bin/open %@) &", pid, preOpenCmd, quotedDestinationPath];
  432. [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
  433. }