Jump to content
rodrigobdz

Workflow: Chrome Incognito - Open Google Chrome in Incognito mode.

Recommended Posts

Posted (edited)

 

Open Google Chrome in Incognito mode. If already open, it'll open a new Incognito window.

Requirements

  • Alfred
  • Alfred Powerpack
  • Google Chrome
  • Assistive access enabled in System Preferences > Universal Access

Installation

  1. Download the .alfredworkflow file
  2. Double click to install

Usage

  • incognito - Opens an incognito window with Google.

Usage incognito without query

  • incognito {query} - Opens an incognito window and searches for query.

Usage incognito without query

 

Links

Credits

This workflow is based on the following two workflows:

Edited by rodrigobdz
Missing punctuation

Share this post


Link to post
Posted (edited)

Hi @rodrigobdz

 

I am testing your workflow and every time I use it (by typing "incognito"), it first opens one new tab in the already opened non incognito mode window, then one new incognito window.

 

When I type incognito + "query" it it first opens one new tab in the already opened non incognito mode window  and searches for "query", then one new incognito window.

 

- Google Chrome v66.0.3359.181

- Mac OS X 10.12.6

Edited by politicus

Share this post


Link to post
On 5/20/2018 at 5:21 PM, rodrigobdz said:

@politicus I just released v1.0.1. Please let me know if you find another bug.

 

I have the same problem as Politicus on 1.0.1. Every time I use this workflow with Chrome closed I get two windows of Chrome opened, one of them being in incognito mode.

Share this post


Link to post
Posted (edited)
On 6/2/2018 at 12:27 AM, Gevor said:

 

I have the same problem as Politicus on 1.0.1. Every time I use this workflow with Chrome closed I get two windows of Chrome opened, one of them being in incognito mode.

@Gevor I cannot reproduce the error on 1.0.1 anymore.

Edited by rodrigobdz

Share this post


Link to post

@rodrigobdz 

I am sorry, my bad. I checked again and the behaviour of the workflow is not really how I described it. It does open one window of Chrome and it is in incognito mode. But for some reason it also opens Safari window. I took it for Chrome when I first tried the workflow.

Share this post


Link to post
Posted (edited)

Looking at the source code, I can see why these users are experiencing these issues.  I downloaded the workflow and had a look at the AppleScript, which has quite a few issues with it.  @rodrigobdz, I note from your original post that Assistive Access is required to run this workflow.  Writing an AppleScript to achieve what you want here—namely opening a Chrome window/tab—shouldn't need to employ any accessibility features via System Events.

 

Here's your code that I copied from the workflow:

tell application "System Events"
	set myList to (name of every process)
end tell
if (myList contains "Google Chrome") is false then
	do shell script "open -a Google\\ Chrome --new --args -incognito"
	open location "https://google.com/search?q={query}"
else
	tell application "Google Chrome"
		activate
		tell application "System Events" to keystroke "n" using {command down, shift down}
		open location "https://google.com/search?q={query}"
	end tell
end if

Google Chrome is scriptable, therefore you don't need to access System Events processes to determine whether or not Chrome is running.  Scriptable applications have a property called running that returns true or false depending on whether or not Chrome is running.  You can access it as with any other property, like so:

set isOpen to the running of application "Google Chrome"
if isOpen then ...

But, for this particular property (and a couple of others that return boolean values for the state of an application, another one being the property frontmost), you can use this very natural-language syntax:

if the application "Google Chrome" is running then ...

 Following this, the reason the users are reporting two windows opening up—one of which might even be Safari instead of Chrome—is because you have one command telling a shell script to open Chrome in incognito mode (bearing in mind that launching a web browser usually opens a blank window); and you have a second command telling AppleScript to open location and this will always happen in a new window (that makes two windows), and it won't necessarily be Chrome. I believe it uses the system's default browser, which explains why there's a user reporting that your workflow opened Safari and Chrome for him (it did with me as well).

 

Basically, don't use open location when you want to target a specific web browser.  You could have used the same shell command that launches Chrome to get it opening a desired URL in the first window:

do shell script "open -a 'Google Chrome' 'https://imdb.com' --args -incognito"

And, finally, for the last part of your script, you've once again elected to use System Events to issue a keypress that activates a shortcut to a menu item as a means to open an incognito window.  That's a bad method.  But, immediately afterwards, you use open location again, intending for the URL to open in the window you've just created.  For some users, it might do just that; but for others, it won't.

 

Basically, don't use open location, and don't use System Events for its accessibility hooks when you don't need to.  It's always a method of last resort, because it's very unreliable, and poor coding practice.  Do, however, use System Events for things like file and folder handling.

 

Anyway, here's how you first check to see if an incognito window is already open, then open a URL in a new tab in the existing window instead of creating a new window altogether, or, if an incognito window doesn't yet exist, opening a new URL in a new incognito window:

tell application "Google Chrome"
	set W to every window whose mode is "incognito"
	if W = {} then set W to {make new window with properties {mode:"incognito"}}
	
	set [W] to W
	
	tell W to set T to make new tab with properties {URL:"{query}"}
	close (every tab of every window whose URL is "chrome://newtab/")
	
	set index of W to 1
	
	activate
end tell

In fact, on my system, that works regardless of whether or not Chrome is running, negating the need for all the bits that came before.  Thus, that ends up being the entire script.

 

I hope this helps you out, and helps resolve the issues your users were having.

 

 

Edited by CJK

Share this post


Link to post
Posted (edited)
4 hours ago, CJK said:

I believe it uses the system's default browser

 

That is correct.

 

One additional tip: Don't use {query}. It's hacky and finicky. Use "with input as argv" instead, and add a top-level on run argv handler to your AppleScript:

on run argv
   set theQuery to item 1 of argv
   ...
   ...
end run
Edited by deanishe

Share this post


Link to post
2 hours ago, deanishe said:

One additional tip: Don't use {query}. It's hacky and finicky. Use "with input as argv" instead, and add a top-level on run argv handler to your AppleScript:

 

Yes, good point.  Thanks for adding that. ??

Share this post


Link to post
3 hours ago, gandalfsaxe said:

Is there a way to customize whether it uses Safari or Chrome?

Currently there is no such option but I will consider your request for a future release.

Share this post


Link to post
3 hours ago, gandalfsaxe said:

Is there a way to customize whether it uses Safari or Chrome?

 

Not in a good way: Safari's AppleScript dictionary doesn't support private mode.

Share this post


Link to post
Posted (edited)

@CJK I implemented the suggested changes. However, the script fails to close empty tabs. I would like to know why.

close (every tab of every window whose URL is "chrome://newtab/")

I found that create-react-app from Facebook includes a script for finding tabs with a given URL. Just for reference, here is the link.

Edited by rodrigobdz

Share this post


Link to post
9 minutes ago, rodrigobdz said:

I would like to know why.

 

Works on my machine:

tell application "Google Chrome" to close (every tab of every window whose URL is "chrome://newtab/")

Share this post


Link to post

@deanishe That works but in my script it doesn't.

 

on run argv
	tell application "Google Chrome"
		set W to every window whose mode is "incognito"
		if W = {} then set W to {make new window with properties {mode:"incognito"}}
		set [W] to W

		set userQuery to item 1 of argv
		set incognitoQuery to "https://www.google.com/search?q=" & userQuery
		tell W to set T to make new tab with properties {URL:incognitoQuery}
		close (every tab of every window whose URL contains "chrome://newtab/")

		set index of W to 1

		activate
	end tell
end run

 

Share this post


Link to post

For me, it works sporadically. The first time, it works correctly. It doesn't work again until you close the incognito window or open a new tab in it.

Share this post


Link to post
Posted (edited)

@rodrigobdz, you could try inserting a modest delay before that line, i.e.

delay 0.2
close (every tab of every window whose URL contains "chrome://newtab/"

but I'm unable to recreate the problem on my machine, which probably indicates the delay might help other machines.

 

P.S. Thanks for including the GitHub link.  That was very considerate and it's helpful when people share their research in this manner.  I actually don't like the way he wrote his lookupTabWithUrl() handler because of it's unnecessary use of repeat loops; I can't think of a specific reason to do it that way.  He's basically iterating through every window, and then iterating through every tab in each window, and testing whether each tab's URL matches the argument passed to the handler (he exits on first match).  So, this is identical in result to (but slower to execute than) this:

to lookupTabWithURL(www)
	local www

	tell application "Google Chrome"
		set _W to a reference to (every window whose URL of tabs contains www)
		
		if (count _W) = 0 then return null
		set [W] to _W
		
		set T to the first tab of W whose URL is www
		
		return {wID:id of W, tID:id of T}
	end tell
end lookupTabWithURL

which could be condensed somewhat further if you only need a reference to the tab (more likely) and not the individual tab and window IDs:

to lookupTabWithURL(www)
	local www
	
	tell application "Google Chrome"
		set T to every tab of every window whose URL is www
		
		if (count T) = 0 then return null
		return item 1 of item 1 of T
	end tell
end lookupTabWithURL

 

Edited by CJK
Added some extra AppleScript insight

Share this post


Link to post
Posted (edited)
7 hours ago, CJK said:

 


to lookupTabWithURL(www)
	local www
	
	tell application "Google Chrome"
		set T to every tab of every window whose URL is www
		
		if (count T) = 0 then return null
		return item 1 of item 1 of T
	end tell
end lookupTabWithURL

 

Thanks for the lesson @CJK. I think there is a small error in getting the result's count by doing:

if (count T) = 0 then return null
-- Logging (count T) always returns (*2*)
-- Logging T returns (*,*) when no empty tabs are open.

Thus, throwing an error by accessing item 1 of item 1 when no empty tab is open.

 

It works with:

if (count item 1 of T) = 0 then return null

 

Edited by rodrigobdz
Add a proposed solution.

Share this post


Link to post

I think they're both broken, tbh.

 

The if (count T) = 0 ... line only protects against the case that no windows are open.

 

T is a list of lists, and it might look like this {{}, {}, {tab ...}, {}} or indeed this {{}, {}, {}, {}}.


There might be some shortcut via a whose clause (I'm no great shakes at AppleScript), but I would use repeat clauses personally.

Share this post


Link to post
Posted (edited)
On 6/24/2018 at 11:27 AM, deanishe said:

I think they're both broken, tbh.

 

The if (count T) = 0 ... line only protects against the case that no windows are open.

 

T is a list of lists, and it might look like this {{}, {}, {tab ...}, {}} or indeed this {{}, {}, {}, {}}.


There might be some shortcut via a whose clause (I'm no great shakes at AppleScript), but I would use repeat clauses personally.

There must be a way to write the condition with whose clauses. In my opinion, the condition with whose clauses look more elegant.

Edited by rodrigobdz

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×