Jump to content
rodrigobdz

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

Recommended Posts

13 hours ago, rodrigobdz said:

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.

 

My apologies.  If you compare my two versions, you can see an omission in the second (unintentional) that is the cause of the error you're experiencing.  The line in question is

set T to every tab of every window whose URL is www

which should be, as per the script above it:

set _T to a reference to (every tab of every window whose URL is www)

and then change T to _T as it occurs in the subsequent lines; I like to mark my variables that are stored by reference with an underscore, so I don't lose track of it.  Once an AppleScript filtered list reference gets dereferenced to a standard list object, the way it gets handled changes at a fundamental level.

 

My basic rule-of-thumb, in these cases where one is building up clauses of filtered lists, is to try and store them as references until you the moment you need to evaluate a property collection or do something with an item in that list.  Doing so will probably dereference it implicitly, or you can explicitly deference it by writing

contents of _T

The rest should work as it is now, including when no empty tabs are open (which is where count _T = 0 and null is returned).

 

As you stated, @rodrigobdz, filter-whose clauses are much more elegant, and ought to be a lot faster too than a repeat loop.  I, personally, would go so far as to say I loathe repeat loops, and do my best to avoid them (at least in AppleScript; they're fine in other languages).  Of course, there's no rational basis for my feelings on this whatsoever, and it's largely a stylistic choice (except in cases where the speed benefits are dramatic, and those cases do exist).

Share this post


Link to post
36 minutes ago, CJK said:

which should be, as per the script above it:

 

Could you post the complete, working code? It would be clearer than prose and individual lines, and more useful.

Share this post


Link to post
1 hour ago, deanishe said:

 

Could you post the complete, working code? It would be clearer than prose and individual lines, and more useful.

 

Yes, of course.

 

(30 minutes later of frustrating edits...)

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

This one above returns both window and tab id numbers, should you need to target them in that manner, for example, to bring a window to the front, then switch the active tab.

to lookupTabWithURL(www)
	local www
	
	tell application "Google Chrome"
		set _T to a reference 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

This one just returns the reference to the tab object, e.g. tab id 45 of window id 2088.  This can be used to target the identified tab and manipulate its properties or close it, for example.  It currently only returns the first matched tab, as that was the objective of the original script upon which this handler was based, largely as a demonstration on the different scripting methods.

Share this post


Link to post
2 hours ago, deanishe said:

Quick question: What does this line do? set [W] to _W

 

If _W is a list, then it sets W to item 1 of that list.

 

It follows from the way one can assign multiple values to multiple variables in one go:

    set [a, b, c, d] to [1, 2, 3, 4]

which, as you might expect, assigns 1 to a, 2 to b, 3 to c, and 4 to d.

 

If the number of variables supplied is fewer than the number of data objects to store, the variables are filled in order, and the remaining (excess) data values are discarded.  So,

    set [a, b] to [1, 2, 3, 4]

assigns 1 to a, 2 to b, whilst the and the are ignored.

 

Therefore,

    set [T] to every tab in the front window 

will get a list of every tab, and assign the first one to T.  It works with nesting as well, so something like:

    set [T] to every tab of every window

assigns a list of tabs to T, but if you just want the first item from that list:

    set [[T]] to every tab of every window

will get you a single tab assigned to T.

 

P.S.  Or perhaps your confusion was because I utilise the square brackets for lists much more than other people, who typically favour braces.  For the most part, they're interchangeable as long as opening and closing sets match.  However, when coercing a list to a string, square brackets ignore text item delimiters completely, which can be very useful.  Square brackets can't be used for records objects, although list items in a record can use them freely.

Share this post


Link to post
15 minutes ago, CJK said:

it sets W to item 1 of that list

 

Thanks. I wasn't familiar with that idiom. Now the script makes a whole lot more sense.

Share this post


Link to post
Posted (edited)
2 hours ago, rodrigobdz said:

@CJK What is the purpose of declaring www as local variable in the method?

 

In that script as it is, it serves no specific purpose.  But, whenever I write an AppleScript handler, I tend to declare all of its parameters immediately on the opening few lines as local, really as more of a "good coding practice" ethos than anything else.  The only time is necessary to do this is when you have a global property or variable declared elsewhere in your script with the same name.  If, then, you declare a handler that takes a parameter also by that name and you don't declare your parameter as local, you'll end up retrieving or overwriting the global value instead of the value passed to your handler.

 

Edited by CJK

Share this post


Link to post
7 minutes ago, rodrigobdz said:

Currently there is no version in the making. There are no open issues or feature requests on alfred-chrome-incognito-workflow.

@Gevor What would you like to see in a new version of this workflow?

Then what is the most recent version of this workflow? It's just as far as I know all those issues that were mentioned above with incorrect behaviour  of Incognito have not been solved. All of those code lines don't really mean much to a non-coding population of this forum 😂

 

Was there a fix that I missed? Mine workflow says v.1.1.0

Share this post


Link to post

Let me save everyone a 97MB slow download of a 1-minute video that could easily have been a 3-second GIF, that could easily have just been some words: when launching Chrome in incognito mode via the workflow being discussed in this thread, it does, indeed, open an incognito Chrome window, but also appears to open a normal Chrome window behind it.

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
×