The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   System (http://hintsforums.macworld.com/forumdisplay.php?f=4)
-   -   AppleScript to restart OS X in OS 9 (http://hintsforums.macworld.com/showthread.php?t=1360)

JoelNelson 03-03-2002 09:37 AM

AppleScript to restart OS X in OS 9
 
I read on the Mac OS X Hints site that there is a way to AppleScript OS 9 to restart in OS X (http://www.macosxhints.com/article.p...10416184548354).
Is there a way to write a script for OS X to restart in OS 9?

:confused:

rusto 03-03-2002 10:04 AM

Yea, see this article:

http://www.macosxhints.com/article.p...20302215826526

meancode 03-03-2002 10:06 AM

yes, someone just posted it at the macosxhints site. you can do the do shell script applescript, and if you do not want to use sudo, wich will prompt for an admin password, you can use this:
Code:

password "YourPWHere" with administrator privileges
you can then save it as a run only script. as posted as a comment on that hint, it would be nice to get the startup disk from nvram -p so you could do an if then statement in applescript.

http://www.macosxhints.com/article.p...20302215826526

it uses the bless shell command. very usefull. check out mike bombich's site for more useful unix commands (he has put together a list of ones that are useful for lab admins)

http://www.bombich.com/mactips/commands.html

JoelNelson 03-03-2002 03:40 PM

I am not very familiar with AppleScript or command line on OS X (I am much more familiar with OS 9). What code do I enter to create this script? Could someone e-mail the script to me as an attachment? E-mail

meancode 03-03-2002 04:01 PM

naw, we are not gonna email it, then the rest of the board wouldn't get to see it, and whats the fun in that? we if you are not very familiar with the command line that looking at the bless command is probably pretty confusing. i am writing the apple script right now, and will post it here when i am done.

rusto 03-03-2002 04:31 PM

NOTE: In my experience, the "do shell script" does not work when an AppleScript is saved as an application. Your best bet is to use one of these 3 methods to launch it "like an app":

1) DragThing
2) Script Runner
3) ScriptMenu

meancode 03-03-2002 04:53 PM

really rusto? ive had no problems. one thing to note when you use do shell script you are not performing it is you default shell (tcsh) so that can have some wonky effects.

ive got this apple script up and running and if i check in the system pref pane for startup disk it DOES change the startup system folder. i am going to restart and test thing thing out before i post. will post in a few minutes.

JoelNelson 03-03-2002 05:10 PM

Thanks for the help! The script sounds good.

ÊÊÊWith OS X my current default OS, if I
a> change startup system folder to OS 9 in the OS X Startup Disk panel
b> then restart in OS 9
will the OS 9 control panel also show that the default system folder has been changed to OS 9?
and
c> when from OS 9 I change the startup system folder back to OS X
d> and boot into OS X will it have remembered the settings change from OS 9?

ÊÊÊÊI will be frequently booting into OS 9 but since I will be using OS X even more frequently I want it to be the default OS. However, since I will use OS 9 a lot (with Virtual PC 3.0.3 so I can't just use Classic) I want an easy way to go back and forth between the two OSes. I will also have others in my family using the computer and they will have to restart in OS 9 also... hopefully this script will work since I don't think opening the control panels each time would be very popular. :)

rusto 03-03-2002 05:26 PM

Quote:

Originally posted by meancode
really rusto? ive had no problems. one thing to note when you use do shell script you are not performing it is you default shell (tcsh) so that can have some wonky effects.

Right, "do shell script" operates in the sh shell not tcsh.

meancode 03-03-2002 06:38 PM

to answer your question.

OS 9 and OS X will show that the default system folder has been changed. yes they will both update acordingly.

here is the script. its not the prettiest thing, but it gets the job done.
Code:

