A stylised depiction of Rob

Primary Unit

Restarting in Bootcamp the easy way

Update 2013-05-25

Please ignore this post and look at my most recent post about the script.

I’ve used a Mac — and Windows on a Mac — since 2007 but I’ve just, only just, figured out a way to quickly restart from OSX into Windows.

And it’s really easy to use it on your computer too, even if the words code or script make you freak out a bit. (If they don’t, skim the post for the meat but make sure to read the last few paragraphs.)

The setup

I use LaunchBar to run a script that mounts my Bootcamp partition, sets it as the startup disk for the next time only and then reboots into it. It means I don’t have hold down Option (⌥) while the computer boots up, which I’ve found very unreliable now that I use a bluetooth keyboard. (It might actually just be the SSD booting quicker than the keyboard can switch on. That’s a nice problem to have.)

As it’s just a bit of AppleScript you can also run it from FastScripts or the system-wide script menu.

The script

In this old post, I suggest you place your password in the script and am naive about the protection offered by saving it as run-only. Please ignore this post and read my new one.

applescript:
set deviceID to (do shell script "diskutil info [Your Bootcamp ¬
partition's name] | grep Identifier | awk '{print $3}'")
do shell script "bless -device /dev/" & deviceID & " -legacy -setBoot ¬
-nextonly" password "XXXXXXX" with administrator privileges
tell application "Finder" to restart

Update 07/09/2012: I’ve changed the script to get the device ID in a more sophisticated way, so it’s far less likely to break even if you have more than 10 disks.

Update 21/07/2012: OSX doesn’t guarantee that your Windows partition will get the same device ID every time the computer boots, which means my previous method (which was dependent on the device ID not changing) can and will break if you have more than one HDD.

I’ve changed the script to use the partition’s name and then pull the correct device ID from the result of diskutil info [Partition Name]. If you have more than 10 disks you may run into problems.


First of all, open up AppleScript Editor (called Script Editor before Lion) from the Utilities subfolder of your Applications folder and copy the code above into a new window.

Then open Disk Utility from the Utilities folder, select your Bootcamp partition, click the Info button and change [Partition Name] in the script to its exact name. It should look something like this:

applescript:
set terminalRAW to do shell script "diskutil info BOOTCAMP"

If the partition’s name is more than a single word, put it in quotes with backslashes in front, like this: \"Windows HDD\".

Now change myPassword to your user account’s password and save the script, making sure to check the box next to Run Only at the bottom of the save sheet.

(Saving a run-only script makes sure no-one will be able to open it and grab your password. If you’d rather not put it in there to begin with just delete password "myPassword" and the system will prompt you for it every time you run the script.)

What should I do with it?

Generally the best place to put your finished script is in ~/Library/Scripts. This means it’ll appear in the system-wide script menu, which you can enable from AppleScript Editor’s preferences, as well as FastScripts. (By default the user library is hidden in Lion so just copy that path, switch to the Finder, click Go in the menu bar and choose Go to Folder and paste it in).

To use it in LaunchBar you’ll want to go into its indexing preferences (⌘⌥I), select Actions, then Options, and check the box next to ~/Library/Scripts. I use “Restart in Windows” to keep things simple.

Why not save it as an application?

I tried this and it didn’t work out well. Even though it should quit immediately after the final command (restart) is passed, Lion’s resume-after-restart feature (where it reopens running applications) can put the computer into a loop where booting back into OSX from Windows causes the computer to boot into Windows.

(If you accidentally get into this position perform a safe boot by holding shift from the Mac chime until you see the Apple logo. Then log in, delete the application and empty the trash.)

Thankfully the only advantage to having it as an application, that I can see, is that you can put it in the dock. (And you don’t want to be one of those people.)