-
Notifications
You must be signed in to change notification settings - Fork 117
User Interactions
You know, alert
, prompt
and confirm
is missing. We can reimplement it in JavaScript for Automation!
Note: The alert
, prompt
, and confirm
shims are available on npm. To use it, see this recipe.
Note: iTunes cannot display system alert dialogues by default (neither can any app). Use app.includeStandardAdditions = true
. Credits: Alex Guyot
AppleScript Equivalent: display alert "wow"
app.displayAlert('wow')
// => {"buttonReturned":"OK"}
AppleScript Equivalent: display alert "wow" message "I like JavaScript"
app.displayAlert('wow', { message: 'I like JavaScript' })
// => {"buttonReturned":"OK"}
Shim:
function alert(text, informationalText) {
var options = { }
if (informationalText) options.message = informationalText
app.displayAlert(text, options)
}
AppleScript Equivalent: display dialog "What is your name?" default answer ""
app.displayDialog('What is your name?', { defaultAnswer: "" })
// => {"buttonReturned":"OK", "textReturned":"Text you entered"}
// OR !! Error on line 1: Error: User canceled.
Shim:
function prompt(text, defaultAnswer) {
var options = { defaultAnswer: defaultAnswer || '' }
try {
return app.displayDialog(text, options).textReturned
} catch (e) {
return null
}
}
Cancelling
If a user clicks the dialog Cancel button, JXA throws an Error()
with a custom errorNumber
property of -128
. The runtime has special handling for this error; unless caught, an error with code -128 will silently abort the script/workflow (no error dialog shown). This is useful in an Automator workflow where Run Javascript is followed by subsequent actions. The following can be used to emulate the dialog's User Canceled action:
var e = new Error("User canceled.") // message can be changed to anything
e.errorNumber = -128 // required - macOS error code for "User cancelled."
throw e
AppleScript Equivalent: display dialog "Delete resource forever?"
app.displayDialog('Delete resource forever?')
// => {"buttonReturned":"OK"}
// OR !! Error on line 1: Error: User canceled.
Shim:
function confirm(text) {
try {
app.displayDialog(text)
return true
} catch (e) {
return false
}
}
AppleScript Equivalent: choose from list { "red", "green", "blue" }
app.chooseFromList(['red', 'green', 'blue'])
// => ["blue"]
// OR => false
AppleScript Equivalent: choose from list { "red", "green", "blue" } with prompt "What is your favorite color?"
app.chooseFromList(['red', 'green', 'blue'], { withPrompt: 'What is your favorite color?' })
AppleScript Equivalent: choose from list { "red", "green", "blue" } with prompt "What is your favorite color?" with multiple selections allowed
app.chooseFromList(['red', 'green', 'blue'],
{ withPrompt: 'What is your favorite color?',
multipleSelectionsAllowed: true })
Use the display notification command.
app.displayNotification('The file has been converted',
{ withTitle: 'Success', subtitle: 'Done' })
Note: displayNotification
will return a privilege error unless it is run on Application.currentApplication()
(and .includeStandardAdditions
is true
)
AppleScript Equivalent: set volume output volume 10
app.includeStandardAdditions = true
app.setVolume(null, {outputVolume: 100})
- Foreword
- Conventions Used in This Cookbook
- Using JavaScript for Automation
- ES6 Features in JXA
- Getting the Application Instance
- User Interactions
- User Interaction with Files and Folders
- Using Objective-C (ObjC) with JXA
- Shell and CLI Interactions
- Importing Scripts
- iTunes
- Keynote
- Messages
- System Events
- Safari & Chrome
- Script Editor
- XML
- Examples