AppleScript_ The Definitive Guide - Matt Neuburg [214]
end tell
When I run that example, the Confirm Access to Keychain dialog appears twice (once for the Keychain Scripting application and once for the host application in which the script is running), because this is a password whose access control is set to require confirmation. Thus, this script could not run successfully without human intervention, which is a good thing. As a further security measure, you cannot access the access control rules for a key through Keychain Scripting.
Image Events
This is another background-only application living in the CoreServices folder along with the Finder and System Events. It is a front end to sips (type sips —help in the Terminal for more information), which performs some basic manipulations on image files, such as scaling, rotating, and manipulating color profiles, as well as supplying information about monitors. For example:
tell application "Image Events"
set f to (path to desktop as string) & "bigImage.tiff"
set f2 to POSIX path of (path to desktop) & "/smallerImage.tiff"
set im to open file f
scale im by factor 0.5
save im in f2
end tell
Database Events
Database Events, yet another background-only application located in CoreServices, is for manipulating "databases." Here we create and populate a tiny database:
property fieldnames : {"firstname", "lastname", "book"}
property theData : {{"Matt", "Neuburg", "AppleScript"}, ¬
{"Charles", "Dickens", "Pickwick"}, ¬
{"William", "Shakespeare", "Hamlet"}}
tell application "Database Events"
set d to (make new database with properties {name:"people"})
tell d
repeat with i from 1 to (count theData)
set r to (make new record with properties {name:""})
repeat with j from 1 to (count fieldnames)
tell r to make new field with properties ¬
{name:item j of fieldnames, value:item j of item i of theData}
end repeat
end repeat
save
end tell
delete every database -- actually, just closes the database
end tell
Observe that we do not try to tell Database Events where to save the database; this is because every database, rather annoyingly, is automatically saved in and retrieved from your ~/Documents/Databases folder. Now we'll query the database:
tell application "Database Events"
tell database "~/Documents/Databases/people.dbev"
get value of field "book" of ¬
(every record where value of field "firstname" is "Matt")
-- "AppleScript"
end tell
end tell
The syntax illustrated here is extraordinarily clumsy and touchy, even for Apple, and the object model's approach to databases is unhelpful and nonstandard; you cannot execute an SQL command, nor does Database Events understand that a true database consists of tables (so you could not use it to make a relational database, and even the claim that it makes "databases" seems dubious). Furthermore, files created by Database Events cannot be opened by SQLite (the excellent lightweight open-source database engine included with Mac OS X starting in Tiger; see http://www.sqlite.org), which makes it hard to credit Apple's boast that Tiger includes a "Data Events suite" that "lets you create and parse SQLite databases." In sum, Database Events is nowhere near as powerful, coherent, and complete as the sqlite3 command-line tool. So it's best to disregard Database Events as a bad job, and use the command line instead. Here we create and populate the database:
set d to space & "~/Desktop/people.db" & space
set s to "sqlite3" & d & quote
set s to s & "create table people(firstname, lastname, book); "
set s to s & "insert into people values('matt','neuburg','AppleScript'); "
set s to s & "insert into people values('charles','dickens','Pickwick'); "
set s to s & "insert into people values('william', 'shakespeare', 'Hamlet');"
set s to s & quote
do shell script s
And here we query it:
set d to space & "~/Desktop/people.db" & space
set s to "sqlite3 -list" & d & quote
set s to s & "select book from people where firstname = 'matt';"
set s to s & quote
do shell script s -- "AppleScript"
Chapter 24. Unscriptable Applications
Some applications