Sample Header Ad - 728x90

Run an AppleScript from Swift

0 votes
0 answers
1746 views
I'm trying to return a value from AppleScript to Swift (in XCode). If I do this simple code it works: let myAppleScript = """ return "hello world" """ var error: NSDictionary? if let scriptObject = NSAppleScript(source: myAppleScript) { let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error) print(output.stringValue) } and if I run display dialog I get the alert on the screen, though there are issues with dismissing it. But if I run this code: let myAppleScript = """ set appList to my getAppList() return appList on getAppList() tell application "System Events" return name of every application process as list end tell end getAppList """ var error: NSDictionary? if let scriptObject = NSAppleScript(source: myAppleScript) { let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error) print(output.stringValue) } It returns nil. I am trying to port an AppleScript I wrote to XCode to make it more dynamic and give it a UI. Am I doing something wrong or is this just not possible?
Asked by PruitIgoe (101 rep)
Dec 7, 2021, 03:04 PM