Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Created May 5, 2022 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erdemildiz/db6df5a1c405337d094124943f881989 to your computer and use it in GitHub Desktop.
Save erdemildiz/db6df5a1c405337d094124943f881989 to your computer and use it in GitHub Desktop.
Run applescript
// 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()
}
}
@erdemildiz
Copy link
Author

Sample script 1

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

Sample script 2

tell application "Notes"
	tell account "iCloud"
		show item 1
	end tell
end tell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment