Reboot Into BootCamp – Script, AppleScript, App 10

Tutorial

Share
Reboot Into BootCamp – Script, AppleScript, App

Hey

I use Windows semi-regularly to play PC games, when booting into Windows with Boot Camp, I normally have to restart as normal, wait for the main screen to pop up, press Option, restart my Mac again because I forgot to press option, select my Windows hard drive and boot up as normal. This takes far too long and in the boot time I would rather get a drink than wait to select the correct drive. So I went on a quest last night to find a simple one click solution to reboot, automatically into Windows. I wanted a solution where I click an icon and my Mac restarts, avoiding an option to enter my password. As you will find out this is a lot harder than you would think. There is three solutions that do work each with varying results. These are a Terminal command /script. An AppleScript and an Application someone has designed.

Terminal Script

When doing anything like this I automatically turn to a Terminal script. Most of the time there is a solution. There is actually a solution which does work, however I deem it to be a bit “violent” as it kills everything immediately and then restarts. This is OK, however any unsaved files will not prompt the save dialogue box. The script itself is as follows.

/usr/sbin/bless --device /dev/disk0s3 --setBoot --legacy --nextonly | sudo shutdown -r now

The thing to note is that /dev/disk0s3 is the Boot Camp disk. You can find out which disk it is in Disk Utility, you may need to change disk0s3 if you have more than one partition or drive.

The previous script did work, a warning does come up every so often in Terminal. Due to the way your Mac system is built you cannot skip the add password bit. This includes setting the script as root user. I could get round this by adding my password into the script as a variable but this would open a security hole that I didn’t want.

As a result the Terminal script did what I want, however it was a little bit violent, as mentioned, and I would need to add my password, so I turned to AppleScript.

AppleScript – Background

I find AppleScripts tend to follow Terminal scripts in there layout and function. As a result it wasn’t too hard in finding and working out a simple one line script that I could use. In AppleScript typing the following and compiling it would allow you to restart into BootCamp.

do shell script "bless -mount /Volumes/WINDOWS/ -legacy -setBoot -nextonly; shutdown -r now" with administrator privileges

This will prompt your for your password and restart in the same manner as the Terminal script (since it is running a Terminal script). AppleScript has an option where you can add a password for it to run with. Normally you would be opening up a hole in your security, but if you save the resulting Application as “Run-only” the contents is scrambled, as a result it is a lot harder to get your password. This can’t be done with a Terminal script.

do shell script "bless -mount /Volumes/BOOTCAMP/ -legacy -setBoot -nextonly" password "xxxxxxx" with administrator privileges
do shell script "shutdown -r now" with administrator privileges

This still restarts your computer by killing everything, it doesn’t do a nice restarts by closing Applications, this is where the power of AppleScript comes in.

AppleScript – Working App

Having learn’t (for about two hours) in getting something to work I ended up if a working AppleScript that did what I wanted. It would restart my computer into Boot Camp, have a nice shutdown by closing apps (not violently killing them) and not need my password but still being secure. To do this open AppleScript Editor and paste the following.

do shell script "bless -mount /Volumes/BOOTCAMP/ -legacy -setBoot -nextonly" password "xxxxxxx" with administrator privileges
tell application "Finder" to restart

Edit both the disk which Windows is stored on and your password. Then go to File > Save As, name the application and select “Application” as the file format and select “Run Only” from the check boxes. Then save and test out your file. It should restart into window, while shutting your computer down in a timely and safe manner.

Prefect, a script to do exactly what I want in a safe any easy way. The icon was produced by pasting an image into the icon in the Get Info pane of the finished AppleScript Application.

reboot into windows applescriptApplication

To finish off this post I searched for an application which does the same thing, if your note a code wizz but want something useful. I searched around and found BootChamp, it will restart you Mac into Windows but you can quickly access this from the menu bar. It does need your password which is semi-annoying but it is still a good little app.

Conclusion

To finish off I have shown your three ways in which your can restart you Mac into Windows. The first is a Terminal script which is violent but very quick. An AppleScript which does the same job but allows your to skip using your password and an Application which does the same job.

If you have any comments please leave one below.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips

