MACRO: Activate, Reload, or Open Browser Tab, v4.0

PURPOSE

This macro will open a URL in a Safari, Arc, Chrome, Brave, Edge, or Vivaldi tab. If the URL is already open in a tab, then the tab will be selected. The macro includes some advanced features which are discussed below.

The macro must be triggered and the browser information must be supplied via the macro %TriggerValue%. This can be done from another macro using the Execute a Macro action or another macOS application via AppleScript (e.g., Alfred and Bunch) using this template:

-- Activate, Reload, or Open Browser Tab

property theP : "(browser information)"

tell application "Keyboard Maestro Engine"

  do script "F993DABB-8E10-41C1-9CA7-97CA74E6F9F0" with parameter theP

end tell

This macro can be run in three modes depending on the format of the browser information.

Mode 1: The browser information is specified as a single URL. For example:

https://forum.keyboardmaestro.com/latest

Mode 2: The browser information is specified as a single character for the browser (s Safari, a Arc, c Chrome, b Brave, e Edge, v Vivaldi), a colon, and a URL. For example

a:https://forum.keyboardmaestro.com/latest

Mode 3: The browser information is specified as JSON.

{

  "Browser": "Safari|Arc|Chrome|Brave|Edge|Vivaldi",

  "Url": "https://forum.keyboardmaestro.com/latest",

  "UrlPattern": "https://forum\\.keyboardmaestro\\.com.*",

  "ReplaceMatchedUrl": 0|1,

  "ReloadTab": 0|1,

  "NewWindowIfNotFound": 0|1

}

"Url" is the only required key.

Note: In JSON, double backslashes (\\) are required to escape characters in "UrlPattern".

Default values for the other keys:

"Browser": local_Browser_default

"UrlPattern": the value of "Url"

"ReplaceMatchedUrl": local_ReplaceMatchedUrl_default

"ReloadTab": local_ReloadTab_default

"NewWindowIfNotFound": local_NewWindowIfNotFound_default

If an existing browser tab matches "UrlPattern" (using Regular Expressions), then it will be activated; otherwise a new tab will be opened to "Url".

If "ReplaceMatchedUrl" is 1, then an activated tab will be changed to "Url". Otherwise, the "Url" will not be changed, however, the page will be reloaded if "ReloadTab" is 1.

If a matching tab is not found, then a new browser tab ("NewWindowIfNotFound" = 0) or a new browser window ("NewWindowIfNotFound" = 1) will be opened.

SETTINGS

local_Browser_default : the browser (Safari|Arc|Chrome|Brave|Edge|Vivaldi) that will be used if it no specified. The default is Safari.

local_ReplaceMatchedUrl_default : 0|1

local_ReloadTab_default : 0|1

local_NewWindowIfNotFound_default : 0|1

IMPLEMENTATION NOTES

This macro could be easily modified to accommodate any browser that includes the AppleScript Safari suit or Chromium Suite. For more information refer to the AppleScript code within this macro.

ACKNOWLEDGEMENTS

a) This macro was inspired by Open or Refresh a URL in a Tab (Sub-Macro) by @DanThomas. Activate, Reload, or Open Browser Tab includes some overlapping features.

b) This macro uses the AppleScript Select First Action of Current Macro by @ccstone.

TESTED WITH

• Keyboard Maestro 11.0.2
• Sonoma 14.3.1 (23D60)/MacBookPro18,2
• Sonoma 14.3.1 (23D60)/VariableMacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

( expand / collapse )

1.0 initial version

2.0
a) Added support for the Vivaldi browser which includes the AppleScript Chromium Suite.

b) Added an error message if the "Browser" is improperly specified.

c) When changing JSON in a Set Variable to Text action, the KM Editor sometimes automatically converts dumb double quotes to smart double quotes. This can easily go undetected by a user. Thus, this macro now converts all smart double quotes to dumb double quotes.

d) In this header comment, "UrlPattern" is now shown as "https://forum\\.keyboardmaestro\\.com.*" with the dots properly escaped with two backslashes (\\.) because two backslashes are required in text within JSON.

e) The Keyboard Maestro Engine converts \\ to \ in text that is passed using With Parameter within the Execute a Macro action. Thus, this macro converts all \ to \\ to add back the second \ so that the JSON remains valid within this macro.

f) BUG FIX: When "ReplaceMatchedUrl" = 1 and "ReloadTab" = 0, the webpage will no longer be refreshed when the browser tab URL exactly matches "Url".

3.0
a) Added an error message if the calling macro uses Mode 2 (JSON), but the "Url" is not specified or the JSON includes a syntax error.

b) Improved the error message if this macro is run directly or the Execute a Macro action does not include a With Parameter value.

c) BUG FIX: When "UrlPattern" is not specified, the macro sets it to "Url". It now properly escapes all special regex characters when setting "UrlPattern".

3.1
a) In browser AppleScripts, changed references to window 1 to front window. The window 1 reference sometimes caused an error for some users.

b) BUG FIX: The embedded browser AppleScripts scan the open tabs in each window. While searching for a match with the specified "Url", the scripts remove a trailing slash in each tab URL. The macro now similarly removes a trailing slash from the specified "Url".

3.2 BUG FIX: The Brave Browser AppleScript had an extraneous character at the end that prevented proper execution.

4.0
a) Added support for Arc.

b) Added a third trigger Mode: x:URL, where x is a single character (case insensitive) used to specify the browser, i.e., the first letter of one of the supported browsers: Safari, Arc, Chrome, Brave, Edge, or Vivaldi

c) Added the settings local_Browser_default, local_ReplaceMatchedUrl_default, local_ReloadTab_default, and local_NewWindowIfNotFound_default.


Download: Activate- Reload- or Open Browser Tab .kmmacros (97 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.3.1 (23D60)
  • Keyboard Maestro v11.0.2

Activate, Reload, or Open Browser Tab (Caller–Example 1)

Download: Activate- Reload- or Open Browser Tab (Caller–Example 1).kmmacros (4.1 KB)

Macro-Image


Activate, Reload, or Open Browser Tab (Caller–Example 2)

Download: Activate- Reload- or Open Browser Tab (Caller–Example 2).kmmacros (4.2 KB)

Macro-Image


Activate, Reload, or Open Browser Tab (Caller–Example 3)

Download: Activate- Reload- or Open Browser Tab (Caller–Example 3).kmmacros (7.3 KB)

Macro-Image


Activate, Reload, or Open Browser Tab (Caller–Example 4)

Activate, Reload, or Open Browser Tab (Caller–Example 4).scpt.zip (8.1 KB)

AppleScript ( expand / collapse )
-- Activate, Reload, or Open Browser Tab (Caller–Example 4), v4.0
-- revised: 2024-03-13, Jim Sauer

-- PURPOSE
-- This script provides an example call to macro 'Activate, Reload, 
-- or Open Browser Tab' using Mode 2, i.e., the browser information 
-- is specified as JSON.

property theP : "{ 
	\"Browser\": \"Chrome\", 
	\"Url\": \"https://forum.keyboardmaestro.com/latest\", 
	\"UrlPattern\": \"https://forum\\.keyboardmaestro\\.com.*\", 
	\"ReplaceMatchedUrl\": 1, 
	\"ReloadTab\": 0, 
	\"NewWindowIfNotFound\": 1
} "

-- {
--		"Browser": "Safari|Arc|Chrome|Brave|Edge|Vivaldi",
--		"Url": "https://forum.keyboardmaestro.com/latest",
--		"UrlPattern": "https://forum\\.keyboardmaestro\\.com.*",
--		"ReplaceMatchedUrl": 0|1,
--		"ReloadTab": 0|1,
--		"NewWindowIfNotFound": 0|1
-- }
--
-- "Url" is the only required key.
-- 
-- Note: In JSON, double backslashes (\\) are required to escape 
-- characters in "UrlPattern".
-- 
-- Default values for the other keys:
-- 
-- 	"Browser": local_Browser_default
-- 
-- 	"UrlPattern": the value of "Url"
-- 
-- 	"ReplaceMatchedUrl": local_ReplaceMatchedUrl_default
-- 	
-- 	"ReloadTab": local_ReloadTab_default
-- 
-- 	"NewWindowIfNotFound": local_NewWindowIfNotFound_default
-- 
-- If an existing browser tab matches "UrlPattern" (using Regular 
-- Expressions), then it will be activated; otherwise a new tab 
-- will be opened to "Url". 
-- 
-- If "ReplaceMatchedUrl" is 1, then an activated tab will be 
-- changed to "Url". Otherwise, the "Url" will not be changed, 
-- however, the page will be reloaded if "ReloadTab" is 1.
-- 
-- If a matching tab is not found, then a new browser tab 
-- ("NewWindowIfNotFound" = 0) or a new browser window 
-- ("NewWindowIfNotFound" = 1) will be opened.

tell application "Keyboard Maestro Engine"
	do script "F993DABB-8E10-41C1-9CA7-97CA74E6F9F0" with parameter theP
end tell

4 Likes

Hello Jim (@_jims)

You’ve done great work on this one - thanks for this Macro.

Here are some thoughts on adding more features …

  • the change of the NewWindowIfNotFound Key to a string based one instead of integer based with a new Name NewWindowOrTabIfNotFound with the options Tab | Window | Private Window

  • an additional Key ReopenInOtherBrowserIfFound supporting all browsers with closing tab in current browser before opening in the target one

  • an additional Key ReopenInPrivateWindow with closing current Tab beforehand

  • adding Orion Browser and Vivaldi and maybe Arc (if it has AppleScript support) to the supported browsers list

I hope to see these enhancements in the next Version for this macro because I think they‘ll make it even more powerful in versatility.

Greetings from Germany

Tobias

1 Like

Hi, @Nr.5-need_input. Thanks for the positive comment. Glad you like the macro.

Thanks for your suggestions. Unfortunately, at least at this time, I'll only be able to implement one of your suggestions. More below...

If I'm interpreting the above correctly, you are suggesting features that would require the macro to check the tabs for all open browsers. This adds a great deal of complication that would undoubtedly decrease performance. For me, this added functionality would not be worth that cost.

With that said, you are more than welcome to create a fork of the macro that would include this enhancement.

The macro can be easily adapted to add browsers that:

  1. support AppleScript, and

  2. include the Safari suite or the Chromium Suite. (Yes, the former is oddly lowercase suite.)

Of the three browsers suggested, only Vivaldi qualifies. In the next version of the macro, I'll add support for it.

I've been experimenting with Arc and find it very intriguing. Since the creators tout "Arc is the Chrome Replacement" I was surprised and disappointed that it did not include the Chromium Suite. We can hope that it will be added later.

I've updated the OP with Version 2.0 of Activate, Reload, or Open Browser Tab:

  • Added support for the Vivaldi browser which includes the AppleScript Chromium Suite.

  • Added an error message if the "Browser" is improperly specified.

  • When changing JSON in a Set Variable to Text action, the KM Editor sometimes automatically converts dumb double quotes to smart double quotes. This can easily go undetected by a user. Thus, this macro now converts all smart double quotes to dumb double quotes.

  • In this header comment, "UrlPattern" is now shown as "https://forum\\.keyboardmaestro\\.com.\*" with the dots properly escaped with two backslashes (\\.) because two backslashes are required in text within JSON.

  • The Keyboard Maestro Engine converts \\ to \ in text that is passed using With Parameter within the Execute a Macro action. Thus, this macro converts all \ to \\ to add back the second \ so that the JSON remains valid within this macro.

  • BUG FIX: When "ReplaceMatchedUrl" = 1 and "ReloadTab" = 0, the webpage will no longer be refreshed when the browser tab URL exactly matches "Url".