tell application "Finder"
        activate
        display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9.1", "Mac OS 9.2.2"} default button "Mac OS 9.1"
        set the user_choice to the button returned of the result
        if the user_choice is "Mac OS X" then
                do shell script "/usr/sbin/bless -folder '/System/Library/CoreServices' -setOF" password "YourAdminPW" with administrator privileges
        else if the user_choice is "Mac OS 9.1" then
                do shell script "/usr/sbin/bless -folder9 '/Volumes/Mac OS 9/System Folder' -setOF" password "YourAdminPW" with administrator privileges
        else if the user_choice is "Mac OS 9.2.2" then
                do shell script "/usr/sbin/bless -folder9 '/System Folder' -setOF" password "YourAdminPW" with administrator privileges
        end if
end tell

you will have to change some things here:

YourAdminPW # change this to your admin password
/Volumes/Mac OS 9/System Folder # change to the path of your OS 9 partition. interestingly enough you do not need escape charectors for the spaces.

just copy this code and paste it into Script Editor, then save it as an run only application (this has your admin password in it, so you dont want other snooping into it, you might also want to give group and everyone only execute privs to the script), make sure that Never Show Startup Screen is checked. i suggest saving it into you Scripts folder, every user should have one at ~/Library/Scripts. then you can access this from the Script Menu. if there are multiple accounts on this machine, you would want to put this script in the main scripts folder wich is at /Library/Scripts.

now to explain what the 3 buttons do. the choices are Mac OS X, Mac OS 9.1 and Mac OS 9.2.2. i have on my main partition Mac OS 10.1.3 and Mac OS 9.2.2, and then on a secondary partition Mac OS 9.1, wich i use when i want to reboot into 9. you may or may not want all 3 options, in wich case you can delete the last else if, also be sure to delete that choice out of the display dialog line as well. if you do not want the third choice the script would look like this:
Code:

tell application "Finder"
        activate
        display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9"} default button "Mac OS 9"
        set the user_choice to the button returned of the result
        if the user_choice is "Mac OS X" then
                do shell script "/usr/sbin/bless -folder '/System/Library/CoreServices' -setOF" password "YourAdminPW" with administrator privileges
        else if the user_choice is "Mac OS 9" then
                do shell script "/usr/sbin/bless -folder9 '/Volumes/Mac OS 9/System Folder' -setOF" password "YourAdminPW" with administrator privileges
        end if
end tell



NOTE: this script will not work if used by a non admin account unless you put them into the group that can use the sudo cammand. to see what i mean login as a non admin, go to the terminal and type: sudo ls. you will get a message saying that you cannot use the sudo command, wich IS NEEDED to pull this swich-the-startup-volume thingy off. the bless command HAS to be run as root. no way around that one.

setting other accounts up as admin accounts is surely not a very safe way to go, especialy if they do not know what they are doing. i am pretty sure a user has to be in the 'wheel' or the 'admin' group. if you make a user an admin user in the users pref pane, that user is put into these groups for you. maybe someone else can share some insight on how to allow a non admin account the ability to use sudo, and not have full admin privs. i would like to know myself.

i am really glad this tip was posted on the main hints site as i cannot stand waiting a half hour (well not that long but still) to chose my startup volume. also note i am not that great at apple script, i am sure there are better ways of doing this.

i got excellent results in testing this out. it worked great for changing the startup disk, i checked every time after i ran the script. i also rebooted to make sure it worked correctly as intended, and it did. the only snafu i came across was this: the first time i tryed to boot into OS 9.2.2 from OS X (after running the script) i got a question mark icon, and it seemed to roll over to the OS 9.1 system folder. so what i did was rebooted from OS 9.1 to OS 9.2.2, had no problems there. i then rebooted from 9.2.2 to OS X. the second time i ran the script and set it to boot from 9.2.2 i had no problems, no blinking question marks. same held true for the 3rd, 4th, 5th and 6th tries. i dont know why it did this. but i wanted to let you know this happened to me. i hope this helps, it sure makes my life easier now, man i love apple script. i learn more and more all the time.

about Virtual PC. you are not missing much by not using VPC 5 in OS X (other than the added features that VPC 4 and 5 have over version 3). it is a dog when run nativly in OS X. i still boot into OS 9 to use VPC 5 in OS 9, even though 5 is native in X. it slow as molassis in january. no kidding.

