Trying to create endless loop macro (Repeat or For Each action?) to switch between custom categories in Gmail

Hello,

I am developing a system to use KBM to facilitate gmail workflow in chrome, and against all odds, the results are quite good. I will be posting my gmail project on this site as soon as I iron out a few problems.

In lieu of multiple inboxes or google dictated categories (updates, promotions, etc), I create my own categories using gmail search syntax.

In Gmail/Chrome I can access the search field using / and then type my search string, then Enter the result being equivalent to a custom category.

For example, let's take the 3 search strings below:

1- from:(NYTimes OR WSJ OR Sigma OR ScientificAmerican) in:inbox

2- from:(NCBI OR NIH or Anderson) in:inbox

3- from:(Tony OR Robert OR George) in:inbox

I can easily write a macro to type / (which puts the cursor in the search box) → paste a search string → Enter to trigger the corresponding search in the inbox, and just as easily write 3 macros: one for each search string.

What I would like to achieve is some way to create an endless loop (using the Repeat or For Each actions ?) whereby I would type a hotkey like Shift-Cmd-8 which would trigger search 1, then search 2, then 3, then back to 1 , 2, 3 etc (I would click the hotkey each time)

thanks in advance for your time and help

I think this should do the trick. Remember the Switch/Case action we recently covered in a different thread? Here's another way you can use it that lets you do everything you describe in a single macro:

Switch Gmail Categories.kmmacros (4.7 KB)

As you can see, the switch checks a variable to know which search string to use, and the subsequent action after inputting the string changes the variable each time it's run, looping from 1 to 2 to 3 to 1 again. It should be clear how it works in practice.

You'll also notice I used the "Set Chrome Field to to Text" action instead of typing / and pasting, as it's both quicker and more reliable. You may need to change the action to use Chrome instead of Brave Browser if you don't use the latter, and you may also need to adjust the path for the field used in case this one doesn't work. Fortunately, the former is easily done by clicking here:

and the latter by clicking here:

1 Like

Thanks very much Gabe for the macro and the explanations.

I reviewed our previous discussion on the same topic. It takes me a while to digest !

1- I used the Chrome Set Field. Everything works except that the set field action only places the text in the search field but does not trigger the search. I tried to add an Enter keystroke action after the set field action to no avail because the cursor is not located in the set field action.

In the meantime, I replaced the set field action with a rather primitive approach which does the following in sequence:
a- type / which puts the cursor in the gmail search box
b- insert by pasting the search string and puts the cursor at the end of the line
c- type Enter

My solution works, but I would prefer yours which is more elegant if I can find a solution to the Enter (trigger search) issue. I try to emulate the habits of the greats of this forum.

