Skip to content

Instantly share code, notes, and snippets.

@macsimom
Last active November 10, 2022 17:10
Show Gist options
  • Save macsimom/8445e8b18433ef3227ab4866ce4a96e1 to your computer and use it in GitHub Desktop.
Save macsimom/8445e8b18433ef3227ab4866ce4a96e1 to your computer and use it in GitHub Desktop.
//
// MIT License
//
// Copyright (c) 2022 Simon Andersen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// call it with osascript -l JavaScript setToOpenWithRosetta.js.applescript [full path to app]
ObjC.import('Foundation');
function run(argv) {
console.log(argv[0]);
if (argv[0] == undefined) {
//setToOpenWithRosetta(ObjC.wrap("/System/Applications/Chess.app"));
return 1;
}
else {
setToOpenWithRosetta($(argv[0]));
}
}
function setToOpenWithRosetta(appPath) {
lsPath = (ObjC.wrap("~/Library/Preferences/com.apple.LaunchServices/com.apple.LaunchServices.plist").stringByExpandingTildeInPath);
if ( $.NSFileManager.defaultManager.fileExistsAtPath(appPath) ) {
console.log("file exists");
lsDict = $.NSMutableDictionary.alloc.initWithContentsOfURL($.NSURL.fileURLWithPath(lsPath));
if (lsDict.isNil()) {
lsDict = $.NSMutableDictionary.alloc.init;
}
if (lsDict.objectForKey(ObjC.wrap("Architectures for arm64") ).isNil()){
lsDict.setObjectForKey($.NSMutableDictionary.dictionary, ObjC.wrap("Architectures for arm64"));
}
}
if ($.NSFileManager.defaultManager.fileExistsAtPath(appPath)) {
arm64dict = lsDict.objectForKey(ObjC.wrap("Architectures for arm64"));
bundleIdentifier = $.NSBundle.bundleWithPath(appPath).bundleIdentifier;
bookmark = $.NSURL.fileURLWithPath(appPath).bookmarkDataWithOptionsIncludingResourceValuesForKeysRelativeToURLError($.NSURLBookmarkCreationSuitableForBookmarkFile, $() ,$(),$());
if (arm64dict.objectForKey(bundleIdentifier).isNil() ) {
arm64dict.setObjectForKey(
$.NSMutableArray.arrayWithArray($([ ObjC.wrap(bookmark), ObjC.wrap("x86_64")]) )
, bundleIdentifier
);
}
else {
if ( ObjC.unwrap(arm64dict.objectForKey(bundleIdentifier).indexOfObject(bookmark)) == 9223372036854775807 ) {
console.log( ObjC.unwrap(arm64dict.objectForKey(bundleIdentifier).indexOfObject(bookmark)));
arm64dict.objectForKey(bundleIdentifier).addObjectsFromArray($.NSArray.arrayWithArray(ObjC.wrap([bookmark, ObjC.wrap("x86_64")]) ));
//arm64dict.objectForKey(bundleIdentifier).addObjectsFromArray($.NSArray.arrayWithArray($([$("foo"), ObjC.wrap("x86_64")]) ));
//arm64dict.objectForKey(bundleIdentifier).addObjectsFromArray($.NSArray.arrayWithArray($([$("foo"), ObjC.wrap("x86_64")]) ));
}
}
lsDict.setObjectForKey(arm64dict, ObjC.wrap("Architectures for arm64"));
lsDict.writeToFileAtomically(lsPath, ObjC.wrap(true));
killalllsd = $.NSTask.launchedTaskWithLaunchPathArguments( $("/usr/bin/killall") , $([$("lsd")]));
killalllsd.waitUntilExit;
console.log(ObjC.unwrap(lsDict.description));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment