Make App Window FrontMost Based on Name [Sub-Macro]

###MACRO:   Make App Window FrontMost Based on Name [Sub-Macro]

~~~ VER: 2.0    2017-08-23 ~~~

####DOWNLOAD:
Make App Window FrontMost Based on Name [Sub-Macro].kmmacros (9.6 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


###This is a Sub-Macro

  • It is NOT intended to be directly triggered by the user
  • It is intended to be called by another Macro, using the Execute a Macro action (KM Wiki)
  • Please see the TEST Macro in the below reply for an example of how to use.

###Use Case

  • Make Frontmost, any Window of an App which meets your criteria for it's Title
  • Can be used in a For Each loop to search for multiple cases.

###ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Make App Window FrontMost Based on Its Name
    • Contains, Starts With, OR Ends With
    • Designed as a Sub-Macro
    • The Search Fields are passed as a Parameter in the Execute Macro Action, which will call this macro.

HOW TO USE:

  1. Make the App of Interest FrontMost
  2. Trigger this Macro
  3. IF the Window is Found, it is made Frontmost, and the Script sets the MWF__ScriptResults to "OK"

MACRO SETUP

  • 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.
    .
  • There are NO User Settings to Make in this Macro.

TAGS: @Windows @FrontMost @UI @SubMacro

REQUIRES:

  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.

USE AT YOUR OWN RISK

  • 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.


###AppleScript

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

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

set scriptResults to "TBD"

try
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set frontApp to path to frontmost application as text -- use for dialogs
  
  tell application "Keyboard Maestro Engine"
    set winNamePartial to getvariable "MWF__SearchFor"
    set posInName to getvariable "MWF__SearchType"
  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
      else
        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

Here's the TEST Macro:

##Macro Library   TEST Make App Win FrontMost


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/065ff9ecaf23dd3ebfad38d8cd6829b19ea446ff.kmmacros">TEST Make App Win FrontMost.kmmacros</a> (8.5 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###Use Case

* Search for Multiple Windows, in order.
* The  first window that meets your criteria is made frontmost, and the macro exits.

---

###ReleaseNotes

This macro is designed simply to TEST the Sub-Macro:
Make App Window FrontMost Based on Name [Sub-Macro]

To setup this macro:

1. Assign a trigger to it
2. Put in a Global Group
3. Change the magenta Action "SET WinSearchList to SearchFor, SearchType" to enter the Window Search terms you would like to use.
  * See the below "Window Search List" comment for instructions.

HOW TO RUN/TEST

1. Make sure above setup is  complete
2. Activate the App for which you want to test
3. Trigger this macro

---


<img src="/uploads/default/original/2X/c/ce4bc81a27a0070eb44ccde1159802a48c36ece2.png" width="486" height="2218">