user3754107
user3754107

Reputation: 55

NSAppleScript opens, but does not connect to Safari?

The below function, when called in a MacOS application (Xcode version: Version 15.4 (15F31d), Sedona 14.5) opens the Safari app (if it is not running) but returns the following error:

error start:

error:  {
    NSAppleScriptErrorAppName = Safari;
    NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";
    NSAppleScriptErrorMessage = "Safari got an error: Application isn\U2019t running.";
    NSAppleScriptErrorNumber = "-600";
    NSAppleScriptErrorRange = "NSRange: {31, 8}";
}
hello

error finish:

How should I change the below code to enable my app to communicate with Safari as I can in plain AppleScript scripting?

func getHTML() -> String {
    
    let source = """
    
    tell application "Safari"
        activate
        set theURL to {URL:"https://google.com.au"}
        try 
            tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
        on error
            open location theURL
        end try
    end tell
    
    """
    
    //print(source)
    var a = "hello"
    var error: NSDictionary?
    if let scriptObject = NSAppleScript(source: source) {
        if let scriptResult = scriptObject.executeAndReturnError(&error).stringValue
        {
            a = scriptResult
            print(scriptResult)
        } else if (error != nil)  {
            print("error: ",error!)
        }
    }
    
    return a
    }


Upvotes: 0

Views: 29

Answers (0)

Related Questions