How to script: restart, run utility, restart


I’d like to write a script for OS X 10.3.9 that does the following:

While running from my drive’s main partition (“boot backup 80”):

  1. reboot into an alternate partition (“storage 200”)
  2. launch disk utility while running on storage 200
  3. (in disk utility) run: “repair disk” on the main partition boot backup 80
  4. (in disk utility) run: “repair permissions” on the main partition boot backup 80
  5. reboot back into the main partition, storage 200
  6. launch a utility called yasu

Is this possible to do, and if so, how can it be done?

I’ve seen the post but it doesn’t say anything about running something on the other side of the reboot; nor, more importantly, whether it’s even possible for the system to remember it’s running a script, which seems to me to be the big question.

Any help would be appreciated!

Model: PowerMac G5 desktop
AppleScript: 1.9.3
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)

The way you would have to do this is to have the script write a value to a text field and then create another script that is a startup item on the other volume. When it starts it checks that value written and if set performs the actions requested… all of which btw can be done wiht shell commands. then it would reset the value back.

Thanks, James, I’ll give it a try.

Well, I seem to have hit a snag on the very first step: namely, opening, with a script, a file in which I’ve stored a parameter. When I run the following script, I get an error message that says: “TextEdit Open Failed/Couldn’t open file parameterfile.rtf”. In the following, “boot backup 80” is the startup partition, “scripting restarts” is a folder on the Desktop, and “parameterfile.rtf” is a file containing a single digit, “1”. Any suggestions as to what I could be doing wrong? I restarted from another partition and repaired the disk and permissions, but that seemed to have no effect.

tell application "TextEdit"
	open file "boot backup 80:Desktop:scripting restarts:parameterfile.rtf"
end tell

well rather then using a application I would take advantage of AppleScript’s built in capablities of reading/writing text documents.

So for example say you have a plain text document called ‘file.txt’ on your desktop. To read that file’s data without using another application you could do this…

set deskPath to path to desktop as Unicode text
set filePath to deskPath & "file.txt"

set fileData to read file filePath

Hey, that works very nicely. Thanks, man, I appreciate it. Say, I’ve searched the Applescript Language Guide (the one that can be downloaded from Apple), and haven’t found anything in there that deals with reading and writing files. Where can I learn some of the details of that capability?

James Coronato.

Can anyone tell me how to script System Preferences? I’ve tried a number of things without success (two are included below the “Activate” command in the script below. But get error messages with all of them.

-- Have a file on the desktop named "file.txt". Won't work with (can't interpret the contents of) an .rtf file, so use .txt only.
set deskPath to path to desktop as Unicode text
set filePath to deskPath & "file.txt"

set fileData to read file filePath

if fileData = "1" then
	display dialog "OK! The following parameter has been set:" & fileData
	display dialog "Not sure what to do with: " & fileData
end if

tell application "System Preferences"
	-- this doesn't work:
	set current pane to pane "System Preferences"
--and neither does this:
	tell menu "view"
		select "Startup Disk"
	end tell
end tell

Well what are you trying to do insie System Preferences? A bunch of that stuff can be manipulated straight through defaults commands or there may be a better way entirely. For a better look at actually scripting System Preferences though take a look at this article, by Kai Edwards.

As for Read/Write those are File Read/Write Suite as defined in StandardAdditions.osax. From Script Editor go to open Dictionary and selecte Standard Additions from the dialog box and then search for read / write.

Hope that helps!

James, that’s a tremendous help! And what especially interests me about what you said concerns the possiblity that ways simpler than what I’m attempting may exist to get the job done. My intention in scripting System Preferences was to set the startup disk to my alternate partition (storage 200) and then reboot”I knew that there should be scriptable commands for doing that. Another routine (as you suggested) on the storage 200 partition would launch at startup, check the parameter in file “file.txt” (on the primary partition), and then launch Disk Utility to repair disk and permissions on the primary partition. Then I’d change the startup disk preferences again and reboot into the primary partition and run a maintenance routine on it.

So if there is a way to boot from one or the other without going into system preferences and without needing any user interaction through the keyboard, I’d really like to know about it. Likewise with the repair routines; if I didn’t have to launch Disk Utility and script it, it might wind up being simpler all around.

Many thanks,

Hi James, To take care of repairing permssions and the disk you need to use the command ‘diskutil’. So for example something like these commands would work for your current booted volume…

set AdminName to "the admins short name"
set pword to "the admin's password"

do shell script "/usr/sbin/diskutil repairPermissions /" user name AdminName password pword with administrator privileges
do shell script "/usr/sbin/diskutil repairvolume /" user name AdminName password pword with administrator privileges

As for setting the boot volume I’m 99.9% positive this can still be done with the shell command ‘bless’, but i haven’t done it in a long time. If I get a chance though I will try it when I’m in the office tomorrow.


Thank you, James, the Unix commands might be just the ticket, since the scripting ideas in Kai Edwards’s article don’t seem to be working for me.

First, I tried this script from Kai Edwards’s article to toggle the dock’s minimization routine:

quit application "System Preferences"
tell application "System Events" to tell property list file ("boot backup 80:Users:jac:Library:Preferences:" & "")'s property list item "mineffect"
    if value is "Genie" then
        set value to "scale"
        set value to "Genie"
    end if
end tell
 quit application "Dock"

It won’t compile. And then this script, also from Kai Edwards’s article:

tell application "System Events" to set min_effect to value of property list item "mineffect" of property list file ("boot backup 80:Users:jac:Library:Preferences:" & "")

Now, I don’t actually need to toggle my dock’s properties; I copied these scripts directly out of the article just to see if I could find a piece of code that worked on my machine. But neither of these has worked. An applescript error results every time from the word “property” in the phrase, “value of property list item”, and it goes no further. Furthermore, no key words “mineffect”, “Genie”, or “scale” even exist in my dock.plist file, which apparently has a structure different from the one intended to be operated on by this script.

Next, I take a different tack and change the startup disk manually using the System Preferences application. At the moment I do, the Date Modified property of files and changes immediately to the current date and time. But when I compare two versions of the systempreferences.plist file (one in which startup disk is set for the primary partition, and one in which the startup disk is set as the secondary partition) TextWrangler says that both files are identical, as indeed they appear upon inspection to be. So what gets modified when I change the startup disk preference? If I could find that, I might be able to do something constructive.

Finally, I try GUI scripting, since I have been unable to script System Events to do it, and run the following script, also from Kai Edwards’s article, to ensure that GUI scripting is enabled:

to |enable GUI scripting|()
	if (system attribute "sysv") < 4138 then display dialog "This script requires the installation of Mac OS X 10.3 or higher." buttons {"Cancel"} default button 1 with icon 2
	tell application "System Events" to if not UI elements enabled then
		tell me to display dialog "This script requires the built-in Graphic User Interface Scripting architecture of Mac OS X, which is currently disabled." & return & return & "Enable GUI Scripting now? (You may be asked to enter your password.)" buttons {"Cancel", "Enable"} default button 2 with icon 2
		set UI elements enabled to true
		if not UI elements enabled then error number -128
	end if
end |enable GUI scripting|

|enable GUI scripting|()

But here again, it fails. When I run this script, the second dialog in the above script is displayed on my screen, which asks whether to “Enable GUI Scripting now?” I say Yes and click the “Enable” button, only to get an error message that says: System Events got an error: NSInternalScriptError. The only thing I haven’t tried yet is a shell script, about which I know little, so I appreciate the commands you’ve sent along in your latest post above.