1

I have tried using AppleScript to change my startup disk for Boot Camp and Mac. Previously I have tried using terminal (do shell script) but it seems that that option requires me to enter an administrative password.

So:

  1. How do I make it such that I can retrieve the password? I can use keychains.
  2. How do I program the AppleScript/use Terminal through AppleScript to:
    • go into System Preferences/launch it if it is not already open
    • go to the Startup Disk preference pane
    • set the bootdisk to Windows/Mac and have the option to shut down/reboot. (I would like to be able to have a single script to handle everything even if I am restarting into macOS, or shutting down and will use Windows later etc.)

Thanks in advance! (P.S. I have tried searching for some answers and there is a really good answer here over at StackOverflow but it doesn't seem to work on Sierra. Also the other responses I have found on SO and Ask Different don't work.)

The two sets of code I tried (first one using System Preferences, second using Terminal, both using AppleScript):

display dialog "Select a startup disk" buttons ¬
    {"BOOTCAMP", "Macintosh HD"}
if button returned of the result = "BOOTCAMP" then
set bootVol to "BOOTCAMP" as text
else if button returned of the result = "Macintosh HD" then
    set bootVol to "Macintosh HD" as text
end if

do shell script "security 2>&1 >/dev/null find-generic-password -gl \"Insert Password Here\" | awk '{print $2}'"
set myPass to (text 2 thru -2 of result) as text

do shell script "bless -mount \"/Volumes/" & bootVol & ¬
    "\" -setBoot" user name "username" password myPass with administrator privileges

do shell script "shutdown -r now" user name "username" password myPass with administrator privileges

(Insert Password Here refers to the generic key. What I found:

  • when running the command in terminal without backslashes the command works and gives me the password assigned including the quotes like this "Password"

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.startupdisk"
    get the name of every anchor of pane id "com.apple.preference.startupdisk"
end tell

try
    tell application "System Events" to tell process "System Preferences" to click button "BOOTCAMP Windows" of radio group 1 of scroll area 1 of group 1 of splitter group 1 of window 1
end try

try
    tell application "System Events" to tell process "System Preferences" to click button "Restart..."
end try

I need to be able to unlock System Preferences, preferably using the same keychain password as I have mentioned in the first script. The second command tell application "System Events" to tell process "System Preferences" to click button "Restart..." does not work.

  • Can you clarify what didn't work from the answer you referenced at SO? Also, are you able to share your code - obviously removing any username/password details. It may be that what you already have only needs very minor editing in order for it to work. – Monomeeth Oct 4 '16 at 5:50
  • @Monomeeth When inputting the code into AppleScript and running it stated that on set myPass to getPW() script doesn't understand the error message. I have two sets of code, one trying to use System Preferences, one trying to use Terminal via AppleScript. – Brick Oct 4 '16 at 6:42
  • The part about putting Bootcamp as the volume after receiving bootcamp can be shortened to just copy the returned box but I would like to be able to customise the name if need be in the future. – Brick Oct 4 '16 at 6:55
  • @klanomath I'll assume you meant enabling the following lines separately. Lines 9-10 when activated gives "my_real_password"; lines 11-13 when activated gives The variable BootVol is not defined but substituting it for an actual drive gives the variable myPass is not defined, to which I substituted my password, and it worked, giving "". Enabling lines 14-15 gave "shutdown -r now" after entering in my administrative password. – Brick Oct 4 '16 at 13:56
  • Doing the bless command with administrative permissions with password var myPass returns The administrator user name or password was incorrect. Value of myPass is "Password" including quotes. Lines 9-13 were activated. – Brick Oct 4 '16 at 14:16
1

In my environment the script works as expected. I used a different name for the generic key though: boot_key

The attributes of boot_key:

enter image description here enter image description here

The password is self-evidently also klanomath's login password. security is always allowed to use the key!


To debug the Apple script enable "Show Log" with cmd3

The script:

display dialog "Select a startup disk" buttons ¬
    {"BOOTCAMP", "Macintosh HD"}
if button returned of the result = "BOOTCAMP" then
    set bootVol to "BOOTCAMP" as text
else if button returned of the result = "Macintosh HD" then
    set bootVol to "Macintosh HD" as text
end if

do shell script "security 2>&1 >/dev/null find-generic-password -gl \"boot_key\" | awk '{print $2}'"
set myPass to (text 2 thru -2 of result) as text

do shell script "bless -mount \"/Volumes/" & bootVol & ¬
    "\" -setBoot" user name "klanomath" password myPass with administrator privileges

do shell script "echo \"shutdown -r now\"" user name "klanomath" password myPass with administrator privileges

will then reveal - after choosing "Macintosh HD" - in the "Replies" part:

tell application "Script Editor"
    display dialog "Select a startup disk" buttons {"BOOTCAMP", "Macintosh HD"}
        --> {button returned:"Macintosh HD"}
end tell
tell current application
    do shell script "security 2>&1 >/dev/null find-generic-password -gl \"boot_key\" | awk '{print $2}'"
        --> "\"Passw0rd\""
    do shell script "bless -mount \"/Volumes/Macintosh HD\" -setBoot" user name "klanomath" password "Passw0rd" with administrator privileges
        --> ""
    do shell script "echo \"shutdown -r now\"" user name "klanomath" password "Passw0rd" with administrator privileges
        --> "shutdown -r now"
end tell
Result:
"shutdown -r now"

You may log various variables explicitly by adding log $variable lines (which is redundant with the "Replies" in some cases):

display dialog "Select a startup disk" buttons ¬
    {"BOOTCAMP", "Macintosh HD"}
if button returned of the result = "BOOTCAMP" then
    set bootVol to "BOOTCAMP" as text
else if button returned of the result = "Macintosh HD" then
    set bootVol to "Macintosh HD" as text
end if
log bootVol

do shell script "security 2>&1 >/dev/null find-generic-password -gl \"boot_key\" | awk '{print $2}'"
set myPass to (text 2 thru -2 of result) as text
log myPass

do shell script "bless -mount \"/Volumes/" & bootVol & ¬
    "\" -setBoot" user name "klanomath" password myPass with administrator privileges
log bootVol
log myPass

do shell script "echo \"shutdown -r now\"" user name "klanomath" password myPass with administrator privileges
log myPass

which then reveals:

tell application "Script Editor"
    display dialog "Select a startup disk" buttons {"BOOTCAMP", "Macintosh HD"}
        --> {button returned:"Macintosh HD"}
end tell
(*Macintosh HD*)
tell current application
    do shell script "security 2>&1 >/dev/null find-generic-password -gl \"boot_key\" | awk '{print $2}'"
        --> "\"Passw0rd\""
    (*Passw0rd*)
    do shell script "bless -mount \"/Volumes/Macintosh HD\" -setBoot" user name "klanomath" password "Passw0rd" with administrator privileges
        --> ""
    (*Macintosh HD*)
    (*Passw0rd*)
    do shell script "echo \"shutdown -r now\"" user name "klanomath" password "Passw0rd" with administrator privileges
        --> "shutdown -r now"
    (*Passw0rd*)
end tell

or as a screenshot:

enter image description here

Now you should be able to detect the error in your script or your environment.

  • Thanks @klanomath for this answer but is it possible to have a dialogue that will prompt for shutdown or reboot right after the bless command, replacing the makeshift shutdown -r now – Brick Oct 5 '16 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .