Skip to content

Instantly share code, notes, and snippets.

@constgen
Forked from bastienrobert/README.md
Created December 25, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save constgen/b4c82844224f12ed25c4ae4b74bb745c to your computer and use it in GitHub Desktop.
Save constgen/b4c82844224f12ed25c4ae4b74bb745c to your computer and use it in GitHub Desktop.
Script to open chrome with specific URL gived in params

OpenChrome

Inspired by Create React App

This script is checking in every chrome window if there's a tab with the URL gived in params:

  • If there's one: It opens chrome in first-ground and show the good chrome window with the good tab
  • If there's multiples: Same behavior but it use the first tab found
  • If there's not: It launch chrome (if it's not open) and create a new tab with the given URL

Utilisation

osascript openChrome.scpt YOUR_FORMATTED_URL

Exemple

osascript openChrome.scpt https://google.com

-- Check if app is running
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
on run argv
-- Check if chrome is running
set chromeRunning to is_running("Google Chrome")
-- Set URL to the once gived in arguments
set lookupUrl to item 1 of argv
-- Engine
if chromeRunning then
tell application "Google Chrome"
set i to 0
set j to 0
repeat with w in (windows)
set j to j + 1
repeat with t in (tabs of w)
set i to i + 1
if (t's URL as string) contains lookupUrl then
tell t to reload
tell w to activate
set (active tab index of window j) to i
return
end if
end repeat
end repeat
tell application "Google Chrome"
activate
open location lookupUrl
end tell
end tell
else
tell application "Google Chrome"
activate
open location lookupUrl
end tell
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment