[futurebasic] Re: [FB] Running app from Download folder

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2017 : Group Archive : Group : All Groups

From: Bernie <bernie.fblist@...>
Date: Fri, 10 Mar 2017 08:54:06 +0000
Brian wrote:
> 
>> Steve Crossman wrote:
>> 
>> When the app runs from the Downloads folder in macOS 10.12, it cannot properly locate this folder
> 
> Perhaps not as satisfying as fixing the security issue but the app could notify the user ( put up a dialog ) if it finds itself being launched in the Downloads directory ( i.e. ask them to drag the app to the desktop etc. )
> 

The following function is based on Brian’s excellent idea. There’s extra code to check if the the app is in a folder (or folders) deeper in the Downloads directory.

// ---------------
void local fn AreWeInDownloadsDirectory
'~'1
BeginCCode
NSString *appDirPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
NSString *downloadsDirPath = [[[[NSFileManager defaultManager] URLsForDirectory:NSDownloadsDirectory inDomains:NSUserDomainMask] objectAtIndex:0] path];
NSUInteger length = [downloadsDirPath length];
if ( length <= [appDirPath length] ) {
     if ( [downloadsDirPath isEqualToString:[appDirPath substringToIndex:length]] ) {
          NSAlert *alert = [[NSAlert alloc] init];
          [alert setMessageText:@"Downloads directory error."];
          [alert addButtonWithTitle:@"Quit"];
          [alert setInformativeText:@"Cannot be run from the Downloads directory.\nMove this app to another location and try again."];
          if ( [alert runModal] == NSAlertFirstButtonReturn ) [NSApp terminate:nil];
     }
}
EndC
end fn

fn AreWeInDownloadsDirectory()

window 1, @"My App"
print "We got here!"

RunApplicationEventLoop()