Skip to content

Instantly share code, notes, and snippets.

@dautermann
Created January 9, 2018 12:34
Show Gist options
  • Save dautermann/119ca5bb0c851c88ea0289b94cb79be3 to your computer and use it in GitHub Desktop.
Save dautermann/119ca5bb0c851c88ea0289b94cb79be3 to your computer and use it in GitHub Desktop.
create desktop alias to current macOS app
#import <Cocoa/Cocoa.h>
@interface AddAliasToDesktopUtility : NSObject {
}
- (BOOL) addAliasNow;
@end
@implementation AddAliasToDesktopUtility
- (BOOL) addAliasNow
{
NSArray *arr = [[NSFileManager defaultManager] URLsForDirectory: NSDesktopDirectory inDomains: NSUserDomainMask];
NSURL *desktopUrl = [arr firstObject];
NSURL *originalUrl = [[NSBundle mainBundle] bundleURL];
NSURL *aliasUrl = [desktopUrl URLByAppendingPathComponent:[originalUrl lastPathComponent]];
NSData *bookmarkData = [originalUrl bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
NSError *err;
if(bookmarkData != nil) {
BOOL success = [NSURL writeBookmarkData:bookmarkData toURL:aliasUrl options:NSURLBookmarkCreationSuitableForBookmarkFile error:&err];
if(NO == success) {
//error
NSLog(@"error is %@", [err localizedDescription]);
return FALSE;
}
}
return TRUE;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment