Skip to content

Instantly share code, notes, and snippets.

@itissid
Created March 15, 2018 17:01
Show Gist options
  • Save itissid/0725fc55ae837dcb0b40ce1b65511d50 to your computer and use it in GitHub Desktop.
Save itissid/0725fc55ae837dcb0b40ce1b65511d50 to your computer and use it in GitHub Desktop.
Open a file in chrome
on run argv
set targetTabName to quoted form of (item 1 of argv)
set targetFullPath to (item 2 of argv)
tell application "Google Chrome"
--Variables
set targetWindow to 0
set found to false
set targetTab to 0
--Repeat for Every Window
repeat with theWindow in every window
set targetWindow to theWindow
set theTabIndex to 0
--Repeat for Every Tab in Current Window
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
set targetTab to theTab
--Get Tab Name & URL
set tabName to title of theTab
set tabURL to URL of theTab
--log tabName
--log tabURL
--log theTabIndex
if ((quoted form of (contents of tabName) as string) is equal to targetTabName) then
set found to true
--log "***Found the tab with URL:" & tabURL & " tab number " & theTabIndex
exit repeat
end if
end repeat
if found then
exit repeat
end if
end repeat
if found then
-- REFRESH THE TAB
--log "."
activate window with targetWindow
tell theTab
reload
set active tab index of targetWindow to theTabIndex
end tell
--log ".."
else
--log "Did not find an open tab named: " & targetTabName & ". Opening it"
--open a tab in the browser
--log targetFullPath
open location targetFullPath
activate
end if
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment