Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I am more used to the Javascript style event handlers,

but is there a similar thing in applescript that listens to system events like logout or shutdown to execute code

or is there a way to have an applescript run on shutdown or logout?

I know about script timer but I would rather have something free.

All my script does is mute the volume for the next startup.

share|improve this question
add comment

2 Answers

Logout hooks were deprecated in 10.4, but they still work in 10.8.

echo $'#!/usr/bin/osascript\n\nset volume with output muted' > ~/.logouthook
chmod +x ~/.logouthook
sudo defaults write com.apple.loginwindow LogoutHook ~/.logouthook

sudo defaults write modifies files in /var/root/Library/Preferences/. The program can't be inserted as an argument directly.

Trapping the exit signal only seems to work when logging out and not when shutting down.

share|improve this answer
add comment

Launch agents can be used for this. See this tutorial on MacScipter. If you are not familiar with the process, Lingon offers a simple interface to create the file. Here is an example to launch a script named ZuluDeltaNiner.scpt on login.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.paul.applescript_Launchd</string>
   <key>ProgramArguments</key>
   <array>
       <string>osascript</string>
       <string>/ZuluDeltaNiner.scpt</string>
   </array>
   <key>QueueDirectories</key>
   <array/>
   <key>RunAtLoad</key>
   <true/>
   <key>StartOnMount</key>
   <false/>
   <key>WatchPaths</key>
   <array/>
</dict>
</plist>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.