0

I'm trying to find a way to automate bringing a web browser to the front (ie - give it focus, make it visible on the screen) based on an event that happens in a web page. I have full control over the webpage, so I can make it do whatever I need to in order to trigger this.

I need to have several apps open with a web page in the background which is monitoring some video cameras. When a video camera has motion, I need the web browser window to come to the front so it's visible instead of whatever app was being used.

I was thinking something similar to what this guy was trying to do : How to bring application to front every 15 minutes on Mac OS X Lion?

I was thinking of using AppleScript to run in a continuous loop, and once a second check the open page in Safari, scan for some change and then send "activate" command to Safari to bring it to the front. Is this how I should go about this, or is there a better way?

1 Answers1

1

I'd probably just use launchd or cron instead of running the script as a background process.

  1. Save a script like this somewhere in AppleScript Editor:

    try
        tell application "Safari"
            tell document 1 where name starts with "Webcam" to do JavaScript "--"
            if result is "--" then activate
        end tell
    end
    
  2. Save a property list like this ~/Library/LaunchAgents/com.superuser.443513.plist

    <?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>com.superuser.443513</string>
        <key>ProgramArguments</key>
        <array>
            <string>osascript</string>
            <string>/Users/username/Library/Scripts/script.scpt</string>
        </array>
        <key>StartInterval</key>
        <integer>5</integer> <!-- every 5 seconds -->
    </dict>
    </plist>
    
  3. launchctl load ~/Library/LaunchAgents/com.superuser.443513.plist


Could you just use curl to check the condition? You could run it every other minute by editing the crontab with EDITOR=nano crontab -e and adding a line like this:

*/2 * * * * [[ -n "$(curl -L http://superuser.com/questions/443513 | grep automate)" ]] && open http://example.com

Lri
  • 40,894
  • 7
  • 119
  • 157