###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:
- Make the App of Interest FrontMost
- Trigger this Macro
- 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.
.
- 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:
- Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
- 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