1 Like

Hello Jim

First of all - thanks for Vivaldi support !!

No, I’m sorry to say that - you’re interpreting that wrong …

The Key change from NewWindowIfNotFound to NewWindowOrTabIfNotFound with the options Tab | Window | Private Window I suggested is to make it more clear to the user what is used for the scope of searching - it only adds Private windows to the Browser defined - not to all Browsers. I’ve thought about requesting this but I didn’t.

The Key ReopenInPrivateWindow was requested to complement the first Key to open the matching URL in the Private Window if it is set to 1 and closing the tab in the normal window when found in a Normal and a Private window or otherwise. Maybe therefore I would recommend to use ReopenInPrivateWindowIfFound for a better understanding.

The Key ReopenInOtherBrowserIfFound supporting all browsers with closing tab in current browser before opening in the target one - it is again only thought for the defined Browser in the use of the search scope … not all supported browsers. The support for all Browsers in the list is just for Moving to one of them like so:

  • Tab is in normal window of Safari and moves to Vivaldi with closing in Safari beforehand.

  • Tab is in Private window of Chrome and moves to a Private window in Edge with closing the tab in Chrome’s Private window beforehand.

No search for duplicates in the target Browser.

I hope my request makes more sense to you now… As I requested those features yesterday I was in a hurry … I am trying getting better in asking for features…

While my English is still quite good after nearly 20 years being out of school - it’s still not German - the language I am speaking and writing in for 70% of my day - every day

Greetings from Germany

Tobias

1 Like

I've become a big fan of Arc. It has greatly changed (for the better) my workflow when interacting with the web. It takes a bit getting used to but I love it. As you noted @_jims, it is missing support for features found in other Chromium browsers. I have not had luck in getting javascript to execute on a specific tab in Arc. I've tried to adapt KBM examples in the forum that execute javascript on a specific tab but have not had any success.

Hopefully Arc will add full Chromium someday.

1 Like

Hello,
I just put put up a question for @ccstone responding to his comment where he addressed the same use case. I am trying to figure out a way to do this in brave. That original post seems to be 3 yrs old. Realised you have a post for the same use case that was uploaded recently. Glad to have bumped into it.

I tried your macro with caller example 1 because I need to be able to edit target URL easily. Other caller examples sound like I'll have to edit the URLs at multiple places and inside a code.
I am using Brave.
I had the default URL (Keyboard Maestro Discourse) in the caller. I had the same URL open in Brave. But tab with the URL was not active. Some other tab was active.

When i ran the macro, it gives either of the two outcomes.

  1. It opens the URL in safari (Safari app was not open)
  2. It opens a new tab with the URL in Brave (A tab with the URL was already open in Brave. Just not active.)

How do we resolve this?

What I am looking for:
I need to open a URL. If the URL is already open in some tab of brave browser, the tab with that URL should become active and get the brave browser in the front.
I also use Microsoft edge beta, Google Chrome and Google Chrome beta frequently. If you have a solution for these browsers as well, that would be wonderful.
At a time, I'd be only focused on only one browser for a given URL. (I don't have a use case where I have to check all the browsers with a tab with given URL),

Hi, @fluid_eye. Since Safari is the default browser, you need to use Mode 2 and specify Brave as the Browser. Here's an example:

Download: Activate- Reload- or Open Browser Tab (Caller–Example 2-@fuild_eye).kmmacros (4.7 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.4.1 (22F770820d)
  • Keyboard Maestro v10.2

Change the Browser to Edge and Chrome, respectively. Browsers supported are listed in the OP and in the macro header comment.

1 Like

Thanks it works perfect.

I am getting this error msg though. Does this error show by default?

Screenshot 2023-09-06 at 10.03.37

This error occurs only when a tab with given URL already exists.

I also just realised, it turns the focus towards the tab if it contains the URL but macro doesn't execute further actions.
For example, if my macro includes 'insert text' action when it lands on the tab with the URL, it lands on the tab (because it exists with the URL), shows the error and doesn't do the action of text insertion.

Also, I just realised, the macro works for some URLs and not others.
URLs for which it works:

URLs for which it doesn't work:
https://www.google.co.uk/search?q=facebook

When I say 'it does not work', I mean, it doesn't detect the tab with the URL. Instead of focusing on the tab, it creates new tab with the URL. (I only checked this with Brave browser)

Hi, @fluid_eye. Thanks for bringing this issue to my attention; it will be addressed in Version 3.0.

1 Like

I've updated the OP with Version 3.0 of Activate, Reload, or Open Browser Tab:

  • Added an error message if the calling macro uses Mode 2 (JSON), but the "Url" is not specified or the JSON includes a syntax error.

  • Improved the error message if this macro is run directly or the Execute a Macro action does not include a With Parameter value.

  • BUG FIX: When "UrlPattern" is not specified, the macro sets it to "Url". It now properly escapes all special regex characters when setting "UrlPattern".

1 Like

I've updated the OP with Version 3.1 of Activate, Reload, or Open Browser Tab:

  • In browser AppleScripts, changed references to window 1 to front window. The window 1 reference sometimes caused an error for some users.

  • BUG FIX: The embedded browser AppleScripts scan the open tabs in each window. While searching for a match with the specified "Url", the scripts remove a trailing slash in each tab URL. The macro now similarly removes a trailing slash from the specified "Url".

Thanks to @fluid_eye for reporting these issues!

1 Like

Just to educate me @_jims, Arc Browser is advertised as being based on Chromium. I gather that Chromium Suite is something additional? There is an Applescript dictionary but my attempts at converting scripts from other browsers have pretty much failed. It would really be nice to have this macro (and others) working for Arc.

Hi, @cyoungers. Correct, it's additional and since The Browser Company is developing Arc for Windows, they have their hands full.

Before I knew of their Windows effort, I did reach out to TBC to ask about the Chromium Suite, but thus far have not heard back. With that said, each time I update Arc, I do use Script Debugger to search. But as of Arc, Version 1.8.1 (41651), no dice. :frowning:

In contrast, here's Brave.

1 Like

Hello jim, I was wondering if it is possible for you to add support for 'Google Chrome beta'. I have no idea how difficult/easy it is given that the macro already supports chrome but it was worth asking once to you as Google Chrome beta is the browser I use frequently.

Hi, @fluid_eye. I'm not sure. I've never used the beta. Does the beta appear differently otherwise to Keyboard Maestro (e.g., activating the app, etc.)?

If it appears the same, then Activate, Reload, or Open Browser Tab should work it in the same manner as any other version of Chrome.

If it's different, then please share any other macros that you have that work with it. That might give me some clues. (I really don't want to download the beta.)


I'm traveling for a week, thus I probably won't be able to help much for several days.

Thanks for your prompt response Jim.

I don't think so. At the same time, I never really used a macro on beta.

Let me try replacing the text string 'Google Chrome' with 'Google Chrome Beta' everywhere (in subroutine, in caller, in apple script etc). Am I thinking in the right direction?

I had tried replacing 'Safari' with 'Google Chrome Beta' in only the apple script action and it gave me syntax error. I don't know any programming language.

If it is really difficult, seems like the option that i'd resort to is using 'search tabs' option from the menu.
I also tried using 'if/then' action to perform action based on window titles


but KM somehow do not check all open windows of 'Google Chrome beta' (I tried same action for safari. KM doesn't detect safari windows either.)

That definitely wouldn’t work because Chrome and Safari have different AppleScript dictionaries.

That might work, but I have no experience with the beta.


Sorry I’m away from my computer for
10 days so I really can’t help much until then.

1 Like

I've updated the OP with Version 3.2 of Activate, Reload, or Open Browser Tab:

  • BUG FIX: The Brave Browser AppleScript had an extraneous character at the end that prevented proper execution.