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 trying to execute a script when a server becomes available (a bit like when the OS detect the presence of a time capsule device and it starts the backup.) I didn't find a simple way to attach an applescript to an system event. Is there a way?

I am not looking for anything that requires a compiler. I am simply looking at attaching to an OS event (a bit like "Folder Actions" I guess, but for a server.)

share|improve this question
    
Cross-posted to SU. –  Nathan Greenstein Jun 21 '11 at 13:27
add comment

migrated from stackoverflow.com Jun 21 '11 at 12:52

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 1 down vote accepted

The only way I can think of is run an script with an idle handler that periodically checks to see if the server is available and if so executes another script or another portion of the same script.

share|improve this answer
    
since i didn't get any better answer, this is a work around I will accept as an answer. –  machunter Jul 3 '11 at 8:05
add comment

You can use launchd for that purpose. Here is an example of a launchd config plist that will execute an AppleScript when a disk is mounted:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>Example</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/osascript</string>
            <string>/Users/sakra/Documents/Test.applescript</string>
        </array>
        <key>StartOnMount</key>
        <true/>
    </dict>
</plist>

To activate the config plist save it to the LaunchAgents folder in your Library folder as Example.plist.

From the Terminal you can then use the command launchctl to activate Example.plist by running:

launchctl load ~/Library/LaunchAgents/Example.plist

The AppleScript will then be run each time a filesystem is mounted.

To deactivate it, run:

launchctl unload ~/Library/LaunchAgents/Example.plist
share|improve this answer
    
I guess this answers my question partially. The StartOnMount option specifies the script to run when a disk is mounted. What I really want is my script to launch when a server is available on the network. I will look into launchd's man pages see if there is anything I can use. –  machunter Jun 19 '11 at 19:58
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.