Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [213]

By Root 1624 0
"System Events"

try

set f to audio file f

on error

error "Not an audio file."

end try

set k to kind of f

tell (get contents of f)

set t to (its duration) / (its time scale)

set min to t div 60

set sec to round (t mod 60) rounding as taught in school

end tell

return k & ", " & min & ":" & sec -- MP3 Audio File, 2:43

end tell

Here's an illustration of System Events's support for parsing XML. This is very crude: we have to know the schema of the XML beforehand, and all we can do is cycle through elements and attributes looking for a desired value. (We cannot, for example, extract data through an XPath query.) In this example, I cycle down into the System Profiler report to find out what kind of computer I've got:

tell application "System Profiler"

set s to (XML text of document 1)

end tell

on findElementWithValue(e, v)

tell application "System Events"

repeat with i from 1 to (count XML elements of e)

if value of XML element i of e is v then

return i

end if

end repeat

end tell

error "not found"

end findElementWithValue

tell application "System Events"

set x to make new XML data with data s

set e to XML element 1 of XML element 1 of x

set e to first XML element of e where ¬

value of XML element 2 of it is "SPHardwareDataType"

set i to my findElementWithValue(e, "_items")

set e to XML element 1 of XML element (i + 1) of e

set i to my findElementWithValue(e, "machine_name")

return value of XML element (i + 1) of e -- iMacG5

end tell

In this particular case the XML in question is actually a property list, and we can do a much more elegant job of retrieving the desired data by treating it as such:

tell application "System Profiler"

set s to (XML text of document 1)

end tell

tell application "System Events"

set p to make new property list item with data s

set p to property list item 1 of p

set p to property list item "_items" of p

set p to property list item 1 of p

return value of property list item "machine_name" of p -- iMacG5

end tell

System Events is also responsible for GUI scripting (see Chapter 24) and for folder actions (see Chapter 27). Plus, it is System Events that actually runs menu items chosen in Apple's global Script Menu (see Chapter 2), which operates by passing the chosen script to System Events as the parameter of the do script command. System Events is thus a very important application, and its dictionary deserves study. Another example of scripting System Events appeared in Chapter 1.

SpeechRecognitionServer


This application, hidden away in the Carbon framework, is the scriptable front end for the system's built-in speech recognition functionality. For an example, see "Noises" in Chapter 21.

URL Access Scripting


This is a background-only application in the ScriptingAdditions folder. When it was invented (back in Mac OS 8.5, I believe), it was a good thing, because it provided a way to download and upload via HTTP and FTP across the Internet without the overhead a full-featured client. However, I find it undependable, and as we're now in the Unix-based world of Mac OS X, I recommend do shell script and curl instead (see Chapter 25).

Keychain Scripting


This is another background-only application in the ScriptingAdditions folder, and acts as a scriptable front end to the user's keychain, where passwords are stored. This is analogous to the functionality accessed through the Keychain Access utility. In order to access a password, however, Keychain Access itself must be launched by a different process from your script, which calls for a certain amount of extra song-and-dance (see http://docs.info.apple.com/article.html?artnum=301858):

set f to (path to application "Keychain Scripting")

tell application "Keychain Scripting" to quit

tell application "Finder" to open f

delay 5 -- that ought to do it!

tell application "Keychain Scripting"

tell (get current keychain)

set k to (get first generic key whose name is "mattneub")

get {account, description, service, password} of k

-- {"mattneub", "application password", "iTools", "teehee"}

end

Return Main Page Previous Page Next Page

®Online Book Reader