Here are some useful /I think/ AppleScript examples which can be used on their own or in other scripts. The presumption here is you already have an idea of how to use the Script Editor. Keep in mind some scripts might not behave as it should depending on your OS X and will need to be slightly adjusted. I will do separate post just for EL CAPITAN when I got some time. 🙂
Ways to open and close applications with apple script:
If you want or need to use the Terminal to open an application:
tell application “Terminal”
do script “open /Applications/Safari.app; exit”
end tell
delay 2
tell application “Terminal” to quit
But if you prefer the short way of doing it you can just type
do shell script “open /Applications/Safari.app; exit”
or
tell application “Safari” to activate
And to close application few options are as follows
tell application “Safari” to quit
or
do shell script “killall Safari”
Closing Finder application will simply reload it, which is sometimes necessary
do shell script “killall Finder” password “YourPassword” with administrator privileges
Managing optical drives:
When the case is to manage optical drives using applescript my personal opinion is this is best done with the use of drutil command. To find more about the command you can man it and in terminal window you can:
$ drutil info
As an example to close the tray of optical drive with the name “DVD-RW GH41N” you can:
tell application “Terminal”
do script “drutil tray close -drive ‘DVD-RW GH41N’; exit”
end tell
delay 3
tell application “Terminal” to quit
or if it is external one you can just
tell application “Terminal”
do script “drutil tray close -drive external; exit”
end tell
delay 3
tell application “Terminal” to quit
To open the tray
tell application “Terminal”
do script “drutil eject -drive ‘DVD-RW GH41N’; exit”
end tell
delay 3
tell application “Terminal” to quit
Automating shutdown and restart:
If the case is to shutdown or restart the computer with just a click of a button you can use the scripts below. They will also do some additional tasks like switching off the option of restarting the active application on boot, silence the startup chime, empty the trash can, which you may of course skip.
Reboot using terminal command:
do shell script “defaults write com.apple.loginwindow TALLogoutSavesState -bool false” password “YourPassword ” with administrator privileges
delay 2
do shell script “nvram SystemAudioVolume=%80” password “YourPassword ” with administrator privileges
delay 2
do shell script “shutdown -r now” password “YourPassword” with administrator privileges
Reboot using Mac OS X menu, mind though menu item 13 in the example might not be correct for you, so adjust it if necessary:
do shell script “defaults write com.apple.loginwindow TALLogoutSavesState -bool false” password “YourPassword” with administrator privileges
delay 2
do shell script “sudo nvram SystemAudioVolume=%80” password “YourPassword” with administrator privileges
delay 2
tell application “Finder” to activate
tell application “System Events”
tell process “Finder”
click menu item 13 of menu 1 of menu bar item “Apple” of menu bar 1
end tell
end tell
delay 3
tell application “System Events”
tell process “loginwindow”
activate
click button 2 of window 1
end tell
end tell
To shutdown the computer, following the same order and logic as above use the examples below. Again mind menu item 15, the number might not be the same for you.
tell application “Finder”
empty trash with security
end tell
do shell script “shutdown -h now” password “YourPassword” with administrator privileges
or
tell application “Finder”
empty trash with security
end tell
delay 10
tell application “Finder” to activate
tell application “System Events”
tell process “Finder”
click menu item 15 of menu 1 of menu bar item “Apple” of menu bar 1
end tell
end tell
delay 3
tell application “System Events”
tell process “loginwindow”
activate
click checkbox 1 of window 1
click button 2 of window 1
end tell
end tell
Changing startup disk:
Something else which like me you can find useful is to quickly reboot to another Mac OS X installation. The first script is using script with buttons, which is faster, but Apple decided you do not need more then 3 buttons in a script dialog. So if you are having more than 3 bootable drives /some people do, trust me :-)/ you have to use a script with a list. Reboot.app in the examples is another script to reboot the computer, but you can do it differently.
The example script with the buttons:
display dialog “Select a startup disk” buttons {“NameOf HD1”, “NameOf HD2”, “QUIT”} default button “QUIT”
if button returned of result = “QUIT” then
quit
else
set bootVol to the button returned of the result as text
do shell script “bless -mount \”/Volumes/” & bootVol & “\” -setBoot” password “YourPassword” with administrator privileges
tell application “Reboot.app” to activate
end if
And for a list it might be something like this:
set Disks to {“1”, “2”, “3”, “4”, “5”}
set selectedDisk to {choose from list Disks with title “Startup Disk” with prompt “Choose Disk” default items “2” without multiple selections allowed and empty selection allowed}
do shell script “bless -mount \”/Volumes/” & selectedDisk & “\” -setBoot” password “YourPassword” with administrator privileges
tell application “Reboot.app” to activate
This is just an example script showing how to control an application with System Events, in this case Skype. Here the preferred web cam is picked from the list of available devices.
tell application “Skype” to activate
delay 5
tell application “System Events”
tell process “Skype”
click menu item 3 of menu 1 of menu bar item “Skype” of menu bar 1
end tell
end tell
delay 5
tell application “System Events”
tell process “Skype”
set winName to the name of window 1
if winName = “General” then
click button “Audio/Video” of toolbar 1 of window 1
click pop up button 2 of window 1
click menu item “FaceTime HD Camera (Display)” of menu 1 of pop up button 2 of window “Audio/Video”
else
tell application “System Events” to keystroke “w” using command down
end if
end tell
end tell
That is quite enough for now, future scripts will be in separate posts, hope you found this information useful. Enjoy 🙂