The title of any web page is stored in a field named <title> in the source, and we can use this fact to get the title from any page in any browser by searching the returned text of the source.
Here's a little demo macro that activates Firefox, copies the URL, then downloads the source of that URL (temporarily), searches it for the <title> tag, and displays the results in a pop-up window (after saving it to a variable).
Download Macro(s): Get Firefox page title.kmmacros (5.5 KB)
Macro screenshot
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 15.7.1
- Keyboard Maestro v11.0.4
Run it, and get output like this:
The trickery in the shell script is the -0777 flag on the perl command. This forces Perl to treat the inbound text as one (massive) line, so that the search will pick up both of these formats:
<title>this is the page title>
<title>
this is the page title
</title>
I had an AI help me write this a while ago, for use in another macro. The two things it did that I couldn't figure out were the multi-line handling, and accounting for attributes on the title tag, which is done by the title[^>]*> piece of the regular expression.
Notice that there is probably still some post processing you'd want to do (depending on the source), and that's where things get interesting, because every site structures their titles differently.
In the case of the Keyboard Maestro forum, you'd (probably) want to strip out everything after the second hyphen from the end, as that's all forum-related stuff.
To handle this, I'd add a Case action, and then just cover the cases I know are special—here's a version of the macro that does that for the forums and for YouTube, for instance:
Download Macro(s): Get Firefox page title with post-processing.kmmacros (7.6 KB)
Macro screenshot
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 15.7.1
- Keyboard Maestro v11.0.4
Run that version on this forum page, and you get just the page title back:
You might think it'd be slow to grab and search the full page source, but it's not—on a very long web page, the shell script portion of the macro runs in just over a tenth of a second.
Working with the text from a page's source is a good way to work with browsers that don't do AppleScript, and to avoid having to do any manual selection in the GUI. Yes, you may have to do some post-processing, but that's a much simpler thing to do than trying to capture your selection, or otherwise manually interact with the page.
Hope this helps!
-rob.