Jump to content

Recommended Posts

Hi all,

 

New Alfred user here. I wanted a way to open a new tab, new window, and new incognito window from alfred, but couldn't find an existing workflow for new tab so I hacked one together from the 'new window' workflow I found. And I figured someone might be interested in having all 3 in a single place.

 

Here's a zip of the workflows for anyone interested:

 

https://dl.dropboxusercontent.com/u/29173129/Google%20Chrome%20Workflow%20Kit%20for%20Alfred.zip

 

And here is the applescript I used for each:

 

New Chrome Window

on alfred_script(q)
tell application "/Applications/Google Chrome.app"
     make new window
     activate
end tell
end alfred_script

New Chrome Tab

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"
else
     tell application "Google Chrome"
     activate
     tell application "System Events" to keystroke "t" using {command down}
end tell
end if

New Incognito Window

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"
else
     tell application "Google Chrome"
     activate
     tell application "System Events" to keystroke "n" using {command down, shift down}
end tell
end if
Edited by avayl

Share this post


Link to post

Telling System Events to perform keystrokes should be avoided when possible, as it can produce undesirable results. In this case, you don’t need it at all, since Chrome has good AppleScript support.

To open a new tab, instead of

tell application "Google Chrome"
activate
tell application "System Events" to keystroke "t" using {command down}

you can simply do

tell application "Google Chrome" to make new tab at end of tabs of front window

To make a new incognito window, instead of

tell application "Google Chrome"
activate
tell application "System Events" to keystroke "n" using {command down, shift down}

you can use

tell application "Google Chrome" to make new window with properties {mode:"incognito"}

These methods are both faster and less prone to unforeseen results.

You also do not need the on alfred_script(q) and end alfred_script bits. You can launch these scripts with a Run Script using /usr/bin/osacript as the language.

On the “New Chrome Window” script you do not need to specify the full path to Chrome, you can just call it “Google Chrome”, like on the others.

Finally, keep in mind you do not need to wrap code in a tellend tell block when giving a single command (though it’s not harmful, either). For example, you could write tell application "System Events" to set myList to (name of every process), and it would have the same effect, with less verbosity (which makes it easier to read later).

 

Hope some of these will prove useful.

Share this post


Link to post

I combined avayl's original post with Vítor's suggestions, cleaned up a few of the calls to Google Chrome (my installation was trying to start Chrome in parallels, which is weird), and came up with the following:

 

New Chrome Window

on alfred_script(q)
tell application "/Applications/Google Chrome.app"
     make new window
     activate
end tell
end alfred_script

New Chrome Tab

on alfred_script(q)
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 '/Applications/Google Chrome.app' --new --args"
else
	tell application "/Applications/Google Chrome.app" to make new tab at end of tabs of front window
end if
end alfred_script

New Chrome Incognito Window

on alfred_script(q)
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 '/Applications/Google Chrome.app' --new --args --incognito"
else
	tell application "/Applications/Google Chrome.app" to make new window with properties {mode:"incognito"}
end if
end alfred_script

Share this post


Link to post

Hi! avayl and Hi! Vitor

 

Can u give me a AppleScript to switch Google chrome user by selecting one ?

 

thanks in advance

 

I was actually just looking into this, and it looks like there is no applescript support for user switching in chrome at this time, and so it would have to be done via UI scripting, which also means that it would be incredibly idiosyncratic. Unless I'm missing something. So I'd say that this functionality is probably something that cannot be done at this time.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...