(* 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 |
This comment has been minimized.
This comment has been minimized.
joprice
commented
Oct 20, 2012
I could not get this to work without explicit string comparison: if (theTab's URL = theUrl as string) then |
This comment has been minimized.
This comment has been minimized.
danomatika
commented
Feb 26, 2014
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 |
This comment has been minimized.
This comment has been minimized.
CraigWilliams
commented
Jan 4, 2015
If you are wanting to reload the active tab, you can use. tell application "Google Chrome" to reload active tab of window 1 |
This comment has been minimized.
This comment has been minimized.
jamesraylittle
commented
Jul 8, 2016
•
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:
it is important to include the http:// or https:// |
This comment has been minimized.
This comment has been minimized.
mohan-chinnappan-n
commented
Aug 31, 2017
@jamesraylittle do we have an equivalent (to make Google Chrome open/reload a URL) |
This comment has been minimized.
haqu commentedJul 11, 2012
thanks