2-thank you for the explanations about set browser field. Be it fields or radio buttons, do you have any way to determine which field or radio button as per ScreenCapture below (2) corresponds to what on the screen, or is it via trial and error ?
By using fields, would you be able for example to click the checkbox as per (3). I am asking for didactic purposes, not because I actually want to write a macro to check that box (which can be accessed with a simple shortcut: * → a
I sometimes use forms with a gazillion fields and have a lot of difficulty identifying the proper radio buttons to press..

3- I have a question about macro groups and palettes. Normally a group or palette is available in some or all applications. Is it conceivable to configure a palette to only be available in a specific Chrome (or Safari) tab like Gmail, Google Calendar ?

thanks very much Gabe

IME, automating a web page is most often best done using JavaScript in Browser.
This can be very easy, or very hard, depending on the actual web page.
To analyze, I would need the URL of the web page of interest.

Having said that, taking a quicklook at my gmail page, I found this works for me:

var searchElem = document.querySelector('input[aria-label="Search mail"]');
searchElem.value = 'subject:(update primary email address)';

var searchBtn = document.querySelector('button[aria-label="Search Mail"]');
searchBtn.click();

You can change the "update primary email address" in this line:
searchElem.value = 'subject:(update primary email address)'
to be whatever Subject text you would like to search for.

You can try this using Execute a JavaScript in Front Browser action.

I have NOT tested this in KM, but it worked fine for me in the Chrome JavaScript console.

Of course, there are more Search fields you can enter:

If you'd like more extensive development, let me know.

  1. I was able to have KM submit the search with this action, which programmatically clicks the Search button:

image

Here's an updated version of the macro that uses it:

Switch Gmail Categories.kmmacros (6.0 KB)

  1. If it's not clear which field or radio button you want KM to manipulate from the dropdown menu I showed you in my last post, the easiest way I've found is to right click, choose Inspect (or Inspect Element, if you do this in Safari), then right click on the highlighed area in the Inspector window that corresponds to the element in question and choose Copy > Copy Xpath. That's how I determined that yes, it is possible to click the checkbox you show in (3). That said, the querySelector JavaScript method @JMichaelTX talked about is almost certainly more robust, but unfortunately, I still know very little about JavaScript myself, don't yet know how to use it to isolate and manipulate specific elements like I have with Xpath. I do hope to learn at some point, but I haven't been able to do so just yet.

  2. Yes, it is. The best way I've found requires making a new macro group, and it depends on unique text being present in any page on the site, but assuming the latter exits, it's really quite easy. To make one for Gmail, for example, you would just need to set up the group like this:

42%20PM

1 Like

thank you very much @gglick and @JMichaelTX . I notice a bizarre behaviour. You use Brave, I use Chrome which is fine.

In the Chrome Field to text action, when I click the gear → Chrome , the other drop down list which shows the links remains Safari which I rarely if ever use. I can see the chrome links, but I keep getting a notification about turning on javascript in safari.

Would you have any idea what is going on ?

@gglick @JMichaelTX
I found the solution. It seems that you can't just change to Chrome with the gear. Safari remains unchanged. So I inserted a new Chrome Field to Text and all is fine.
Perhaps a bug ?
thanks very much

I'm guessing it has something to do with the fact that, when you don't have a "custom" browser installed on your Mac, and you import a macro or macro action that references one, KM defaults to Safari for these actions. The easiest thing to do may just be the solution you found, i.e. insert a new action, but it does seem strange that the action looked like it had Chrome properly selected and still insisted on defaulting to Safari; maybe it is a bug in that case.

At any rate, did our posts answer your latest questions sufficiently? Does the new version of my macro work? Have you been able to create a macro group whose macros only work on certain sites?

1 Like

the answer is that the macro works perfectly and I have learned a lot with your explanation about chrome fields. I have already written a few macros today thanks to you explanation. It's now crystal clear.
thanks VERY much !

1 Like

@ronald and @gglick,

Looks like @gglick has provided a good solution for when using gmail.

For future reference, I would make one caution: I have noticed that often some web pages use dynamic HTML classes and ids. This means that syntax like this:

image

can fail for other users, or even the same user upon later viewing of the same web page because the id="gs_lc50" has changed to some other code, maybe something like id="gt_lc50".

I thought Google used to use this dynamic approach with gmail, but perhaps they changed or I misremembered. :wink:

But for now, I'm glad it is working for you.

2 Likes

@gglick @JMichaelTX
With you help, I am now proficient at using Links and Xpath.

It has made me realize that it is necessary to use 2 browsers because I found multiple web pages for which it was impossible to identify the clickable link with Chrome but straightforward with Safari. In addition, Safari has more clickable buttons like print, and other advantages, like a better reading mode.

So the question is how to use a KBM macro so Cmd-Click on a Link opens in Chrome and Cmd-Opt Click opens it in Safari, with perhaps the option that if I press the shift key the link is opened in the background.

thanks in advance for your time and help

I have a feeling this might have been better suited for its own topic, but that aside, I'm glad to hear your proficiency has advanced.

To answer your latest question, the current version of KM has no way of using Cmd-Click for a trigger with Apple input devices, i.e. the Magic Mouse or Magic Trackpad, but if you use a third-party peripheral, you can do so with the USB Device Key trigger. If you do use an Apple input device, and have access to BetterTouchTool, then you can use a BetterTouchTool "normal mouse" trigger that runs a KM macro, like so:

As for the macro in question, this should get you started:

Open Link in Alternate Browser.kmmacros (4.3 KB)

This way, Cmd-Clicking on a link in one browser will open the link in the other, and vice versa, which I think might be simpler than Cmd-Click for one and Cmd-Opt-Click for another (unless you're also using a third browser like Firefox, but in that case, the JavaScript needed for this macro won't work anyway, so it's a moot point). As for Shift-Cmd-Clicking to open in the background, that's an option you can set with KM's Open URL action's gear menu:

So I'll leave that as an exercise for you to figure out how to make that work. If you use a third-party peripheral, you could try adding new cases to the Switch/Case action in this macro that choose different actions depending on whether Shift is present in the %TriggerValue% token or not, or, if you use BTT with Apple peripherals, you could duplicate this macro to make a new one to use with a BTT Shift-Cmd-Click trigger.

1 Like

thank you very much ! I will print out this page, work through it and give you an update

1 Like

I'm sorry Gabe:

  • I'm unable to locate LeftClick in the list of BTT triggers (I have a magic trackpad, so I do have a device)
  • I want to create another version of the macro whereby if I am in Mail, Cmd-Click will open the link in Chrome and Cmd-Opt-Click will open it in Safari. Is this realistic (just replace application contains with Mail?). Before going ahead, I just want to make sure that this makes sense.
    thank you

For the first question, hopefully this will clear it up:

Here's the page in BTT's documentation for a bit more info: Assigning Actions to Buttons

For Mail, the same kind of macro won't work because as far as I know Mail doesn't let you execute JavaScripts, so you have to automate the right-click context menu. And in that case, the best solution I've found for that actually involves foregoing KM entirely and doing the whole thing in BTT, using the "Trigger Context Menu Item" action. Here's an example of what it looks like:

Incidentally, just a thought, but assuming you have Chrome set to your default browser, do you really need two macros, since regular clicking on a link in Mail will open it in Chrome by default? If Chrome is your default, then I would just make a Cmd-Click one for opening the link in Safari.

1 Like

BTT: thank you
Very good point: just consider one browser in addition to the default.
thank you so much !

1 Like

yes, but never in the background. You explained that the background option is available in KBM

I see. In that case, you don't need a macro or a BTT action, as Cmd-Clicking in Mail already opens the link in your default browser in the background.

1 Like

I think that there is a misunderstanding. When I changed by default browser to safari, links in mail now open in the background, which did not happen when chrome was the default.
I spent the last hour trying to understand why there is no 'Open With ...' in my mail context menu. I checked the services menu, forums on the Internet, etc
thanks

thanks very much for the BTT mouse trick. I have used it in 5 macros already today.

1 Like