Documentation Archive

Developer

Mac Automation Scripting Guide

On This Page

Speaking Text

Spoken text is another way to provide feedback to users during script execution; instead of reading a message visually, the user can listen to it audibly. Listing 25-1 and Listing 25-2 show how the Standard Additions scripting addition’s say command can be used to speak a phrase.

APPLESCRIPT

Open in Script Editor

Listing 25-1AppleScript: Speaking text
  1. say "Processing is complete."

JAVASCRIPT

Open in Script Editor

Listing 25-2JavaScript: Speaking text
  1. var app = Application.currentApplication()
  2. app.includeStandardAdditions = true
  3. app.say("Processing is complete.")

The say command has a number of optional parameters, some of which allow you to specify a voice and attributes such as speaking rate, pitch, and modulation. See Listing 25-3 and Listing 25-4.

APPLESCRIPT

Open in Script Editor

Listing 25-3AppleScript: Speaking text with custom speech attributes
  1. say "Just what do you think you're doing Dave?" using "Alex" speaking rate 140 pitch 42 modulation 60

JAVASCRIPT

Open in Script Editor

Listing 25-4JavaScript: Speaking text with custom speech attributes
  1. var app = Application.currentApplication()
  2. app.includeStandardAdditions = true
  3. app.say("Just what do you think you're doing Dave?", {
  4. using: "Alex",
  5. speakingRate: 140,
  6. pitch: 42,
  7. modulation: 60
  8. })

Saving Text as an Audio File

The say command’s saving as parameter adds another level of power, enabling text to be converted to audio format and saved as an .aiff file for later listening. This technique could be used, for example, to save email messages in audio format, as demonstrated by Listing 25-5 and Listing 25-6.

APPLESCRIPT

Open in Script Editor

Listing 25-5AppleScript: Saving text as audio
  1. tell application "Mail"
  2. tell message 1 of inbox
  3. set theSubject to subject
  4. set theBody to content
  5. end tell
  6. end tell
  7. set theOutputFile to (path to desktop as string) & "message.aiff"
  8. set theAudio to "Message Subject: " & theSubject & return & "Body: " & theBody
  9. say theAudio saving to theOutputFile

JAVASCRIPT

Open in Script Editor

Listing 25-6JavaScript: Saving text as audio
  1. var Mail = Application("Mail")
  2. var app = Application.currentApplication()
  3. app.includeStandardAdditions = true
  4. message = Mail.inbox.messages[0]
  5. subject = message.subject()
  6. body = message.content()
  7. outputFile = ((app.pathTo("desktop").toString()) + "/message.aiff")
  8. audio = "Message Subject: " + subject + "\nBody: " + body
  9. app.say(audio, {savingTo: outputFile})

Speaking Text While Displaying a Dialog

Typically, a script executes a single command at a time, waiting for a command to complete before moving onto the next. Listing 25-7 and Listing 25-8 demonstrate how to display a dialog message, while simultaneously using the NSTask class in the Foundation framework to read the message out loud.

APPLESCRIPT

Open in Script Editor

Listing 25-7AppleScriptObjC: Speaking text while displaying a dialog
  1. use framework "Foundation"
  2. use scripting additions
  3. set theStatusText to "Processing is complete."
  4. set theTask to (current application's NSTask's launchedTaskWithLaunchPath:"/usr/bin/say" arguments:{theStatusText})
  5. try
  6. display dialog theStatusText
  7. theTask's terminate()
  8. on error
  9. try
  10. theTask's terminate()
  11. end try
  12. end try

JAVASCRIPT

Open in Script Editor

Listing 25-8JavaScriptObjC: Speaking text while displaying a dialog
  1. var app = Application.currentApplication()
  2. app.includeStandardAdditions = true
  3. var statusText = "Processing is complete."
  4. var task = $.NSTask.launchedTaskWithLaunchPathArguments("/usr/bin/say", [statusText])
  5. try {
  6. app.displayDialog(statusText)
  7. task.terminate
  8. }
  9. catch(error){
  10. task.terminate
  11. }