Why does KM not get the CORRECT browser title?

As part of my well needed text macro I have the following either/ or condition:

If All Conditions Met
Application ‘Google Chrome’ is at the front
Execute the Following Actions:
Insert Text by Pasting


Otherwise, Execute the Following Actions:
Insert Text by Pasting


My question: Why does KM not understand that ”Otherwise” means Safari is at the front?

As it is now I get everything right (url, some text) except for I get the title from the other browser which is NOT at front!!

Should this be built in another way?

with best regards,
Omar KN
Stockholm, Sweden

%ChromeTitle% will always give the title of the front Chrome window.

%SafariTitle% will always give the title of the front Safari window.

The If you show will always execute the first actions if Google Chrome is at the front and the second actions if Google Chrome is not at the front.

It’s hard to debug what might be going wrong from what you have shown - any number of other things might be wrong.

Because "Otherwise" means any app OTHER THAN "Google Chrome".

If you want Safari to be frontmost, then you need to test for it and/or activate it.

I use a Switch or Case action (KM Wiki) to branch on Browsers:

You could put Actions in the "otherwise" block to handle non-browser apps being frontmost.

Yes there is some dawdling going on there.

Even with the recommended Switch or Case action, I still don't get the GoogleTitle, although in a precedent test with Safari, I got the SafariTitle.

Here is a screenshot:

And also the complete macro:

Send to all-touched-pages-projects 1 paragr. vs.2 Chr-Saf.kmmacros (7.3 KB)

(I don't know how to upload a macro and put it in an existing forum thread.)

with best regards,
Omar KN
Stockholm, Sweden

Hi Omar,

I'm afraid I don't have time at the moment to point out where I think you may have erred with your macro, but I was able to take the liberty of making some adjustments to it to help you achieve what I believe is your desired result in a hopefully simpler manner. Please try this out and see if it works any better.

Send to all-touched-pages-projects 1 paragr. vs.2 Chr-Saf.kmmacros (8.1 KB)

%Application%1% will presumably never be “Chrome” since the application name is “Google Chrome”.

I’d recommend using “contains” and “Google Chrome”.

Almost solved - so I thought!

Did that (contains G C), ok now - thank you @peternlewis .

Also thanks to @gglick for rewriting the macro.

At one point when copying from Safari, the copied text was correct but the URL & TITLE were from another SAFARI page behind the one I was currently using. I could reproduce this once, but thank God - the macro is working now as it should.

I’d like to share the macro with the community, where to upload it?

with best regards,
Omar KN
Stockholm, Sweden

Several times, and just now every time, when copying from Safari/ Chrome, the copied text was correct but the URL & TITLE were from another SAFARI/ Chrome page behind the one I was currently using. I could reproduce this.

When this happens with Chrome, Safari copy was 100% correct, but before when it happened with Safari, Chrome was 100% correct.

This happened even after restarting the KM engine and KM itself.

Now when I make sure that only 1 browser page is available everything is fine - obviously. Another browser tab or a newly opened secondary browser page does not matter.

So is this a cache problem somewhere?

Any assistance would be greatly appreciated!

With best regards,
Omar K N

I have not seen the issue/behavior you are reporting.

Maybe this macro would be of some help that I wrote back in 2015 that gets the Web Page Title and URL of the frontmost browser and it’s frontmost window:

Get the Browser Page Title & URL for Safari, Chrome, or FireFox

Share it to the Macro Library category by using the Share to Forum button at the top of the main window. Ensure you have the macro selected.

The actions are all based around AppleScript, along the lines of:

tell app "Safari" to tell window 1 to …

So if it is selecting the wrong window, it would only be because the system is selecting the wrong window.

I have never heard of this happening, so if it is happening to you I would suggest either something is corrupted in the windowing system (reboot), you are misunderstanding something (eg multiple displays, spaces, whatever), or there is some software installed on your Mac that is mucking with the windowing system and screwing things up.

Sorry if I did't clarify: the current update of the macro is solely based on KM modules, vs. 3,
essentially based on @gglick's rewrite:
(Send to all-touched-pages-projects + 1 paragr. vs.3 Chr-Saf)

It seems to be ok right now - after reboot.

Send to all-touched-pages-projects 1 paragr. vs.3 Chr-Saf.kmmacros (8.8 KB)

with best regards,
Omar KN
Stockholm, Sweden

Yes, the Keyboard Maestro tokens for SafariTitle etc are all based on AppleScript. It is essentially:

tell application "Safari" to tell document 1 to set r to do JavaScript "document.title"

Its more complicated that that because it deals with stuff like Safari not running, but it is essentially just asking Safari to do the job. There isn’t any scope for accidentally picking the wrong window - not on Keyboard Maestro’s end anyway, and it should be largely impossible on the other end too.

Glad my rewrite proved useful, and that your issues with unexpected URLs and titles seem to have been resolved!

Hey Folks,

Over and over again I’ve mentioned that various apps including Safari fail to properly understand the concept of FRONT WINDOW or WINDOW 1 IF the app has been hidden with “Hide <App-Name>” or “Hide Others”.

Google Chrome has the same problem.

This is an idiotic and horrible bug Apple has elected to NOT fix for about a decade…

The workaround goes like this:

tell application "System Events"
   tell application process "Safari"
      if visible is false then set visible to true
   end tell
end tell

While I’m not certain this issue is the crux of the problem with Omar’s macro, it has caused many users more than a little head-scratching time.


1 Like

Again this morning I have run into the same problem and it can be reproduced:

The text section copied from Safari or G Chrome is correct, but if there are more than 1 browser pages the URL and the TITLE are caught from another (hidden) page. (Not always - but right now.)
(There have been reboots in between).

So thanks to Chris I included the above AppleScript into the macro, but to no avail :frowning:
(See screendump below) - maybe not at right position?

It's probably not KM's fault.

So the page from which I'm copying is not hidden, it's on top, but what I get is the URL and the TITLE from the 2nd or 3rd page "under" my top page (hidden).

If all other, hidden pages are closed, the macro works ok.

The macro as it is now:

Send to all-touched-pages-projects 1 paragr. vs.3 Chr-Saf.kmmacros (10.3 KB)

I can't seem to reproduce your issue on my own system (10.12.4, KM 7.3.1) but I did think of a workaround that should avoid any possible issues caused by the current Safari or Chrome tab not being the frontmost window. Try this version out and see if it makes any difference.

Send to all-touched-pages-projects 1 paragr. vs.3 Chr-Saf.kmmacros (6.5 KB)


You’re using a variable ( tppLog ) for the top stripe (URL, TITLE etc.) and it works.

So far I have not been able to see anything fishy!

And I’m not complaining! :wink:

Thank you so far, :+1: :+1: :+1:

with best regards,
Omar KN
Stockholm, Sweden

1 Like

Hey Omar,

That script will do NOTHING with so-called “hidden” windows. It ONLY makes HIDDEN applications visible.


Any window that has been minimized or turned into a full-screen window is at the mercy of the system and may or may NOT be available to a normal scripted solution. You have to test to be sure of these things.

Because of Apple’s ham-handed handling of Spaces I rarely use them — particularly since they can interfere with scripting.

Rule-of-the-thumb — never, ever use the Clipboard when a more direct means of getting the selection is available.

Here’s now I’d handle your task.

Since you’re dealing with plain-text it’s relatively simple to gather all your required data using some AppleScript, some JavaScript – then massage the data a little and write it to a file with the Satimage.osax.

This method should be lightning-fast and very reliable.

NOTE — You the user need to set the variable logFilePath to the correct POSIX Path. If the target file does not exist the script will fail.

# Auth: Christopher Stone
# dCre: 2017/05/06 02:00
# dMod: 2017/05/07 08:23
# Appl: Google Chrome & Safari
# Task: Write Selected Text to a Log File with an Info-Header.
# Libs: None
# Osax: Satimage.osax --> http://tinyurl.com/satimage-osaxen
# Tags: @Applescript, @Script, @System_Events, @Write, @Selected, @Text, @Log, @File, @Info-Header, @Header
# Vers: 1.02

# Note: This script REQUIRES the Satimage.osax AppleScript Extension to be INSTALLED!!!!


   set logFilePath to "~/Documents-LC3/_loggar/my-touched-pages-IN.txt" -- POSIX-Path or Tilde-POSIX-Path
   tell application "System Events" to set logFilePath to POSIX path of disk item logFilePath
   set logDate to current date
   set logDate to strftime logDate into "logged tpp %m/%d/%Y %H:%M =="
   set pathToFrontAppHFS to path to frontmost application as text
   if pathToFrontAppHFS ends with "Google Chrome.app:" then
      set theApp to "Chrome"
      set documentTitle to doJavaScriptInChrome("document.title")
      set documentURL to doJavaScriptInChrome("document.URL")
      set selectedText to doJavaScriptInChrome("window.getSelection()+'';")
      if selectedText = "" then error "No text was selected in Chrome!"
      set selectedText to cng("\\A\\s+|\\s+\\Z", "", selectedText) of me
   else if pathToFrontAppHFS ends with "Safari.app:" then
      set theApp to "Safari"
      set documentTitle to doJavaScriptInSafari("document.title")
      set documentURL to doJavaScriptInSafari("document.URL")
      set selectedText to doJavaScriptInSafari("window.getSelection()+'';")
      if selectedText = "" then error "No text was selected in Safari!"
      set selectedText to cng("\\A\\s+|\\s+\\Z", "", selectedText) of me
      error "Google Chrome or Safari must be the frontmost app!"
   end if
   set logText to join {logDate, "URL: " & documentURL, documentTitle, "", selectedText, "", ""} using linefeed
   set outputText to logText & (readtext logFilePath)
   writetext outputText to logFilePath
   display notification "Log text written!" with title theApp subtitle "" sound name "Tink"
on error e number n
   stdErr(e, n, true, true) of me
end try

on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
on doJavaScriptInChrome(jsCMD)
      tell application "Google Chrome" to tell front window's active tab to execute javascript jsCMD
   on error e
      error "Error in handler doJavaScriptInChrome() of library NLb!" & return & return & e
   end try
end doJavaScriptInChrome
on doJavaScriptInSafari(jsCMD)
      tell application "Safari" to do JavaScript jsCMD in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
on stdErr(e, n, beepFlag, ddFlag)
   set e to e & return & return & "Num: " & n
   if beepFlag = true then
   end if
   if ddFlag = true then
      tell application (path to frontmost application as text)
         set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK" with icon 0
      end tell
      if button returned of dDlg = "Copy" then set the clipboard to e
      return e
   end if
end stdErr


Hi and good day Chris,

How does one know the Satimage.osax AppleScript Extension is correctly installed & functional?

I downloaded it ( XMLLib osax 3.7.0 (build 390) from http://www.satimage.fr/ .

When testing your script with Script Debugger

a.) there was the error: ”The variable documentURL is not defined.”

b.) At the start I changed the path to the fixed one (POSIX Path gave an error):
set logFilePath to “/Volumes/LC3/Documents-LC3/_loggar/_tpp/my-touched-pages-IN.txt”

