AppleScript to Shutdown

Im new to AppleScript, and was wondering how to write one that would close all open applications and shutdown. Also, it would be nice if there were no interface whatsoever.

Posted on Jul 12, 2010 5:59 PM

Reply
3 replies

Jul 12, 2010 9:04 PM in response to Arzati

Using any 'normal' AppleScript (such as telling System Events to shut down) would have the same apparent effect as choosing Shut Down from the Apple menu - that is, it will iterate through each open applications giving the app a chance to clean up.

This may include giving the user the option to save their work in progress, which obviates your 'no interface whatsoever' request.

The only way to ensure that doesn't happen is to either a) only use applications that you can script directly and that have an option to quit silently (few do), or b) force a shutdown of the system (which runs the risk of corrupting files that are open/in use (a really, really bad idea for applications with active files). The latter approach can be achieved via something like:

do shell script "shutdown -h now" with administrator privileges username "<youradminusername>" password "<youradminpassword>"


Although there are caveats of posting a script that includes your username and password in it.

Jul 12, 2010 9:07 PM in response to Arzati

--You didn't specify your operating system. If it's Snow Leopard, then copy the script below and paste it into a fresh AppleScript Editor window. If it's an earlier operating system, paste it into a fresh Script Editor window.

--In either case, save the script to the desktop as an application named "Quit Apps & Shut Down." If you double click on that, it should do what you requested -- but note that it will ask you (for each app) whether you want to save unsaved changes (if there are any). (There may be some instances where an open app will prevent shutdown.)


tell application "System Events"
set ProcNm_ to name of every application process whose visible is true
repeat with i_ from 1 to count items of ProcNm_
set TarProc_ to item i_ of ProcNm_
try
tell application TarProc_ to quit
end try
end repeat
shut down
end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to Shutdown

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.