Make Window FrontMost Based on Name

###MACRO:   Make Window FrontMost Based on Name

~~~ VER: 1.0    2017-08-18 ~~~

Make Window FrontMost Based on Name.kmmacros (8.4 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

###Use Case

  • Bring a App Window FrontMost based on its name




  • Make App Window FrontMost Based on Its Name
    • Contains, Starts With, OR Ends With


  1. Make the App of Interest FrontMost
  2. Trigger this Macro


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    (all shown in the magenta color)
    • GRP: Enable the Actions you Want for Input
      • Prompt for User Input
      • Set Variables for WinName, PosInName

TAGS: @Windows @FrontMost @UI


  • Any Action in magenta color is designed to be changed by end-user


  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions


  1. Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
  2. El Capitan 10.11.6+
  • It make work with Yosemite, but I make no guarantees.


  • While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.


property ptyScriptName : "Make App Window FrontMost Based on Name"
property ptyScriptVer : "1.2"
property ptyScriptDate : "2017-08-18"
property ptyScriptAuthor : "JMichaelTX"

  • KM Variables:  
    • MWF__WinName
    * MWF__PosInName
Local:  /Users/Shared/Dropbox/SW/DEV/KM/Scripts/Make @Window @FrontMost Based on Name @UI @KM @AS.scpt
property LF : linefeed

set scriptResults to "TBD"

  set frontApp to path to frontmost application as text -- use for dialogs
  tell application "Keyboard Maestro Engine"
    set winNamePartial to getvariable "MWF__WinName"
    set posInName to getvariable "MWF__PosInName"
  end tell
  tell application "System Events"
    set frontmostApp to name of first item of (processes whose frontmost is true)
    tell process frontmostApp
      if (posInName = "starts with") then
        set oWin to first window whose name starts with winNamePartial
      else if (posInName = "ends with") then
        set oWin to first window whose name ends with winNamePartial
        set oWin to first window whose name contains winNamePartial
      end if
      set value of attribute "AXMain" of oWin to true
    end tell
  end tell
  set scriptResults to "OK"
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
  end if
  if errNum = -1719 then
    set msgStr to "Window Could NOT be Found Whose Name " & LF & ¬
      posInName & " \"" & winNamePartial & "\""
    set msgTitleStr to ptyScriptName
    display notification msgStr with title msgTitleStr sound name "Basso.aiff"
  end if
  set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
    & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
    & "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return scriptResults
1 Like

Hi @JMichaelTX

I was trying to change this macro so that it will take two variables that it can search against instead of one.

So right now I can set MWF__WinName as a. but I also wish to set some variable MWF__WinName_fallback to 'wf.'.

And then if there is no window active with string 'a.', it will search whether there is a window with string 'wf.' in there and if nothing is found throw an error as it does now.

Here is the macro in which I am using your wonderful macro :

open mindnode - working project.kmmacros (29.6 KB)

I would really love some help on how I can customise it to accept to variables to do the string matching on.

Thank you so much.

@nikivi, I think this should do the job for you.

See this test, example macro that fits your specific use case:
MACRO: TEST Make App Win FrontMost

It uses this Sub-Macro:
MACRO: Make App Window FrontMost Based on Name [Sub-Macro]

You will need to download both macros.

Please let us know if this works for you.