3

I try to run AppleScript command from Swift code like this:

 var appleScriptCmd = "tell application \"System Events\" to make login item at end with properties {path:\"" + appPath +  "\", hidden:false, name:\"Some App\"}";

var appleScriptCmd2 = "tell application \"System Events\" to set visible of process \"Safari\" to false";

and then I have tried both:

let script = NSAppleScript(source: appleScriptCmd2)!;
        var errorDict : NSDictionary?
        script.executeAndReturnError(&errorDict)
        if errorDict != nil { print(errorDict!) }

or older approach:

Process.launchedProcess(launchPath: "/usr/bin/osascript", arguments: ["-e", appleScriptCmd])

neither works and simultaneously both commands I have tried are working from Terminal program using osascript -e "some command" tool.

3
  • The code is supposed to work. Is the app sandboxed? Commented May 17, 2017 at 14:18
  • what does it mean sandboxed? Commented May 17, 2017 at 14:26
  • I have in Project Settings > Capabilities turn on App Sandbox, but I am running the app normally by pressing Play button (without Archive) Commented May 17, 2017 at 14:26

1 Answer 1

5

Since your app is sandboxed (Project Settings > Capabilities turn on App Sandbox) you have three options:

  • Add temporary entitlements for the applications you want to use.
  • Put your scripts in the appropriate directory in ~/Library/Application Scripts/ and use NSUserAppleScriptTask.
  • Implement an AppleScriptObjC bridge and run the AppleScript code from the ASOC framework (requires also an Objective-C bridging header file).

In a sandboxed app NSAppleScript refuses to work.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you mean that app published in Mac App Store cannot use AppleScript?
Using one of the three options it can. The recommended option by Apple is the second one. For the first one you need good reasons to convince the Apple team and I don't know if option three is supported.
If I understand correctly, this script must be placed by the end application user manually in this "~/Library/Application Scripts/" or can be preinstalled by application?
Yes, there is the restriction that your application cannot write into the Application Scripts folder. I'd give the entitlements a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.