I'm trying to run an AppleScript from my macOS app (Mojave, Xcode 10.3). However, when the AppleScript is run from the app, I get this error:
/Users/my_username/Desktop/test_apple_script.scpt: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
Based on other posts, I have made sure that my app can control my computer (System Preferences > Security & Privacy > Privacy > Accessibility), and it also has control over System Events (System Preferences > Security & Privacy > Privacy > Automation).
Here is my AppleScript:
tell application "System Events"
key code 48 using {command down}
end tell
The script works when run in Script Editor (I only had to give Script Editor the access in System Preferences > Security & Privacy > Privacy > Accessibility).
This is the code I am using to run the Apple Script from my app (via osascript):
let proc = Process()
proc.launchPath = "/usr/bin/env"
proc.arguments = ["/usr/bin/osascript","/Users/my_username/Desktop/test_apple_script.scpt"]
proc.launch()
This code works perfectly when I run a simple AppleScript that just displays a notification:
tell application "System Events"
display dialog "Test"
end tell
How can I make it so that my AppleScript runs from my macOS app just as it does when I run it in Script Editor?
Edit: For some more context, the command tab case here is just an example. I am trying to find a generalizable approach to simulating keyboard shortcuts by clicking buttons in my app (e.g., the user clicks on the "cut" button and the app acts as if the user has just pressed command + x). There may be a better approach to this than using an AppleScript, but I know that it is possible to do what I want using AppleScripts.
Edit: In case it wasn't clear from my original question, the simple AppleScript that displays a notification was already working from within my app. The problem arises when I try to run an AppleScript that simulates user input from the keyboard.
Edit: This code here worked for me temporarily, but it no longer works. I did not change the code at all. I simply ran my app again.
let PlayPauseScript = "tell application \"Spotify\"\n playpause\n end tell"\"test\"\n end tell"
if let scriptObject = NSAppleScript(source: PlayPauseScript) {
scriptObject.executeAndReturnError(nil)
}
Edit:
Here is where the problem currently stands. Based on suggestions, I added this to my info.plist, and now the Spotify AppleScript consistently works (when called via NSAppleScript):
<key>NSAppleEventsUsageDescription</key>
<string>...this is why I need permission...</string>
It seems my problem (now) is just with AppleScripts that use System Events. Despite making sure that both my app and osascript are allowed to control my computer (via security & privacy > privacy > accessibility), I now get the same error (1002) when using NEAppleScript and osascript. My app is also allowed to control System Events (security & privacy > privacy > automation). The osascript error prints to the console in Xcode, and I uncovered the error code for NEAppleScript by using this code (in the @IBAction of the button):
let script =
"""
try
tell application "System Events"
keystroke "c" using {command down}
end tell
on error errMsg number errorNumber
display dialog "An unknown error occurred: " & errorNumber as text
end try
"""
if let scriptObject = NSAppleScript(source: script) {
scriptObject.executeAndReturnError(nil)
}
The above code caused this to appear in the dialog box:
An unknown error occurred: 1002
When my app is run, it does prompt me to give it control over System Events, which I always do. My app is not sandboxed. I believe that both NSAppleScript and oascript did work for me once, but stopped working when I ran the app again (despite my not having made any changes to my code, I'm almost positive).
I don't know if this is informative, but the only other clue I have as to what might be going on is that I keep having to check and uncheck Script Editor's permission in security & privacy > privacy > accessibility to get it to run these AppleScripts because it tells me that Script Editor is not allowed to send keystrokes.
Security & Privacy -> Privacy tab -> Accessibility
Add /usr/bin/osascript
NOTE: You may also need to add your terminal (iTerm in my case)
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