5.7. Saving Files
The last piece of the Mac OS X file puzzle is saving documents you
already have open. AppleScript makes this simple: the
save command works the same way as choosing
File
Save.
 |
Similarly, the save as command works the
same way as choosing File
Save As (that is, when you provide a file path to the save
as command, Mac OS X saves a copy of
your current file in the location you specify).
|
|
Unfortunately, this trick works only in certain programs (TextEdit,
Microsoft Word, and Safari, for example). To see if a particular
program supports AppleScript-based saves, open the
program's dictionary and see if the
dictionary's Standard Suite (Section 3.2.3) includes
the save command.
Power Users' Clinic You Can't Judge a File by its Extension | Back in the days of Mac OS 9, before you had to put file extensions
at the end of documents' names, your Mac knew what
kind of files you had by their type and
creator codes. The type
code told your Mac what kind of information was
stored in a file. For example, if a file's type code
was "TEXT," that meant the file was
just plain text, while a type code of
"APPL" meant the file was an
application. The creator code, on the other hand, told your
Mac which program produced a file. A file created with Photoshop
would use the creator code "8BIM,"
while one created with AppleWorks would use
"BOBO"go
figure. The importance of type and creator codes in Mac OS X is reduced, but
they're still around. In fact, if a file has a type
and creator code, they override any settings for
which program should open the file. That's why the
help files that come with Photoshop won't open in
your default Web browser: Adobe has set their creator code to
"MSIE," so they'll
always open in Internet Explorer. Thankfully, AppleScript lets you modify type and creator
codesor get rid of them completely. The following script can
help you do it: 
set selectedFile to (choose file)
(* Get the file's current type and creator codes: *)
tell application "Finder"
set fType to the file type of ¬
selectedFile
set cType to the creator type of ¬
selectedFile
end tell
(* Get the new type and creator codes you want to use: *)
set newF to text returned of ¬
(display dialog "Current type code: " ¬
& fType & ". New:" default answer "")
set newC to text returned of ¬
(display dialog "Current creator ¬
code: " & cType & ". New:" default ¬
answer "")
--Set the new type and creator codes:
tell application "Finder"
set the file type of ¬
selectedFile to newF
set the creator type of ¬
selectedFile to newC
end tell
If you'd like to banish the type and creator code
from a fileso that Mac OS X will judge the file by its
extensionenter "????" for
both codes (or, if you want the details of erasing such codes, see
http://daringfireball.net/2004/02/setting_empty_file_and_creator_types).
If you'd rather just replace the existing codes with
new ones, check out http://kb.indiana.edu/data/aemh.html for a
list of type and creator codes for different programs and files. |
5.7.1. An Example: Saving in TextEdit
As the epitome of Mac-ness, TextEdit does support
the save command. Thus,
if you needed a script to save your current TextEdit document, this
one would work
well:
tell application "TextEdit"
activate
save the front document
end tell
If you haven't saved your current TextEdit document
yet, a Save dialog box appears. (The Save dialog box, as you know,
lets you specify a name for the document and where you want it
stored.)
5.7.2. Forgoing the Dialog Box
Of course, you could always save your files
without using AppleScript. The real benefit of
scripting Save operations, though, is that you can completely bypass
the Save dialog box, saving you several precious seconds:
tell application "TextEdit"
activate
save the front document in ":Users:
yourUsername
:Desktop:kiwi.rtf"
--Remember to replace
yourUsername
with your actual one-word username
end tell
The in option lets you specify the
colon-separated path of the file into which you want to save your
document (in this example, the kiwi.rtf file on
your desktop). If there's a particular file you
often savesay, your personal home pageyou might find it
useful to run the previous script whenever you want to save a
new version of the document in the
old location.
 |
Make sure you specify a file, not a
folder! If you put a path to a folder after the
in option, AppleScript overwrites that folder
completely. And if that folder were your desktop, the script would
instantly trash your Desktop folder and every file
inside it, leaving your desktop files as mere
memories. Here's what to avoid:
|
|
--DO NOT RUN THIS SCRIPT!!
tell application "TextEdit"
activate
--Wanna erase your Desktop? Here's how:
save the front document in ¬
"Macintosh HD:Users:yourUsername:Desktop:"
(* Since you specify the path to your Desktop, Mac OS X overwrites the
Desktop...permanently! *)
end tell
 |
Instead, specify the actual file you want to
save the document into. (You can tell you're
specifying a file because it won't end in a colon.)
|
|
5.7.3. Saving All Documents at Once
There's one more timesaving
trick to the
save command: saving all your documents in
one step. This can come in handy for those times when you have
multiple files open and want to quickly save all of
themwithout switching to each document window individually and
hitting -S. Use this script to get the job
done:
tell application "TextEdit"
activate
save every document
end tell
When you run this script, TextEdit does the same thing it would do if
you chose File
Save All.
The nice thing about this script, though, is that it works in many
programs that don't even have a
Save All command, such as Microsoft Word, Safari, and even Script
Editor itself. Simply change the tell statement
to reflect the program you want to command, and run the script again.
For example, here's how that script would look if
you wanted to use it with Word:
tell application "Microsoft Word"
activate
save every document
end tell
Now that you know how to open, move, copy, and save documents
automatically, you can call yourself a true AppleScript filephile.
|