Last active
January 20, 2022 03:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made a Py-Objc version at https://gist.github.com/WardsParadox/d6a8fbe0c0d1f100b5574188e4bd627d