Skip to content

Instantly share code, notes, and snippets.

@macsimom
Last active January 20, 2022 03:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save macsimom/46dd93ad29f085cc1e109315eb59ebe3 to your computer and use it in GitHub Desktop.
Save macsimom/46dd93ad29f085cc1e109315eb59ebe3 to your computer and use it in GitHub Desktop.
openwithrosetta - a tool to check the box "Open with Rosetta" on Apple Silicon Big Sur Macs
//
// main.m
// openwithrosetta
//
// Created by Simon Andersen on 13/09/2021.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSMutableDictionary *lsDict;
NSString *appPath;
NSString *bundleIdentifier;
NSData *bookmark;
NSString *lsPath = [@"~/Library/Preferences/com.apple.LaunchServices/com.apple.LaunchServices.plist" stringByExpandingTildeInPath];
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
if ([arguments count] == 2) {
appPath = arguments[1];
if ([[NSFileManager defaultManager] fileExistsAtPath:appPath]) {
lsDict = [[NSMutableDictionary alloc] initWithContentsOfURL:[NSURL fileURLWithPath:lsPath]];
if (lsDict == nil) {
lsDict = [[NSMutableDictionary alloc] init];
}
if ([lsDict objectForKey:@"Architectures for arm64"] == nil){
[lsDict setObject:[NSMutableDictionary dictionary] forKey:@"Architectures for arm64"];
}
NSMutableDictionary *arm64dict =[lsDict objectForKey:@"Architectures for arm64"];
bundleIdentifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
bookmark = [[NSURL fileURLWithPath:appPath] bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:nil];
if ([arm64dict objectForKey:bundleIdentifier] == nil) {
[arm64dict setObject:[NSMutableArray arrayWithObjects:bookmark,@"x86_64", nil] forKey:bundleIdentifier];
}
else {
[ (NSMutableArray *)[arm64dict objectForKey:bundleIdentifier] addObjectsFromArray:[NSArray arrayWithObjects: bookmark,@"x86_64",nil]];
}
[lsDict setObject:arm64dict forKey:@"Architectures for arm64"];
[lsDict writeToFile:lsPath atomically:YES];
NSTask *killalllsd = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall" arguments:[NSArray arrayWithObjects:@"lsd", nil]];
[killalllsd waitUntilExit];
}
else {
NSLog(@"Invalid path: %@",arguments[1]);
return 1;
}
}
else {
NSLog(@"Not enough arguments");
return 1;
}
}
return 0;
}
@WardsParadox
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment