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?
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.
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.
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.
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.
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.
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.
This error occurs if the application is sandboxed.
You have two options:
com.apple.security.temporary-exception.apple-events
entitlement for System Events
Application Scripts
folder of the application and run it from there with NSUserAppleScriptTask
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With