rusto 03-03-2002 06:57 PM

Nice job, Ken!

meancode 03-03-2002 07:10 PM

thanks rusto :D, i really impressed myself as well. id have to say thats the first script i have ever started from scratch.

btw here is an adition to the script to add a standard icon to the window, change this line:
Code:

        display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9.1", "Mac OS 9.2.2"} default button "Mac OS 9.1"
to this:
Code:

        display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9.1", "Mac OS 9.2.2"} default button "Mac OS 9.1" with icon note
it just adds a little more user friendly feel to it.

JoelNelson 03-03-2002 07:31 PM

Thanks! I have no partitions on my drive. I only have OS X 10.1.3 and OS 9.2.2. Will this script still work?

meancode 03-03-2002 08:09 PM

yes it will. here is a modified version for you, so you dont have to get muddy editing that thing :D
Code:

tell application "Finder"
        activate
        display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9"} default button "Mac OS 9" with icon note
        set the user_choice to the button returned of the result
        if the user_choice is "Mac OS X" then
                do shell script "/usr/sbin/bless -folder '/System/Library/CoreServices' -setOF" password "xxxxx" with administrator privileges
        else if the user_choice is "Mac OS 9" then
                do shell script "/usr/sbin/bless -folder9 '/System Folder' -setOF" password "xxxxxx" with administrator privileges
        end if
end tell


maclaw 03-03-2002 08:14 PM

The script is nice with the buttons, particularly for people with more than 2 alternatives. One thing I am considering is to create an "if" statement that would pick the default button based upon the current setting. This under the theory that, if you are already set to boot into 9.1 (for instance) and wanted it to stay that way, then you probably wouldn't be running the script. So the default should then be 10.1.3. But if you are currently set for 10.1.3, then you probably want the default to be 9.1 (or something else). Also, it would be nice to include a line in the dialog stating "Your current startup folder is 10.1.3 [or whatever it is]." I don't know if it's worth the trouble.

For the person who asked above, you can script the status of your current Startup Folder by using a couple of grep commands and checking the output.

For instance...
Quote:

bless -info / | grep -c "Blessed System Folder is /System/Library/CoreServices"
This line will return a value of 1 if the Startup Disk is the OS X system you are currently booted from. It will return a value of 0 if the Startup disk is anything else.
Quote:

bless -info / | grep -c "Blessed System Folder is /System Folder"
This will return a value of 1 if the OS 9 version on the same partition as your OS X boot volume is set as Startup, and 0 if it is not.
Quote:

bless -info / | grep -c "Blessed System Folder is /Volumes/Main/System Folder"
This final version will return a 1 if the OS 9.04 system on my partition "main" is set, and a 0 if it is not.

Consequently, if you run all three commands in a script only one of them will return a value of 1 at any given time. Set a variable to the value of the output and see which one equals 1. That is your current startup disk.

Of course, you will need to tailor the paths in these commands to look for the system folders on your specific system. Also, you will also need to repeat the command for every possible startup disk.

I'll actually write the script if I get some time but I thought others might at least be interested in the approach (since it was asked about). Be forewarned that I haven't actually tried this yet -- it's just an idea. But I don't see any glaring flaws. Feel free to chime in!!

meancode 03-03-2002 08:27 PM

maclaw - that is exactly what i was looking for. how to get what was the current starup and do an if then so the dialog would actually be conditional to the current setting. thanks. i will tinker some more :)

meancode 03-03-2002 08:44 PM

apple script chokes on the syntax:

bless -info / | grep -c "Blessed System Folder is /System/Library/CoreServices"

though that works correctly in sh and tcsh. dont know why. if you could shed some light on this maclaw and modify the script, please do! im still tinkering with it.

meancode 03-03-2002 09:32 PM

now when i try and startup classic mode i get the error stating "to start classic you need OS 9.1 or later installed... i dont know how this script could have affected this, none the less this is not good. what i dad to do was bless the 9.2.2 system (with the script). so this is one side effect to doing this through a script and not manualy through the pref pain :( but it did work after i reblessed the 9.2.2 system all was good again.

