Skip to content
Mat Gessel edited this page Dec 6, 2020 · 17 revisions
← previous · · · · · · · · · · · · · · next →

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

alert

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)
}

prompt

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

confirm

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
  }
}

Choose from list

AppleScript Equivalent: choose from list { "red", "green", "blue" }

app.chooseFromList(['red', 'green', 'blue'])
//    => ["blue"]
// OR => false

With Prompt

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?' })

Multiple Selections

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 })

Displaying Notifications

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)

Set volume

AppleScript Equivalent: set volume output volume 10

app.includeStandardAdditions = true
app.setVolume(null, {outputVolume: 100})
← previous · · · · · · · · · · · · · · next →