2

I have this code I can keep running inside Script Editor. But it causes my laptops fans to scream! So I am not sure is this right way to do this?

repeat until application "Google Chrome" is not running
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "facebook.com") to "twitter.com"
        end tell
    end if
end repeat

Other question is that why my code isn't working? I would like to use this for redirecting from spesific urls to another. But even this implementation doesn't work.. If I open facebook.com it doesn't redirect to twitter.com.

Any help is appreciated.

NOTE: YES, I want to use applescript for this so focus on it, pls :)

lehtu
  • 868
  • 1
  • 12
  • 27
  • 1
    Regarding the fans: your code is very expensive, because it's sending permanently Apple Events to Chrome. An applet with `on idle()` handler which is called once per second might be a better solution. – vadian Oct 31 '15 at 16:08
  • Thanks for you too! :) – lehtu Nov 01 '15 at 11:06

1 Answers1

2

When an application appears to work, but doesn’t seem to be matching its conditions, it is always helpful to look at the actual values being checked. In this case, open the URL you think should be matched, and then get that URL to look at.

tell application "Google Chrome"
    get URL of tab 1 of window 1
end tell

When I entered “Facebook.com” in a new window in Chrome, and then ran the above script, what I found was https://www.facebook.com/?_rdr=p.

When I replaced “Facebook.com” in your code sample with https://www.facebook.com/?_rdr=p, the script now had an affect; it redirected to Chrome’s about page.

Taking a cue from the necessity of using the full URL in the condition, I switched “twitter.com” to “http://twitter.com/”.

The script now performs as I understand you intended it to: every tab in every window that matches https://www.facebook.com/?_rdr=p gets redirected to twitter.com.

repeat until application "Google Chrome" is not running
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
        end tell
    end if
end repeat

Applescript and Google Chrome are matching your conditional’s text against the full URL, so your conditional has to use the full URL as well.

@vadian’s note about using an idle handler is a very good one. See this Hourly Pop-up Alert question for ideas on using an idle handler. Something like this should work:

on idle
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
        end tell
    end if

    --number of seconds to wait for the next check
    return 1
end idle

Save as application, stay open after run handler.

Community
  • 1
  • 1
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • Thank you so much! Worked like a charm :) In fact I found out that with this condition it does it for root domain ```where URL contains "facebook.com"``` – lehtu Nov 01 '15 at 11:06