Jump to content

Alfred Workflow hotkey to open new Chrome incognito is flaky


Recommended Posts

Hi,

 

For context, I am trying to use a hotkey to trigger a Workflow to open a new Chrome incognito tab (if none is open) and switch to a Chrome incognito tab if there is one. I have mapped it to Cmd + Option + N (though I don't think this matters). 

 

The issue would be more straightforward if this straight-up doesn't work. The weird thing is that it mostly works, then it stops working, but when I restart my laptop, it works again. In previous versions of macOS/Alfred, this has mostly worked, so this seems to be a new issue. 

 

Steps taken to debug and other relevant info:

- I have used ShortcutDetective and it shows that Alfred is the one receiving the shortcut

- On macOS 11.5.1 and Alfred 4.5

- See AppleScript code below (not sure if this might be relevant)

 

On a side note, I have recently experienced a similar issue with using caps to change my input source, i.e. it became flaky but works after restart. Not sure if related (Alfred wasn't involved in that). I'd prefer not to restart my laptop every few days. 

 

The relevant AppleScript triggered by the workflow, in case anyone is interested:

 

on alfred_script(q)
	tell application "Google Chrome"
		-- Open incognito window
		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

		-- Focus incognito window
		set index of W to 1
		activate

		set emptyTab to my lookupTabWithUrl("chrome://newtab/")
		if emptyTab is not null then
			close emptyTab
		end if

		-- Focus incognito window
		set index of W to 1
		activate

	end tell
end alfred_script

 

Link to comment

When asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re shooting in the dark. See the Reporting Problems with Workflows topic for an overview on building effective reports.

 

Based on the on alfred_script(q) you’re probably using a Run NSAppleScript. Don’t. It can be unreliable and you should only pick it if you know you need it. Use instead a Run Script with /usr/bin/osascript (AS) as the language.

Link to comment

My apologies. You are right I'm using `Run NSAppleScript`. How do I rewrite it using Run Script with /usr/bin/osascript (AS) as the language? 

 

My workflow is currently based on someone else's workflow from the forums. You can find it here.

 

Thank you very much! (It's currently working, but like I said, it stops working somewhat erratically.) 

Edited by newbie1234
Link to comment
  • 3 months later...

Hi, in case anyone comes across this, this code works properly. 

 

This is because the previous `activate` only makes the Incognito tab visible, but doesn't actually focus on it. 

 

I incorporated the code found here; do clickthrough for a more comprehensive explanation of what the code does. 

 

# Activates the specified window (w) of the specified AppleScriptable
# application (a).
    # Note that both parameters must be *objects*.
# Example: Activate the window that is currently the 2nd window in Chrome:
#   tell application "Google Chrome"
#       my activateWin(it, window 2)
#   end tell
on activateWin(a, w)
    tell application "System Events"
        click menu item (name of w) of menu 1 of menu bar item -2 ¬
                   of menu bar 1 of process (name of a)
    end tell
    activate a
end activateWin

on run argv
  tell application "Google Chrome"
		-- Open incognito window
		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

		-- Focus incognito window
    tell application "Google Chrome"
        activate
        set index of (every window whose mode is "incognito") to 1
    end tell
    my activateWin(it, window 1)
		if title of W does not contain "New Tab" then tell application "System Events" to keystroke "t" using command down

		#  set emptyTab to my lookupTabWithUrl("chrome://newtab/")
		#  if emptyTab is not null then
		# 	close emptyTab
		#  end if

		-- Focus incognito window
		# my activateWin(it, W)

	end tell
end run

 

Edited by newbie1234
Improve robustness of code for when more than 2 non-incognito windows are open
Link to comment
  • 2 weeks later...

@newbie1234 Could you please fill in your Powerpack email address in your forum profile before asking Powerpack-specific questions? This is only visible to me as administrator and allows us to confirm your registered address.

 

Any questions, just drop me a line.

 

Cheers,
Vero

Link to comment

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
×
×
  • Create New...