Created
May 5, 2022 08:16
-
-
Save erdemildiz/db6df5a1c405337d094124943f881989 to your computer and use it in GitHub Desktop.
Run applescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.jessesquires.com/blog/2018/11/17/executing-applescript-in-mac-app-on-macos-mojave/ | |
private func runScript() { | |
// Solution 1 | |
let script = """ | |
tell application "Notes" | |
tell account "iCloud" | |
make new note at folder "Notes" with properties {name:"14:53 Daily Notes", body:""} | |
set noteRunTime to get notes whose name is "14:53 Daily Notes" | |
show item 1 of noteRunTime | |
end tell | |
end tell | |
""" | |
var error: NSDictionary? | |
if let scriptObject = NSAppleScript(source: script) { | |
if let outputString = scriptObject.executeAndReturnError(&error).stringValue { | |
print(outputString) | |
} else if (error != nil) { | |
print("error: ", error!) | |
} | |
} | |
// Solution 2 | |
if let scriptPath = Bundle.main.url(forResource: "script", withExtension: "scpt") { | |
if let scriptObject = NSAppleScript(contentsOf: scriptPath, error: &error) { | |
if let outputString = scriptObject.executeAndReturnError(&error).stringValue { | |
print(outputString) | |
} else if (error != nil) { | |
print("error: ", error!) | |
} | |
} | |
print(scriptPath.absoluteString) | |
// Solution 3 | |
let task = Process() | |
task.launchPath = "/usr/bin/osascript" | |
task.arguments = [scriptPath.path] | |
task.launch() | |
let proc = Process() | |
proc.launchPath = "/usr/bin/env" | |
proc.arguments = ["/usr/bin/osascript", scriptPath.path] | |
proc.launch() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample script 1
Sample script 2