It seems like the only way to get to the currently selected files/folders of the macOS Finder is via the Scripting Bridge. The SwiftScripting project provides some pre-generated extensions to use. The code is rather simple:
public func getSelected() -> [NSURL] {
guard let app = SBApplication(bundleIdentifier: "com.apple.finder") else {
print("no app")
return []
}
let finder = app as FinderApplication
guard let result = finder.selection else {
print("No items selected")
return []
}
print("result", result)
guard let selection = result.get() else {
print("get failed: ", result.lastError() ?? "")
return []
}
print("selection", selection)
return []
}
But unfortunately the get
of the selection fails
result <SBObject @0x600000d26310: selection of application "Finder" (6039)>
get failed: Error Domain=NSOSStatusErrorDomain Code=-1743 "(null)" UserInfo={ErrorNumber=-1743}
That error is errAEEventNotPermitted
. Which suggests the app needs the correct entitlements. The temporary exception would not be allowed on the App Store - but should in theory be enough. The scripting targets would probably the way to go - but it seems virtually impossible to find the relevant docs on this.
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.finder</string>
</array>
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.finder</key>
<array>
<string>???</string>
</array>
</dict>
</dict>
What parts am I missing to get this working?
Update:
What I was missing was the permission in the Info.plist
. The key Privacy - AppleEvents Sending Usage Description
needs to be set. That makes the get
call succeed.
To get this through the app store review though, setting com.apple.security.scripting-targets
correctly would still be required.
I am still looking for information on what target configurations are available for the Finder.