Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running AppleScript from Swift not working

I'm making a simple menubar app to do some actions such as toggling dark mode in Mojave

Writing an AppleScript for it is fine and when it works 100% when directly running the script. In order to implement it into a menu bar app, I tried searching online and the only answer I found was in this link: Can you execute an Applescript script from a Swift Application

However, when I tried the first solution, I got an error in the console saying:

script: /Users/username/Documents/myscript.scpt: Operation not permitted

My code looked like this:

let proc = Process()
proc.launchPath = "/usr/bin/script"
proc.arguments = ["/Users/username/Documents/myscript.scpt"]
proc.launch()

I've tried another solution, which this time uses NSAppleScript:

let myAppleScript = "tell application \"System Events\" \ntell appearance preferences to set dark mode to not dark mode \nend tell"
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
    if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error) {
        print(output.stringValue)
    } else if (error != nil) {
        print("error: \(error)")
    }
}

However this time I get an error saying:

error: Optional({
    NSAppleScriptErrorAppName = "System Events";
    NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";
    NSAppleScriptErrorMessage = "System Events got an error: Application isn\U2019t running.";
    NSAppleScriptErrorNumber = "-600";
    NSAppleScriptErrorRange = "NSRange: {86, 9}";
})

But if I change the script to something simpler, such as "display notification \"test\"", the second solution works perfectly.

This means that the second solution runs AppleScripts fine, but it can't seem to call System Events. How do I fix this?

like image 417
akmin Avatar asked Oct 02 '18 02:10

akmin


People also ask

How do I run an AppleScript login?

Question: Q: Run applescript on login ->System Preferences->Users & Groups->[Login Items] and click [+] to select. ->System Preferences->Users & Groups->[Login Items] and click [+] to select. Test the script thoroughly then save it as an application.

Does Xcode support AppleScript?

Since the AppleScript Studio frameworks from Mac OS X v. 10.5 are included with Mac OS X v. 10.6, existing Studio projects remain editable in Xcode.

How to run an AppleScript in Swift?

Before you dig into the Swift code to run the AppleScript, you need to configure the entitlements. You actually need to add two fields to it. The entitlements are contained in the file <your-app-name>.entitilements. One of type Boolean named Apple Events.

How to script an app using AppleScript using Apple events?

One of type Array named com.apple.security.temporary-exception.apple-events. Set its value to a certain number of items of type String. Their values are the ids of the applications you want to interact with using AppleScript. Each time you want to script an app, add its id in there.

What is the use of the Swift programming language in macOS?

With its human-readable syntax, it is a useful language for inter-process communication on macOS. You can, for example, open a tab on Safari, make it browse a certain URL, or even (I believe) display stuff on the Touch Bar.

Does the script pause the app on Apple TV?

Here, the script pauses Apple TV. But you can do anything else with any scriptable app as long as you declared that app in the entitlements.


1 Answers

This error occurs if the application is sandboxed.

You have two options:

  1. Add the com.apple.security.temporary-exception.apple-events entitlement for System Events
  2. Put the script in the Application Scripts folder of the application and run it from there with NSUserAppleScriptTask
like image 177
vadian Avatar answered Oct 22 '22 12:10

vadian