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-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)
Activate, Reload, or Open Browser Tab (Caller–Example 2)
Download: Activate- Reload- or Open Browser Tab (Caller–Example 2).kmmacros (4.2 KB)
Activate, Reload, or Open Browser Tab (Caller–Example 3)
Download: Activate- Reload- or Open Browser Tab (Caller–Example 3).kmmacros (7.3 KB)
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