So maybe the plugin is not even installed correctly. I only find the XMLLib371.pkg installer package, not the unpacked folder(s).

with best regards,
Omar KN
Stockholm, Sweden

You should find it here:
/Library/ScriptingAdditions Satimage.osax
This script will list all Scripting Additions that are installed in the proper locations:


The following script will create a neatly formatted string describing the contents of all of your ScriptingAdditions folders:

REF:  KINDLE BOOK:  Sanderson, Hamish; Rosenthal, Hanaan (2010-05-05). Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X (Learn (Apress)) (Kindle Locations 15946-15947). Apress. Kindle Edition. 

set osax_info to {}
set folderList to {}

tell application "System Events"
  set osax_folders to scripting additions folder of every domain
  repeat with folder_ref in reverse of osax_folders
    if (contents of folder_ref is not missing value) then
      set AppleScript's text item delimiters to return & tab
      set end of folderList to POSIX path of folder_ref
      set end of osax_info to POSIX path of folder_ref & return & tab & (name of every file of folder_ref whose visible is true)
    end if
  end repeat
end tell

set AppleScript's text item delimiters to return & return

set oAns to display dialog (osax_info as text) ¬
  buttons {"Open Folder", "Cancel", "OK"} ¬
  default button ¬
  "OK" cancel button ¬
  "Cancel" with title ¬
  "Scripting Additions on Your Mac"

if (button returned of oAns = "Open Folder") then
  set osaxFolder to choose from list folderList ¬
    with title ¬
    "Choose ScriptingAdditions Folder to Open" with prompt ¬
    "Choose Folder" default items item 1 of folderList
  if (osaxFolder ≠ false) then
    set osaxFolder to alias (POSIX file osaxFolder)
    tell application "Finder" to open osaxFolder
  end if
end if