App Group - Sharing 'Application Scripts' directory

I will be running user-provided scripts via a Mac application *and* and an extension of the app. The app and extension both belong to the same App Group and individually have the correct entitlements to run scripts.



The Mac app is Sandboxed. According to Apple, any scripts must reside within the Applications Scripts directory: `. applicationScriptsDirectory` / `NSApplicationScriptsDirectory`.



> The NSUserAppleScriptTask class is intended to run AppleScript scripts from your application. It is intended to execute user-supplied scripts and will execute them outside of the application's sandbox, if any.



> If the application is sandboxed, then the script must be in the `NSApplicationScriptsDirectory` folder. A sandboxed application may read from, but not write to, this folder.



>https://developer.apple.com/documentation/foundation/nsuserapplescripttask



The issue I'm facing is that the Main App and the App Extension resolve to different NSApplicationScriptsDirectories:



`let applicationScripts = try! FileManager.default.url(for: .applicationScriptsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)`



Different directories:



- ~/Library/Application Scripts/com.company.MainApp

- ~/Library/Application Scripts/com.company.MainAppFinderSync



I want to run the same scripts from both the App and the Extension. It works to launch scripts from each component's own Application Scripts directory, but the scripts are **not launched** if they reside within the other component's directory.



It is not ideal to maintain a synchronized directory structure, with matching scripts in both locations. It's also difficult to guide the user to put scripts in the correct location, as the main app's Preferences guide cannot open the extension's scripts directory.



**App Groups** solve this issue for other common app/extension scenarios, such as sharing data through NSUserDefaults:



> After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences.



> https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html



Is there any ability to share an Applications Script folder between app and extension that belong to the same App Group?

Replies

Is there any ability to share an Applications Script folder between app and extension that belong to the same App Group?

As far as I know there is not. I don’t think the folks who designed

NSUserScriptTask
contemplated your situation. It seems like good enhancement request territory to me. If you do file a bug, please post your bug number, just for the record.

What sort of app extension are you using? One possible approach might be for the appex to ‘own’ the scripts and then, when necessary, have the app tell the appex to run a script.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

filed a radar here:


App Group [Main App + Extensions] should have access to same Application Scripts directory

rdar://44878320

filed a radar [44878320]

Thanks.

I’d still like to known your answers to the questions I posed yesterday.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

> What sort of app extension are you using?


A macOS FinderSync extension


> One possible approach might be for the appex to ‘own’ the scripts and then, when necessary, have the app tell the appex to run a script.


That's true, but I forsee issues with the extension not being launched when the main app wants to launch a script, or vice versa.


The main app will be a glorified Preferences window for the Extension, and will not be running the majority of the time. And there are many system pitfalls to expecting the Extension to be running at any point.


Having a single App Group location for the Scripts folder, and then programatically copying them to all of the needed Application Scripts folders seems like the best option in lieu of true App Group scripting support. Althought this does require Write Security Bookmarks that I was hoping to avoid, plus more disk access permission.