NSAppleScriptErrorNumber: -600
is a sandbox issue, to fix it (tested in Xcode 14.3):
- Go to [Your Project] > [Your Target] > Signing & Capabilities.
- Delete App Sandbox.
- Under Hardened Runtime (add the capability first by clicking + Capability if necessary), check "Apple Events".
- Add
NSAppleEventsUsageDescription
to your Info.plist
.
Using @kakaiikaka's answer,
func openGetInfoWnd(for urls: [URL]) {
let fileList = urls.map { "POSIX file \"\($0.absoluteString)\"" }
let source = """
set fileList to {\(fileList.joined(separator: ", "))}
tell application "Finder"
activate
set selection to fileList
tell application "System Events"
keystroke "i" using {command down, option down}
end tell
end tell
"""
let appleScript = NSAppleScript(source: source)!
var error: NSDictionary?
appleScript.executeAndReturnError(&error)
// ...
}
Rebuild your app (clean build + run) and it should now ask for permissions to send keystrokes to Finder. You'll likely see these pop-ups:
- Asking for permission to access the directories (the urls with which the function is invoked).
- Asking for permission to control "Finder" and "System Events" (separate pop-ups with the message you specified for the key
NSAppleEventsUsageDescription
).
- Asking for Accessibility Access (you'll have to go to System Settings > Privacy & Security > Accessibility to grant access).
Now everything should work! :)
P.S. If you get a message saying "This method should not be called on the main thread as it may lead to UI unresponsiveness.", just do:
DispatchQueue(label: "AppleScript").async {
// execute the applescript here instead...
}
(you can also set qos = .background
if you want, but either way should work)