maclaw 03-03-2002 09:46 PM

The applescript error is apparently because of the path variable referenced by applescript. The command does work in zsh so you just need to give a direct reference...

Quote:

do shell script "/usr/sbin/bless -info / | grep -c 'Blessed System Folder is /System/Library/CoreServices'"
Enter this single line in a new script editor window and you should get back a response of 0 (or 1, depending on what Startup Disk you have set). Either way, do that quick test and then worry about setting it to a variable and incorporating into your script.

JoelNelson 03-03-2002 09:54 PM

Is this script still the correct one to use or are the above bugs relating to this script?

tell application "Finder"
activate
display dialog "Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9"} default button "Mac OS 9" with icon note
set the user_choice to the button returned of the result
if the user_choice is "Mac OS X" then
do shell script "/usr/sbin/bless -folder '/System/Library/CoreServices' -setOF" password "xxxxx" with administrator privileges
else if the user_choice is "Mac OS 9" then
do shell script "/usr/sbin/bless -folder9 '/System Folder' -setOF" password "xxxxxx" with administrator privileges
end if
end tell

meancode 03-03-2002 10:09 PM

JoelNelson, you are correct, that will work. the bug i refered to is that if you do not "rebless" os 9.2.2 and try and launch classic mode, it tells you u dont got OS 9 at all, not a pretty sight. to fix this all i did was rebless (with the script) the 9.2.2 folder and i was good to go. classic mode started right up and i was using my classic apps without a hitch. i dont know why this is, but im just sayin this is the way it is.

zs 03-04-2002 12:15 AM

Another AS to set the startup disk.
 
Here's a commented enhanced script that I wrote for those who don't feel like editing. I just need to find a way to get the currently blessed system folder so I can have it selected by default.

Code:

tell application "Finder"
        --We start by scanning for bootable disks.
        set ninePath to ":System Folder:" --It will not find any system folder not named "System Folder"
        set tenPath to ":System:Library:CoreServices" --Same idea goes for OS X, but I'm sure no one feels the need to mess with that.
        set theList to {} --Empty list soon to contain paths to bootable systems
        set theDisks to the name of every disk --Get a list of disks
        repeat with theDisk in theDisks --Wash, rinse, and repeat as needed.
                set theDisk to the theDisk as string --I don't know why the strings in theList aren't friendly, but they need to be cooerced.
                if (exists item (theDisk & tenPath)) then set the end of theList to (theDisk & tenPath) --OS X path? Add it to the list.
                if (exists item (theDisk & ninePath)) then set the end of theList to (theDisk & ninePath) --The same goes for OS 9.
        end repeat
end tell
--Need to find the currently blessed system folder to replace "item 1 of theList" with
set theSelection to choose from list theList with prompt "Select a startup disk:" default items (item 1 of theList) without multiple selections allowed
if (theSelection = false) then error number -128 --error number -128 = cancel button in a dialog  (choose from list returns false)
set thePath to (POSIX path of (theSelection as string)) --Turn the path into a POSIX path so the shell will deal with it
set nine to " '" --To patch into the final shell script if the path is an OS X path
try
        do shell script "echo '" & thePath & "' | grep 'System Folder'" --errors if nothing is returned
        set nine to "9 '" --The path is an OS 9 System Folder
end try
--Uncomment the end part and put in your pass if you feel comfortable doing that. I don't, so I just enter my pass every time.
do shell script "/usr/sbin/bless -folder" & nine & thePath & "' -setOF" with administrator privileges --password "insert admin pass here"
activate --Menu bar blanks out if this is not here

I've got 3 years of professional AppleScripting experience, so when I write a script, I like to go all out :D

meancode 03-04-2002 12:31 AM

WOW, cool. ok i was doing the same thing to mine, im glad i was on the write track. here is the snippet i am using to get the current blessed folder.
Code:

       
        do shell script "/usr/sbin/bless -info / | grep -c 'Blessed System Folder is /System/Library/CoreServices'"
        set the startup_volume to the result
       
        if startup_volume = "1" then
                display dialog "Your Startup Vulume is currently Mac OS X
Change your startup disk to:" buttons {"Mac OS X", "Mac OS 9.1", "Mac OS 9.2.2"} default button "Mac OS 9.1" with icon note
                set the user_choice to the button returned of the result
..........

i started putting all the bless commands in variables. the grep command would have to be done 3 times to get the main X, main 9 and main Classic startup volumes.

maclaw 03-04-2002 12:50 AM

meancode --

in order for classic to work, you must specify both an OS X and a classic system folder in your command when switching from OS 9 to OS X. I believe this should work...
Code:

sudo /usr/sbin/bless -folder "/System/Library/CoreServices" -folder9 "/System Folder" -setOF
If you have both, the X folder will be used for startup and the 9 folder for classic by default, unless you override with the -use9 flag.

Using bless -info / will confirm that setting only the OS X folder will leave "No OS Classic + X blessed 9 folder" while using both will show "Classic blessed folder is /System Folder" in addition to setting the Blessed System Folder.

maclaw 03-04-2002 01:10 AM

I'm not personally interested in searching for available startup disks since that is what the Startup Disk preference panel does already. I know exactly what Systems I have on my computer and wanted a way to point them out quickly and easily.

Below is an example of the final version I am using (incorporating other posters suggestions as well as my own).

A few things... I removed references to Finder because I call the script from the "Script Menu" menu extra and see no reason why the Finder should be brought forward. I prefer to pop up the dialog over whatever app I am currently using.

Also, for simplicity and brevity, I set it up using only 2 system folders (even though I have 4). I think it should be obvious how to add more System choices if you like, but if it's not clear let me know. I also used my OS X and OS 9 folders on the same partition to avoid having Volume Name references in the script. Again, you will have to customize the paths to your System Folders and add additional passes through each routine if you have more than 2 system folder choices. Just wanted something short I could post to show all of the features I incorporated.

This script will tell you what system you are currently set to boot from and ask if you would like to change. You are given a button for each choice. If you are currently set to boot into X, then the default button will be 9. If you are currently set for 9 then the default button will be X. Click a button (or hit return to pick default) and that's it.

My apologies that I have not commented the various routines but I wanted to get this done and posted in a hurry. Of course, this also means I may have missed something. Feedback is appreciated if I have left out or overlooked anything discussed above (before too many people rely on my mistakes!!)

Code:

do shell script "/usr/sbin/bless -info / | grep -c 'Blessed System Folder is /System/Library/CoreServices';echo $output"
copy (ASCII number of result) to ChkTen
do shell script "/usr/sbin/bless -info / | grep -c 'Blessed System Folder is /System Folder';echo $output"
copy (ASCII number of result) to ChkNineTwo

if ChkTen = 49 then
        set SysFold to "Mac OS 10.1.3"
else
        if ChkNineTwo = 49 then
                set SysFold to "Mac OS 9.2.2"
        end if
end if

if SysFold = "Mac OS 10.1.3" then
        set defButton to "Mac OS 9.2.2"
else
        set defButton to "Mac OS 10.1.3"
end if
display dialog "You are currently set to boot into " & SysFold & "." & return & return & ¬
        "Change your startup system to:" buttons {"Mac OS 10.1.3", "Mac OS 9.2.2"} default button defButton with icon note
set the user_choice to the button returned of the result
if the user_choice is "Mac OS 10.1.3" then
        do shell script "/usr/sbin/bless -folder '/System/Library/CoreServices' -folder9 '/System Folder' -setOF" password "AdminPassword" with administrator privileges
else if the user_choice is "Mac OS 9.2.2" then
        do shell script "/usr/sbin/bless -folder9 '/System Folder' -setOF" password "AdminPassword" with administrator privileges
end if

Be sure to substitute your PW for the phrase AdminPassword (2 times) and adjust any directory paths or text that may not match your particular setup. Save as an application and RUN-ONLY if you added your PW to the code.

