One-click BootCamp activation using AppleScript and Keychain

12/31/08

Another in a series of “I better post this before I forget how I did it” code snippets.

Switching from a bootcamp Windows partition back to OSX is easy; there’s an icon in the system tray you can click and just choose “restart in Mac OS X”. Going the other way around is not so simple: you either have to open System Preferences, find Startup Disk, select your windows partition, and click Restart; or you have to reboot and hold down the Alt key and hope that it pops up the OS picker which I don’t know about you but for me that only works about one time in ten — most of the time it just goes straight back into OSX and I have to wait through the whole boot process before I can try again.

You can download BootChamp if you don’t mind cluttering your menubar with another icon, or Flipside which is better than the default, but still not quite handsfree.

Or you can save this AppleScript snippet as an application, and trigger it using Quicksilver or Spotlight or the Dock or however else you feel like doing it.

(This was originally posted using a different (and much stupider) technique. This way is better, because it doesn’t require you to muck around in Keychain Access manually.)

try tell application "Keychain Scripting" set myKey to first key of current keychain whose name is "Switch To Windows" set myPass to password of myKey set myVol to account of myKey end tell on error -- This must be the first time this has run. -- Find the windows boot volume: set windowsVolumeName to "" tell application "Finder" repeat with theDisk in (list disks) if (exists folder "Windows" of disk theDisk) and (exists file "CONFIG.SYS" of disk theDisk) then set windowsVolumeName to theDisk as string end if end repeat end tell if windowsVolumeName = "" then display dialog "Sorry, couldn't detect your Windows volume." buttons {"Dang"} default button 1 with icon stop return end if -- get the password: display dialog "Enter your administrator password:" default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer set myPass to the text returned of the result -- make sure it's the correct password before we store it: try set user_name to do shell script "id -un" set test_pass to do shell script "ls /private/var/root" user name user_name password myPass with administrator privileges on error display dialog "That doesn't seem to be the right password. Relaunch this program to try again." return end try -- store the drive and password for next time: tell application "Keychain Scripting" make new generic key at current keychain with properties {kind:"application password", name:"Switch To Windows", account:windowsVolumeName, password:myPass} end tell set myVol to windowsVolumeName end try -- now, finally, reboot into Windows: do shell script "bless -mount /Volumes/" & myVol & " -legacy -setBoot -nextonly" password myPass with administrator privileges do shell script "shutdown -r now" with administrator privileges

Tested only in OS10.5; might or might not work for you without tweaking. For your convenience I’m making a compiled version of this script available — use it at your own risk. If you have any problems with it, though, let me know and I’ll try to fix it.

One final warning: This reboots by force-quitting every application immediately. This has the benefit of not leaving the system hanging there waiting for you to click Safari’s irritating “You still have a window open; are you sure you want to quit?” alert — but if you are running any applications which become unhappy when they don’t exit cleanly, remember to quit them before running this.