2

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47
tcurdt
  • 14,518
  • 10
  • 57
  • 72
  • @vadian That does not seem to be quite correct. It's working for me now - even from the sandbox. But I would still need the correct scripting target entitlements to get this through the review. – tcurdt Dec 12 '22 at 20:45
  • I deleted my previous comment but the app will be rejected from App Store. `ScriptingBridge` is not allowed. – vadian Dec 12 '22 at 20:49
  • @vadian The temporary exception is no longer allowed. But I do have one on the App Store using the ScriptingBridge. But you will need to use the proper scripting target entitlements. And I just cannot find the right docs for the Finder. – tcurdt Dec 12 '22 at 21:10
  • Does this answer your question? [How can I know the Apple Event Access Groups used by an application?](https://stackoverflow.com/questions/21636464/how-can-i-know-the-apple-event-access-groups-used-by-an-application) – Willeke Dec 12 '22 at 23:28
  • To retrieve the currently selected files or folders from macOS Finder, you can use the Scripting Bridge and the SwiftScripting project to generate extensions but you will need to set the correct entitlements and permission in the Info.plist to get it to work. it may not be allowed in the app store review – Goran Feb 26 '23 at 18:46

0 Answers0