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:
- How do I make it such that I can retrieve the password? I can use keychains.
- 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.
set myPass to getPW()
scriptdoesn't understand the error message
. I have two sets of code, one trying to use System Preferences, one trying to use Terminal via AppleScript.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.bless
command with administrative permissions with password var myPass returnsThe administrator user name or password was incorrect
. Value of myPass is"Password"
including quotes. Lines 9-13 were activated.