5.7. Saving FilesThe 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.
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.
5.7.1. An Example: Saving in TextEditAs 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 BoxOf 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.
--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
5.7.3. Saving All Documents at OnceThere'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. |