AppleScript for incognito Chrome - shawfdong/hyades GitHub Wiki

My desktop is a Mid 2010, 27-inch iMac. Oftentimes I want to launch an incognito window of Google Chrome in a separate Space. Here is how to automate the process using AppleScript.

Enable Script menu in menu bar: Open the application AppleScript Editor, click Preferences..., then click the General tab. Check "Show Script menu in menu bar".

Install MouseTools: Unfortunately, as of Mavericks (OS X 10.9), the only way to make a new Space is by clicking the mouse. Fortunately, the tool MouseTools allows us to perform mouse clicks from command line or programmatically. Let's download the tool and save the extracted executable MouseTools in ~/bin.

Create the following AppleScript, save it as ~/Library/Scripts/incognito Chrome in new Space.scpt (so it will appear in the Script menu):

set MT to "/Users/dong/bin/MouseTools "

tell application "System Events" key code 101 -- F9 delay 0.5 tell current application to do shell script ¬ MT & "-x 2550 -y 30;" & "sleep 0.5;" & ¬ MT & "-leftClickNoRelease;" & ¬ MT & "-releaseMouse" key code 53 -- escape delay 0.5 key code 19 using control down -- control-2 delay 0.5 tell current application to do shell script ¬ "open -n -a 'Google Chrome' --args --new-window " &¬ "--profile-directory='Profile 1' --incognito " &¬ "http://example.com/" end tell

Annotations of incognito Chrome in new Space.scpt:

  • A list of key codes can be found in /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h.
  • We press F9 to launch Mission Control.
  • In Mission Control, we click the top-right corner of the screen to make a new Space. The screen resolution of my iMac is 2560 x 1440. You might want to adjust the values for your own Mac.
  • Press Esc to leave Mission Control
  • We want to launch incognito Chrome in Space 2. Press Control-2 to enter Space 2
  • I have 2 Chrome profiles. I want to start an incognito windows using my 2nd (non-default) profile. Chrome's dictionary shows no command to do this, so I use a shell script instead.
  • A short delay is necessary between key strokes, because those events can get out of order.
If you only have one Chrome profile, you can use the following AppleScript instead:
set MT to "/Users/dong/bin/MouseTools "

tell application "System Events"
	key code 101 -- F9
	delay 0.5
	tell current application to do shell script ¬
		MT & "-x 2550 -y 30;" & "sleep 0.5;" & ¬
		MT & "-leftClickNoRelease;" & ¬
		MT & "-releaseMouse"
	key code 53 -- escape
	delay 0.5
	key code 19 using control down -- control-2
	delay 0.5
	tell application "Google Chrome"
		make new window with properties {mode:"incognito"}
		set URL of active tab of result to "http://example.com/"
	end tell
end tell

If the Space already exists, we can use the following AppleScript to launch incognito Chrome in Space 2 (~/Library/Scripts/incognito Chrome in Space 2.scpt):

tell application "System Events"
	key code 19 using control down -- control-2
	delay 0.5
	tell current application
		do shell script "open -n -a 'Google Chrome' --args --new-window --profile-directory='Profile 1' --incognito http://example.com/"
	end tell
end tell

References

⚠️ **GitHub.com Fallback** ⚠️