-
-
Save mayoff/1138816 to your computer and use it in GitHub Desktop.
(* To the extent possible under law, Rob Mayoff has waived all copyright and related or neighboring rights to “AppleScript to make Google Chrome open/reload a URL”. This work is published from: United States. https://creativecommons.org/publicdomain/zero/1.0/ *) | |
tell application "Google Chrome" | |
activate | |
set theUrl to "http://tycho.usno.navy.mil/cgi-bin/timer.pl" | |
if (count every window) = 0 then | |
make new window | |
end if | |
set found to false | |
set theTabIndex to -1 | |
repeat with theWindow in every window | |
set theTabIndex to 0 | |
repeat with theTab in every tab of theWindow | |
set theTabIndex to theTabIndex + 1 | |
if theTab's URL = theUrl then | |
set found to true | |
exit | |
end if | |
end repeat | |
if found then | |
exit repeat | |
end if | |
end repeat | |
if found then | |
tell theTab to reload | |
set theWindow's active tab index to theTabIndex | |
set index of theWindow to 1 | |
else | |
tell window 1 to make new tab with properties {URL:theUrl} | |
end if | |
end tell |
I could not get this to work without explicit string comparison:
if (theTab's URL = theUrl as string) then
I had some trouble with this at first as I didn't realize that Chrome now seems to a trailing slash to it's URLS.
I was trying to reload http://localhost:4567
but the tab URL was actually http://localhost:4567/
so the comparison failed and I'd get a new tab every time. Maybe this is only happening as I'm using a URL with the port in it?
If you are wanting to reload the active tab, you can use.
tell application "Google Chrome" to reload active tab of window 1
I fixed the issue with comparing URLs, I made it take an input parameter as well.
on run {targetUrl}
tell application "Google Chrome"
activate
set theUrl to my remove_http(targetUrl)
if (count every window) = 0 then
make new window
end if
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
set theTabUrl to my remove_http(theTab's URL as string)
if (theTabUrl contains theUrl) then
set found to true
exit repeat
end if
end repeat
if found then
exit repeat
end if
end repeat
if found then
tell theTab to reload
set theWindow's active tab index to theTabIndex
set index of theWindow to 1
else
tell window 1 to make new tab with properties {URL:targetUrl}
end if
end tell
end run
on remove_http(input_url)
if (input_url contains "https://") then
return trim_line(input_url, "https://")
else
return trim_line(input_url, "http://")
end if
return input_url
end remove_http
-- Taken from: http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html --
on trim_line(this_text, trim_chars)
set x to the length of the trim_chars
-- TRIM BEGINNING
repeat while this_text begins with the trim_chars
try
set this_text to characters (x + 1) thru -1 of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
return this_text
end trim_line
to run it just use:
osascript name_of_script.applescript "http://google.com"
it is important to include the http:// or https://
@jamesraylittle do we have an equivalent (to make Google Chrome open/reload a URL)
for Windows?
thanks