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
say "Processing is complete."
JAVASCRIPT
var app = Application.currentApplication()
app.includeStandardAdditions = true
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
say "Just what do you think you're doing Dave?" using "Alex" speaking rate 140 pitch 42 modulation 60
JAVASCRIPT
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.say("Just what do you think you're doing Dave?", {
using: "Alex",
speakingRate: 140,
pitch: 42,
modulation: 60
})
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
tell application "Mail"
tell message 1 of inbox
set theSubject to subject
set theBody to content
end tell
end tell
set theOutputFile to (path to desktop as string) & "message.aiff"
set theAudio to "Message Subject: " & theSubject & return & "Body: " & theBody
say theAudio saving to theOutputFile
JAVASCRIPT
var Mail = Application("Mail")
var app = Application.currentApplication()
app.includeStandardAdditions = true
message = Mail.inbox.messages[0]
subject = message.subject()
body = message.content()
outputFile = ((app.pathTo("desktop").toString()) + "/message.aiff")
audio = "Message Subject: " + subject + "\nBody: " + body
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
use framework "Foundation"
use scripting additions
set theStatusText to "Processing is complete."
set theTask to (current application's NSTask's launchedTaskWithLaunchPath:"/usr/bin/say" arguments:{theStatusText})
try
display dialog theStatusText
theTask's terminate()
on error
try
theTask's terminate()
end try
end try
JAVASCRIPT
var app = Application.currentApplication()
app.includeStandardAdditions = true
var statusText = "Processing is complete."
var task = $.NSTask.launchedTaskWithLaunchPathArguments("/usr/bin/say", [statusText])
try {
app.displayDialog(statusText)
task.terminate
}
catch(error){
task.terminate
}
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13