10 Responses to “Reboot Into BootCamp – Script, AppleScript, App”

  1. 1

    There’s also QuickBoot, which will reboot your computer to another OS [you pick from a list in the menu bar [it's a background app with a right-hand-side menu], and then when you reboot out of that operating system, the Mac restarts in your original OS. So it just reboots to the other OS once, then back to your original. And it lists the OS’s of all attached drives and it’s free.

    Comment By dave on January 9th, at 7:26 pm

  2. 2

    Here is my applescript (which is saved as an application as well):

    do shell script "bless -mount /Volumes/WINDOWS/ -legacy -setBoot -nextonly" password "XXXXXXXX" with administrator privileges
    ignoring application responses
    tell application "Finder"
    restart
    end tell
    end ignoring

    As you can see it’s almost identical.
    Cheers,
    Greville

    Comment By Greville Whittle on January 17th, at 9:45 pm

  3. 3

    Thanks Greville. I assume that code stops any warnings. Which isn’t quite what I wanted, but may be useful to other people. Thanks for sharing.

    Comment By admin on January 17th, at 10:21 pm

  4. 4

    I get an error after following the instructions, any ideas?

    “Can’t statfs /Volumes/Windows_XP”

    Thanks!

    Comment By Hans on January 25th, at 6:38 am

  5. 5

    @Hans, sorry I don’t.

    Comment By admin on January 25th, at 5:29 pm

  6. 6

    sudo bless –device /dev/disk0s5 –setBoot –legacy

    I typed that into my terminal after I installed Ubuntu on my computer along with rEFIt. Whenever I turn my computer on now, it goes straight to ubuntu and fails. I’m going to put my HD into an external casing and attempt to boot from a friends computer. If i CAN.. what is the command to reverse it?
    Something like..

    sudo bless –device /dev/disk0s2 –setBoot –lNAMEOFOSHERE

    But i’m not sure of the name of the OS that apple calls it. They call windows/ubuntu ‘legacy’, obviously. But what about their own? This would be a HUGE AND MAJOR HELP for me if anyone knew how.
    Please email me. or Respond.
    -eric

    Comment By Eric on February 18th, at 7:53 pm

  7. 7

    tried this out, finally got it working.

    @hans: i got the same problem as you did, and i checked it out. it seems that if you have NTFS-3G installed, it interferes with OS X’s native NTFS driver, so OS X cannot detect it as a drive to boot from. if you have NTFS-3G installed, you will need to disable NTFS-3G for your bootcamp partition. this can be done via the NTFS-3G preference pane in System Preferences.

    Another thing I found out is that if you use X11 on your mac, a hidden file called “.Xauthority” will be created in your home directory, which prevents you from accessing your files from Windows, even if you have NTFS drivers installed (both Bootcamp 3.0′s and Paragon’s NTFS drivers). The only way I have found to overcome this problem is to delete .Xauthority.

    my code deletes .Xauthority and restarts the mac in windows. this runs perfectly fine for me with NTFS-3G disabled for my bootcamp parition goes like this:

    do shell script “sudo rm -f .Xauthority” with administrator privileges
    do shell script “bless -mount \”/Volumes/BOOTCAMP\” -legacy -setBoot -nextonly” with administrator privileges
    tell application “Finder” to restart

    just copy and paste this in Applescript Editor, and you should be good to go.

    Comment By jehont on May 10th, at 1:10 pm

  8. 8

    hi, just an update on my previous post.

    the previous code needed the user to type in the password twice. this is highly troublesome, but i still wanted the security of having to input my password rather than having it stored statically as mentioned in the main article. i updated my code to produce a dialog asking for the user to input the password. no password is stored in the code of the program.

    set adminpassword to the text returned of (display dialog “System will now reboot into Windows.\nPlease enter password:” default answer “” with hidden answer)
    do shell script “sudo rm -f .Xauthority” password adminpassword with administrator privileges
    do shell script “bless -mount \”/Volumes/BOOTCAMP\” -legacy -setBoot -nextonly” password adminpassword with administrator privileges
    tell application “Finder” to restart

    hope you guys find this helpful!

    Comment By jehon on May 10th, at 1:33 pm

  9. 9

    so what if I just wanted to boot into another Mac partition? would I remove “-legacy”?

    Comment By Adam on May 20th, at 6:59 pm

  10. 10

    @hans @jehon

    hey guys, I was getting the same error. I found a way to reboot into bootcamp though without having to disable the ntfs-3g drivers for your windows partition (major problem for me as I need write access to many files in this partition from OSX). I basically just bless the disk instead of the volume, to do so though you first need to unmount the bootcamp volume (or so is suggested in the bless manual).


    display dialog "Do you wish to restart?" with title "Reboot?" with icon stop buttons {"Yes, in OSX", "No thank you", "Yes, in Windows"} default button 3 giving up after 5
    if the button returned of the result is "Yes, in OSX" then
    tell application "Finder" to restart
    else if the button returned of the result is "Yes, in Windows" then
    do shell script "hdiutil unmount /Volumes/WINDOWS\\ XP -quiet"
    do shell script "bless -device /dev/disk0s3 -legacy -setBoot -nextonly" password "XXXXXXXX" with administrator privileges
    tell application "Finder" to restart
    end if

    If you use this make sure to change your disk (disk0s3) to your appropriate disk.

    Comment By nick on September 4th, at 6:07 pm