Open URL and Activate Google Chrome via Applescript

ATLChris picture ATLChris · Feb 28, 2014 · Viewed 36k times · Source

I have the following AppleScript that I wrote many years ago. I use this code to program buttons on my Harmony One universal remote to access online video services via Google Chrome. The code is not working. Google Chrome doesn't launch. I am running the code via RemoteBuddy. The code complies fine, but does not work.

Anyone have any thoughts on what might be the problem, or how I can improve the script to make it work?

tell application "System Events" to set open_applications to (name of everyprocess)
if (open_applications contains "Google Chrome") is true then
        tell application "Google Chrome" to quit
else
        tell application "Google Chrome"
                activate
                open location "http://xfinitytv.comcast.net"
        end tell
        delay 1
        tell application "Google Chrome" to activate
end if

Answer

user1804762 picture user1804762 · Mar 1, 2014

Try it this way:

tell application "Google Chrome"
    if it is running then
        quit
    else
        activate
        open location "http://xfinitytv.comcast.net"
        delay 1
        activate
    end if
end tell



Note: it's using the newer "Enhanced Application Model" (second line), more info here:
How to check in AppleScript if an app is running, without launching it - via osascript utility