Skip to content

Instantly share code, notes, and snippets.

@brighid
Created June 4, 2016 01:14
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 brighid/08f07b3b6733506dfdf7b393521c7406 to your computer and use it in GitHub Desktop.
Save brighid/08f07b3b6733506dfdf7b393521c7406 to your computer and use it in GitHub Desktop.
A recipe for opening arbitrary links in Chrome, in an incognito window, without blowing away your existing Chrome session. License, if you care: your choice of MIT, BSDv2+, or GPLv2+.
-- 1: Open Script Editor, make a new script, and paste this whole file in.
-- 2: Save the script as an app.
-- 3: Open the app's Contents/Info.plist
-- 4: Add the following to the top-level <dict> of the plist:
-- <key>CFBundleURLTypes</key>
-- <array>
-- <dict>
-- <key>CFBundleURLName</key>
-- <string>Web site URL</string>
-- <key>CFBundleURLSchemes</key>
-- <array>
-- <string>http</string>
-- <string>https</string>
-- </array>
-- </dict>
-- </array>
-- 5: Save that (it tells the system "this app can handle URLs")
-- 6: Run lsregister to tell the system to notice that your app can handle URLs:
-- /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /path/to/your.app
-- 7: Open System Preferences, go to Security & Privacy, go to the Privacy tab,
-- then go to the Accessibility list in that tab, then add the app you just
-- made and check the box next to it. If you tweak the script, you'll
-- probably need to uncheck the box and check it again, which is vaguely
-- understandable in a security context but it's weird that it doesn't
-- uncheck itself.
-- 8: Right-click on the app and select Open: this will raise the "hey, this is
-- an unsigned app, are you sure you want to open it?" dialogue from the
-- system. If you try to just run the app from somewhere else, it won't
-- necessarily ask you this question: it might just silently fail.
--
-- Using this app as your default browser would be kinda cumbersome. My usage
-- is, I use it as one of my browsers for Choosy https://www.choosyosx.com/ and
-- Alfred https://www.alfredapp.com/ because being able to add "open the
-- following URL in Chrome, in an incognito window" to those tools is very,
-- very helpful.
on run (argv)
qklog("Running from command line...")
if (count of argv) > 0 then
set maybe_url to (item 1 of argv)
else
set maybe_url to ""
end if
open_in_chrome(maybe_url)
end run
on open location my_url
qklog("Running from open location event...")
open_in_chrome(my_url)
end open location
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
on qklog(thing)
-- Uncomment the do-shell-script line if the script is broken and you're
-- trying to debug it (in which event, good fucking luck, you'll need it).
-- It's just a quick hacky print statement that slaps strings onto the end
-- of a file with a timestamp. Try tail -f ~/Downloads/applescript_log.txt
-- to watch it.
-- do shell script "echo $(date '+[%Y-%b-%d %H:%M]') " & quoted form of thing & " >> " & quoted form of "~/Downloads/applescript_log.txt"
end qklog
on open_in_chrome(my_url)
qklog("URL we're looking at: '" & my_url & "'.")
if not is_running("Google Chrome") then
do shell script "open -a /Applications/Google\\ Chrome.app"
-- wait for Chrome to finish starting up, otherwise the window we want,
-- won't necessarily end up in front. :(
delay 0.6
end if
tell application "Google Chrome"
set opened_url to false
repeat with w in (windows)
if mode of w is "incognito" then
my qklog("Found an existing incognito window ...")
set index of w to 1
tell application "System Events" to tell process "Google Chrome"
my qklog("Raising existing incognito window ...")
perform action "AXRaise" of window 1
end tell
activate
if my_url is not equal to "" then
my qklog("Checking whether we already opened " & my_url & " ...")
if not opened_url then
my qklog("Trying to make a new tab ...")
make new tab at end of tabs of w with properties {URL:my_url}
set opened_url to true
end if
end if
activate
return
end if
end repeat
if not opened_url then
my qklog("Trying to make a new incognito window ...")
tell application "Google Chrome"
set incog_window to make new window with properties {mode:"incognito"}
set index of incog_window to 1
tell application "System Events" to tell process "Google Chrome"
perform action "AXRaise" of window 1
end tell
if my_url is not equal to "" then
set URL of active tab of incog_window to my_url
end if
activate
end tell
end if
end tell
end open_in_chrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment