Elaborations on "AppleScripting a Running Start"
This article is published in MacToday's November/December 1998 issue, slimmed-down because of space limitations. Here we elaborate on the article, answering questions or clearing up points rendered obscure by meeting our all-to-small printed word count.
>> Elaboration: Dang it. I have a habit of using lower case, even though my ISP's server is case sensitive. As you've noticed, the URL is really <http://homepage.mac.com/rhowehmd/Inreach/MacToday/> . I'll probably put in a mirror page, or at least a make a page pointing to the correct one. Sorry about that. <<
Background #1: I like Apple's auto-power-on Feature. (Can't find it on your Mac? See our elaboration page.) I like my Mac running when I arrive at work. I like having items in the Startup Items folder so my often-used items are ready when I am.
>> Elaboration: Most Macintosh models these days support auto-power-on. The Macintosh IIsi was the first with hardware support, even though the software didn't appear until a few years later. Some early PowerPC models, like the 6100, only turn off by a switch; I belive some Performa models do too. Check your documentation to see if your Mac supports this feature&emdash;or just try one of the various control panels that activate the auto power on feature.; it'll tell you if it will work on your model. Different versions of the OS have different 'auto-power-on' control panels, which if memory serves me, began along around System 7.1. The "Auto Power On/Off" panel simply let you specify the startup and shutdown times. The Energy Saver (currently shipping) lets you specify screen and disk sleep times as well as startup and shutdown times.
>> If you can turn your Mac on and off by using the power-on button on the keyboard without having to hit another switch&emdash;or if you don't get the message "It is now safe to switch off your Macintosh" when you shut down&emdash;then your Mac probably supports the Auto Power On/Off feature. And if you are running a version of System 7.5 or later, either the control panel is installed or you can reinstall it from your System Disks.<<
Background #2 To maximize a Mac's memory use, follow this rule: open your most-used application first, followed by the second-most-used application, and so on and on. When you need to free up memory, quit the application(s) you use the least.
>> Elaboration: The Macintosh OS doles out memory first-come, first served. Think of memory as a seat-yourself banquet. Everyone lines up at the buffet table piling up food on plates. I see the Jones family is ready to sit down. Look, all five Jones are sitting together at the first bench. Now the newlyweds Smith (John and Jane) sit next to the Jones, then the Mark family (three people) and the James (three more) fill up the rest of the table. If the Smiths and the James finish first, five seats open up, but that doesn't mean the Rose family (five people) can sit together since the Smiths are sitting between the open seats.
>> Applications are like these familes that need to sit together. To have the best chance keeping the biggest amount of space always available, seat first those families that take the longest to eat. <<
The Problem: Items in the "startup items" folder open in this order--first all applications (in alphabetic order), then all documents (in alpha order), and finally all aliases (yup, in alpha order). Try as you might, you won't get an alias to launch before an application...which is a problem if an alias belongs to your most-used application.
[Laura: Why not put only aliases into the startup items folder, alphabetized to force them to start up in a particular order?
Robert: Shhhh! If we say that, we'd have no article this issue!]
>> Elaboration: Dick, MacToday's Editor, jumped on this right away. He wanted to know if this was just a 'busy work' article instead of something really useful. "Couldn't find something useful to write about, eh?" is what he implied, and almost kicked us off the MacToday contributing staff.
>> But there is a value to this article, as I pointed out to Dick. It shows how you can identify a folder, and find the names of the files/folders in that folder. It shows how you can step through the contents of the folder and do something with them. It just so happens that, again because of space limitations, we can't do much more than just open the files. And the most obvious time to open files is when you start up your Macintosh. In a future article we can build on this base to do something more useful. At least we've started! <<
Robert's solution: Let's create an AppleScript to start up items in the order you specify.
In Step 1: We'll create our own startup items folder.
In Step 2: We'll create an AppleScript to open up the items (in alpha order) in our own startup items folder.
In Step 3: We'll put the new AppleScript in Apple's Startup Items folder.
Ready? Let's begin.
Step 1: Create a new folder in your system folder. Call it "My startup items". Next, plop whatever you want into your new "my startup items" folder--applications, documents, aliases. Anything! Rename the items so when they're alphabetized, they'll be in the order you want them to start.
>> Elaboration: no one asked, but I like to begin the files with a two-digit number. So I have items like "01 My 3270 connection", "02 Stickies", "03 MacAClock", and so on. I use leading zeros because if you don't, "10" comes after "1" and before "2". <<
Step 2: Create the AppleScript. When we're done, it will:
(Step 2a) find the folder you created in step 1, (Step 2b) get a list of what's in that folder, and (Step 2c) open each item in turn, which, since you've alphabetized them in Step 1, will be in alpha order.
Start by opening the script editor.
(2a) How do we find the folder and get a list of what's in it? We use the "File Commands" scripting addition which should be in your "Scripting Additions" folder in either your System or Preferences folder. (If not, then go to our elaborations page.)
>> Elaboration: Oh dear. If the File Commands Scripting addition is not in your system, you'll probably need to reinstall AppleScript. That'd be the safe thing to do. If you're in a hurry, you could copy the File Commands Scripting Addition from a different Macintosh, putting it into your Scripting Additions Folder. You might have to restart before AppleScript will recognize it though, and by that time you might as well just reinstall AppleScript. <<
The File Commands adds a "path to" command to AppleScript, which finds the path to specific Finder folders; one of which being the system folder. If you type "path to system folder" (without the quotes) into the script editor by itself, then run it, you'll get a result similar to what I get:
alias "Carry-All Disk:System Folder:"
Trouble is, we don't want that 'alias' word in front. Especially when we need to tack our private startup items folder's name to the end of "Carry-All Disk:System Folder:". So what do we do? We force AppleScript to give us a result as a string. Do this by typing
"path to system folder as string"
You'll now get something similar to:
"Carry-All Disk:System Folder:"
We're almost there! We know where the system folder is, but we want our special folder. If we type
(path to system folder as string) & "My Startup Items"
we'll get:
"Carry-All Disk:System Folder:My Startup Items"
Perfect! Just what we want.
To save a lot of typing, we'll copy that result to a variable we'll use for the rest of the script. Our first line of the AppleScript will be
copy (path to system folder as string) & "My Startup Items" to MySpecialFolder
(2b) Now we have a path to our private startup items folder. We'll use another command of the 'File Commands' scripting addition--the 'list folder' command--to get what's in the folder. Type this as your second line in AppleScript:
copy (list folder MySpecialFolder) to MyStartupOrder
If you run it, you'll get a result like this:
{"first alias", "second application", "third document"}
In AppleScript, that's a list. And you can work with lists by using a loop to get each item in turn. That's what we'll now do.
(2c) Type the following lines into your AppleScript:
repeat with LoopCount from 1 to number of items in MyStartupOrder tell application "Finder" try open file (MySpecialFolder & ":" & (item LoopCount of MyStartupOrder)) on error beep LoopCount end try end tell end repeat
Now I know you're excited, but I have no room to explain what each of these lines is doing. For that you'll have to go to our elaborations page.
>> Elaboration: here's a line-by-line description of what's going on:
>> First Line:
>> repeat with LoopCount from 1 to number of items in MyStartupOrder
>> This defines a variable, cleverly called "LoopCount", which starts at "1" and ends with the number of items we have put into our special startup-up folder. LoopCount gets bigger each time we repeat the the rest of the statements. Thus, we can do something to each and every item in our startup folder.
>> Second Line:
>> tell application "Finder"
>> Applescript is directing the statements that follow to the Finder. Actually, the entire AppleScript is directed to the Finder, so we could probably move this line outside the repeat loop, or even do away with it entirely. I left it in, sacrificing efficiency for clarity.
>> Third Line:
>> try
>> Ah, one of my favorites. "Try" is great--if something can't be done, you won't get an annoying error message; well, you can make your own special message in the 'on error' section coming up. We could open the file without the 'try'. I figure this isn't so critical that it's worth stopping the entire process. Just open as many of the files I want as you can; I'll deal with the ones that didn't open later.
>> Fourth Line:
>> open file (MySpecialFolder & ":" & (item LoopCount of MyStartupOrder))
>> Here we actually open the file. We have to build the file name though. Remember our variable "MyStartupOrder"? It has a list of the files in our special startup folder. The only problem is that Applescript requires the full path name to the file. Lucky for us, the path to the folder is in our variable "MySpecialFolder". We add the ":" because, unlike the scripting addition that tacked the colon on the end of the path to the system folder, we hadn't yet added it to the end of the folder MySpecialFolder.
>> Fifth line:
>> on error
>> Here's our chance to catch problems, notify us, or whatever if something goes wrong in the lines following the 'try' statement but before this statement. Because we lack space in MacToday, I use the 'beep on error' approach--it's fast, it's easy. It's not terribly helpful when something goes wrong though. You can put up a dialog with the error number, or do pretty much anything Applescript can do. In a future article we'll discuss error handling.
>> Sixth line
>> beep LoopCount
>> If there's an error, we just beep. To help us identify what the problem is, we'll beep the "loopCount" times--that is, if the third item we tried to open had a problem, the Mac will beep three times. Like I said, it's a simplistic approach to errors.
>> Seventh line
>> end try
>> This tells the Finder (in our 'tell' statement) that that's all we wanted it to try to do. Everything else (before and after the 'end try') is a 'must do'.
>> Eighth line
>> end tell
>> That's it for what we are telling the Finder. But as I mentioned in the second line's description, we could improve this script's efficiency by moving the 'tell' and 'end tell' to outside the repeat loop.
>> Ninth and last line
>> end repeat
>> This is all we wanted the Applescript to repeat. When it has stepped through all the items in our special startup items folder, it'll finish. <<
Run the script to make sure it works, then choose the file menu command to "Save as Run-Only..." with 'never show startup screen' checked. The final script should look similar to Figure 1. See our elaborations page if you have problems.
>> Elaboration: (You noticed the lowercase "mactoday" in the description, didn't you. Where were you when I sent this off to the editor?! )What problem did you have? Write to us and we'll help track down the problem. It'll help if you send us a copy of the script you're having problems with.<<
(3) Put the AppleScript you created and saved in Step 2c into the System Folder's Startup items folder.
That's it! Restart, and watch each item in your private startup items folder launch in the order you want them to.
If you had any problems, check our elaborations page. And if that didn't help, then write to rhowehmd@inreach.com.
>> Elaboration: Please write with any problems you have with this script. If (when) reports come in, we'll update this page to help anyone else with similar problems. Thanks!
PS: need something automated? Drop Laura or me a line. If we can, we'll help out, and maybe even get you featured in an upcoming MacToday article.
Last updated: 11/02/98