Automator: Applescript - Open a specific Google Chrome profile and use extension
I've been messing around but can't figure this out. Is it even possible?
I want to automate a task where it will:
-
Open a specific Google Chrome profile.
-
Run a specific command from an extension.
I tried playing around with this script
set myURLs to {"https://www.google.com", ¬
"https://www.news.google.com", ¬
"https://apple.stackexchange.com"}
set myProfiles to {"Default", "Profile 1"}
repeat with aProfile in myProfiles
do shell script "open -na 'Google Chrome' --args --profile-directory=" & aProfile's quoted form
delay 1
tell application "Google Chrome"
activate
tell front window
set URL of active tab to first item of myURLs
delay 0.5
repeat with i from 2 to count of myURLs
make new tab at after (get active tab) with properties {URL:item i of myURLs}
delay 0.5
end repeat
set active tab index to 1
end tell
end tell
delay 1
end repeat
I can't seem to find a way though to make it just open a chrome profile.
any tips?
thank you
====
Update:
Anyone? I've been trying with this script:
tell application "Google Chrome"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Google Chrome"
tell group "Extensions"
tell button "Layout Manager"
--click
perform action "A"
end tell
end tell
end tell
end tell
It gives me a script error (System Events got an error: Can’t get group "Extensions" of process "Google Chrome".)
"Layout Manager" is the extension name and "A" is the action I want to enable.
thanks
===
Still messing around but I can't even open the specific profile. I used:
activate application "Google Chrome"
tell application "System Events"
tell process "Google Chrome"
click menu item "Profile" of menu 1 of menu bar item "People" of menu bar 1
end tell
open location "http://example.com"
open location "http://anotherexample.com"
end tell
But it gives me: "System Events got an error: Can’t get menu bar item "People" of menu bar 1 of process "Google Chrome"."
Anyone?
===
Edit:
So I managed to run the Chrome profile. Still trying to run the extension function.
Extension name: Multi Layout Manager
Link: https://chrome.google.com/webstore/detail/multi-layout-manager/ijpiffheldgmdkbaohoilabdnmeinand?hl=en
With it you can save various browser setups (tabs, position, size) and apply them whenever you want. I want the script to click on the extension and then click on a setup (Setup A, Setup B, etc.) The extension is pinned.
I'm trying out this code I found but my knowledge is limited
tell application "Google Chrome"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Google Chrome"
tell group "Extensions"
tell button "Multi Layout Manager"
click
--perform action "Setup A"
end tell
end tell
end tell
end tell
I get the following error message: "System Events got an error: Can’t get group "Extensions" of process "Google Chrome"."
It seems to me that I can't show it which button to click.
I'm still running HS (10.13.6) and Chrome Version 92.0.4515.159 (Official Build) (x86_64)
PS: When you respond, do I edit the main question or post an answer? Sorry, I'm new here.
Solution 1:
The is not meant to answer everything asked in your question and is just to give you some guidance on the items mentioned herein.
The first block of code in your question comes from my answer A: Use Apple Script to open Chrome with specific profile and it does indeed do exactly what it is programed to do, assuming there are two profiles.
You should have a look at my answer A: Create a terminal command to open another user profile in chrome as it shows the basic command to open Google Chrome to a specific profile. That command is also used in the do shell script
command in the code of the other answer, the code you show in the question.
So, to open Google Chrome to a specific profile the code is there, right in front of you:
do shell script "open -na 'Google Chrome' --args --profile-directory=" & aProfile's quoted form
Where aProfile's quoted form
is in the list {"Default", "Profile 1"}
, so to just target one profile, e.g.,:
set myProfile to "Profile 1"
do shell script "open -na 'Google Chrome' --args --profile-directory=" & myProfile's quoted form
As to some of your other code attempts, here is an example of how I'd click a menu item from the Profiles menu in Google Chrome.
Example AppleScript code:
tell application "Google Chrome" to activate
delay 0.25
tell application "System Events" to ¬
click ¬
menu item "Edit…" of ¬
menu "Profiles" of ¬
menu bar item "Profiles" of ¬
menu bar 1 of ¬
application process "Google Chrome"
In the example AppleScript code you'd replace "Edit…
with then the name of the profile you want, as it shows on the Profiles menu.
To bring up the Extensions is would be:
Example AppleScript code:
tell application "Google Chrome" to activate
delay 0.25
tell application "System Events" to ¬
click ¬
menu item "Extensions" of ¬
menu "Window" of ¬
menu bar item "Window" of ¬
menu bar 1 of ¬
application process "Google Chrome"
Not trying to overwhelm you, however, if you need to click several first level menu items in the same script, it's probably better to implement it as a handler, e.g,:
Example AppleScript code:
my clickApplicationMenuCommand("Google Chrome", "Profiles", "Edit…")
my clickApplicationMenuCommand("Google Chrome", "Window", "Extensions")
-- ## Handler ##
to clickApplicationMenuCommand(appName, appMenuName, appMenuCommand)
tell application appName to activate
delay 0.25
tell application "System Events" to ¬
click menu item appMenuCommand of ¬
menu appMenuName of menu bar item appMenuName of ¬
menu bar 1 of application process appName
end clickApplicationMenuCommand
You would place the handler at the bottom of your script and then just click a top level menu item as shown in the two examples of calling the handler.
Update to address Multi Layout Manager extension
The information above was tested in macOS Catalina as it was not know at the time what version of macOS the OP was using.
This update to address triggering a Layout of the Multi Layout Manager extension in Google Chrome when the extension itself is pinned to the Toolbar.
Having installed the Multi Layout Manager extension in Google Chrome under macOS High Sierra and ascertaining the UI element hierarchical object structure to e.g. Setup A and upon closer examination of it, I was not able to get past a certain point in it. As such I decided to stop wasting time and revert to the an ugly form of UI Scripting.
Note that UI Scripting is often kludgy and prone to failure for a number of different reasons, which I'm not going to enumerate here.
The following example AppleScript code assumes Google Chrome is already running and the wanted Profile is active.
Example AppleScript code:
tell application "Google Chrome" to activate
delay 0.25
tell application "System Events"
click ¬
pop up button "Multi Layout Manager" of ¬
toolbar 1 of group 1 of window 1 of ¬
application process "Chrome"
delay 0.25
repeat 3 times
key code 48 -- # tab key
delay 0.2
end repeat
key code 36 -- # enter key
end tell
Notes:
As coded with the repeat
loop, it triggered the first Layout, so to trigger the second Layout the repeat
loop would need to be incremented by 1
, and so on.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.