BTW, I have added my other systems back in and all still works fine. I now have the options of 9.2.2, 9.1, 10.1.2, or 10.1.3. You just have to decide what criteria you want for determining the default button. I alternate between the two I boot into most (9.2.2 and 10.1.3)

Finally, bless does not seems to work with a System Folder prior to 9.1 (i.e. 9.0.4). If anyone can get that to work, let me know.

JoelNelson 03-04-2002 10:24 AM

I am getting a little confused with all the posts! :) I do plan to use Classic, more often even than the OS 9 mode. OS X 10.1.3 is my default OS. Which script is the best one for me to use?

maclaw 03-04-2002 11:59 AM

Quote:

I do plan to use Classic, more often even than the OS 9 mode. OS X 10.1.3 is my default OS.
if you are planning to keep 10.1.3 as your default OS and use Classic, but not OS 9 directly, then you really don't need any of these scripts. The effort here is to find a way to quickly change your startup disk between booting directly into OS X or directly into OS 9 only (not Classic).

Having said that, if you have a simple two OS system (with 10.1.3 and 9.2.2 on the same partition) you can probably use my script posted just above without alteration (aside from substituting your actual admin PW in the two places it currently says "AdminPassword".

Any variation from that basic setup will require customizing the paths to your system folders, the text indicating OS names (e.g. change 9.2.2. to 9.1 if that is what you are running), and so forth.

Because I wanted my script to be as descriptive as possible, while also keeping it brief, it requires customization. There are ways to write this that require no customization (i.e. have the script find your System folders for you, and have the script determine the version of those system folders) but that kind of robust functionality already exists in the Startup Disk control panel. My goal is to create a streamlined version that savrifices automated functionality for speed.

You have to pick the version that does what you want done (based on our descriptions, or based on trying them out). I don't know if any of the others are fully useable yet since I haven't tried them -- just been working on my own.

zs 03-04-2002 08:48 PM

Maclaw: Good point. The script I wrote almost does exactly what the Startup Disk pane does!:eek: My script is a bit quicker and requires fewer clicks, though. This is on my G4 450DP with only 3 partitions, so I imagine it will slow down quite a bit on slower systems with more disks/partitions. I would like to point out that telling the Finder to do something doesn't bring it to the front unless there is an activate in there.

Meancode: I'm glad to see you are discovering the joys of scripting. That code snippet only appears to work on a single partition (or maybe my system is being flakey). I'm looking to get the path of the folder, not just a true/false, though.

I noticed that this wasn't metioned yet, so here goes: The Startup Disk pane can be accessed more quickly if /System/Library/PreferencePanes/StartupDisk.prefPane is put in the dock. I think I saw something similar on my other favorite site once.

JoelNelson 03-04-2002 11:23 PM

I use OS 9 mode for Virtual PC and Norton Utilities/AntiVirus but everything else runs in OS X native or Classic. Thanks for the script; I will give it a try.

Regarding the tip posted at
http://www.macosxhints.com/article.p...10416184548354

I used the following script to try to reboot from OS 9 to X and got the following error when trying to check syntax: A application constant or consideration can't go after this application constant or consideration.
"Startup system folder" was highlighted when the error came up.

tell application "Startup Disk"
activate
set startup system folder alias to alias
"Macintosh HD:System:Library:CoreServices:BootX"
quit
end tell
tell application "Finder"
restart
end tell

Jaime 04-20-2002 07:53 PM

A security hole in this method
 
The procedure of many answers to the question of how to restart automatically OS X in OS 9 using a AppleScript, can have a security hole, provided you have multiple users in your computer. If you bless the system 9 folder with a run-only script with contains the administrator password and other users can start this script (not to read it), then they have access to all the information of the computer after restart, because OS 9 loses the folder privileges.
If this scenario is a problem, there are several solutions:

1) Set the privileges of the script for use of the administrator only.
2) Store the script in a reserved folder.
3) Do not store the password in the script.

I am waiting for your answers.

Jaime.


All times are GMT -5. The time now is 06:21 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.