executing AppleScript-Commands from Swift-App

Hi,


Here's what i'm trying to do: I have written an AppleScript-app that looks for tickets by opening websites in Safari and analysing the displayed text of the site in Safari. That app was working nicely but i wanted to make a better GUI (instead of these "display dialog"s) and optimize the procedure a little bit.

So I started writing some Swift-code, basically consisting of some strings which contain small parts of the AppleScript, the new GUI and the following function:

func runOSA(command: String) {
    print(" > runOSA will execute: \(command)")
    var error: NSDictionary?
    if let scriptObject = NSAppleScript(source: command) {
        if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
            &error) {
            print(output.stringValue ?? "")
        } else if (error != nil) {
            print(" > error during osa_execution: \(String(describing: error))")
        }
    }
}



When I run the app (by clicking that "play"-button in XCode), it will only execute AppleScript-commands like beep or say.

That's the output I get in XCode:

legacyEngine: run() loop started // custom debug output "print()"

it's 2017-12-20 17:02:58 +0000 - waiting for 1sec ... // custom debug output "print()"

quit waiting at 2017-12-20 17:03:00 +0000 // custom debug output "print()"

> runOSA will execute: say "TicketTracker wird in 5 Sekunden seine Daten aktualisieren. Pfoten weg von Maus und Tastatur!"

it's 2017-12-20 17:03:07 +0000 - waiting for 5sec ...

quit waiting at 2017-12-20 17:03:13 +0000

> runOSA will execute: tell application "Safari" to activate

2017-12-20 18:03:13.638977+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.639086+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.682920+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.683020+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.686263+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.686330+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.687983+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.688032+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.689415+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.689462+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.690817+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.690886+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.698280+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.698341+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.770656+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.770729+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.772298+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.772357+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.773560+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.773630+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.775430+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.775502+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.782727+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.782800+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.929275+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.929388+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.932271+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.932366+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.934802+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.934887+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.937242+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.937347+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.946029+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:13.946100+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.234524+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.234632+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.237336+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.237427+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.239742+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.239823+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.242112+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.242199+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.254469+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.254581+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.833985+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.834086+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.836818+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.836903+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.839535+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.839597+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.841487+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.841552+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.844061+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:14.844134+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.007904+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.008001+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.010467+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.010558+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.012599+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.012691+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.014452+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.014512+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.016455+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:16.016506+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.334231+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.334363+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.337500+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.337592+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.339980+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.340070+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.343179+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.343269+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.347120+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.347223+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.350081+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:18.350211+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:20.368597+0100 TicketTracker[732:14520] Month 13 is out of bounds

2017-12-20 18:03:20.368767+0100 TicketTracker[732:14520] Month 13 is out of bounds

> error during osa_execution: Optional({

NSAppleScriptErrorAppName = Safari;

NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";

NSAppleScriptErrorMessage = "Safari got an error: Application isn\U2019t running.";

NSAppleScriptErrorNumber = "-600";

NSAppleScriptErrorRange = "NSRange: {29, 8}";

})

it's 2017-12-20 17:03:20 +0000 - waiting for 1sec ...

quit waiting at 2017-12-20 17:03:22 +0000

> runOSA will execute: tell application "System Events" to keystroke "l" using command down

> error during osa_execution: Optional({

NSAppleScriptErrorAppName = "System Events";

NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";

NSAppleScriptErrorMessage = "System Events got an error: Application isn\U2019t running.";

NSAppleScriptErrorNumber = "-600";

NSAppleScriptErrorRange = "NSRange: {36, 32}";

})

Can anyone tell me how to fix this or show me a way to execute AppleScript-commands more efficiently?