AppleScript for restarting to Bootcamp?

I have tried to use an AppleScript to reboot to Windows It's not going well though, as when I tried it the first time, a white screen appeared on startup and didn't go away. A hard reset rebooted to Yosemite. I have this script saved as an Application and in my Dock for instant restart, but it's been disappointing.

Here's the script:

set adminpass to "MYPASSWORDWENTHERE"

tell application "Finder"
    set iconPath to (get name of startup disk) & ":Applications:Utilities:Boot Camp Assistant.app:Contents:Resources:DA.icns" as alias
end tell

set askRestart to display dialog "Restart in Windows?" buttons {"Cancel", "Restart"} default button 1 with icon iconPath
set doRestart to button returned of askRestart

if doRestart is equal to "Cancel" then
    quit
end if

if doRestart is equal to "Restart" then
    do shell script "bless -mount /Volumes/BOOTCAMP/ -legacy -setBoot -nextonly; shutdown -r now" password adminpass with administrator privileges
end if

Can anyone help me get this thing to work?


I tested this using Yosemite and it does work. I assume you are doing a BIOS boot for Windows. If you are doing an EFI boot, then this script will probably not work. BIOS booting is the traditional way that BootCamp installs Windows on Macs. I added an additional button to allow changing the default operating system to Windows.

You are aware that you can hold down the alt/option key at startup to select which operating system to boot to? I also assume, you know that the default boot operating system can be selected from the System Preferences in OS X and the Control Panel in Windows?

You can also install the free rEFInd boot manager. This product can produce an optional boot menu each time you start (or restart) your Mac. You can then select which operation system to boot to. If interested, let me know, and I will help you set up rEFInd.

set output to do shell script "diskutil list /dev/disk0"
repeat with row in paragraphs in output
    if row contains "Microsoft Basic Data" then
        set slice to last word in row

        set adminpass to "MYPASSWORDWENTHERE"

        tell application "Finder"
            set iconPath to (get name of startup disk) & ":Applications:Utilities:Boot Camp Assistant.app:Contents:Resources:DA.icns" as alias
        end tell

        try
            set askRestart to display dialog "Restart in Windows?" buttons {"Cancel", "Once", "Always"} default button 1 with icon iconPath
        on error
            exit repeat
        end try
        set doRestart to button returned of askRestart

        if doRestart is in {"Restart", "Once", "Always"} then
            set command to "bless --device /dev/" & slice & " --legacy --setBoot"
            if doRestart is in {"Restart", "Once"} then
                set command to command & " --nextonly"
            end if
            set command to command & "; shutdown -r now"
            try
                do shell script command password adminpass with administrator privileges
            end try
        end if

        exit repeat
    end if
end repeat