My end objective is as stated in the Title.
Maybe this can all be done using ASObjC – that would be great.
I have been trying to integrate two scripts:
- Swift script that returns list of app paths – written by a colleague.
- My AppleScript to get Bundle ID given the app path
I’m doing this using Keyboard Maestro, which I know most of you don’t use.
But taking the output of the Swift script into my AppleScript is working.
I’d like to just run the Swift script from my AppleScript, but it appears it is harder than running a normal shell script.
Here’s what I have:
Part 1 – Run Swift Script in AppleScript
==UPDATED==: 2020-06-26 01:55 GMT-5
- replace Swift script with correct script
use AppleScript version "2.5"
use scripting additions
use framework "Foundation" -- Required for all ASObjC commands
use framework "AppKit"
set swiftCmd to "#!/usr/bin/swift
import Foundation
let theURLStrg = \"http://\"
let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)
let appIdentifierKey = \"CFBundleIdentifier\" // App ID
guard let allHandlers = LSCopyApplicationURLsForURL(theURL!, .viewer) else {
fatalError(\"No handlers for '\\(theURLStrg)' found.\")
}
let appURLs = (Unmanaged
.fromOpaque(allHandlers.toOpaque())
.takeUnretainedValue() as NSArray)
.componentsJoined(by: \"\\n\")
print(appURLs)
"
set appListStr to do shell script swiftCmd
That immediately fails with this error:
sh: line 1: import: command not found
sh: line 3: let: =: syntax error: operand expected (error token is "=")
sh: -c: line 4: syntax error near unexpected token `('
sh: -c: line 4: `let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)'
I don’t have a clue about resolving this.
Can anyone help?
I would like to learn two things:
- How to do the entire task in ASObjC
- How to Run a Swift script in AppleScript
TIA.
In case anyone needs it, here is the Swift script un-escapted:
Swift Script
import Foundation
let theURLStrg = "http://"
let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)
let appIdentifierKey = "CFBundleIdentifier" // App ID
guard let allHandlers = LSCopyApplicationURLsForURL(theURL!, .viewer) else {
fatalError("No handlers for '\(theURLStrg)' found.")
}
let appURLs = (Unmanaged
.fromOpaque(allHandlers.toOpaque())
.takeUnretainedValue() as NSArray)
.componentsJoined(by: "\n")
print(appURLs)