Automating the Keyboard Maestro Editor (pre-KM8)


updated by MODERATOR -- 2018-07-29 17:08 GMT-5

[This thread is pending revision.]

Chris and others provided some great scripts/macros in this thread back in the day when UI scripting was needed to control the Keyboard Maestro Editor.

With the release of Keyboard Maestro 8, some of these are obsolete, and should be deprecated.
As of KM 8, it is much easier, and more reliable, to use the new scripting commands/objects.
For more info, see



Hey Folks,

Three AppleScripts to make navigating in the Keyboard Maestro Editor a little easier.

All of these should be run from an Execute an AppleScript action, and they will run a bit faster if run as compiled-scripts rather than as text-scripts.

Move Focus to Group List

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/07/25 03:06
# dMod: 2015/07/25 03:19
# Appl: Keyboard Maestro & System Events
# Task: Set Focus to Group List
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Focus, @Group_List
--------------------------------------------------------------------------------

try
   
   tell application "System Events"
      tell application process "Keyboard Maestro"
         tell window "Keyboard Maestro Editor"
            tell scroll area 1 of splitter group 1 of group 1
               set focused to true
            end tell
         end tell
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

--------------------------------------------------------------------------------

Move Focus to Macro List

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/07/25 03:06
# dMod: 2015/07/25 03:14
# Appl: Keyboard Maestro & System Events
# Task: Set Focus to Macro List
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Focus, @Macro_List
--------------------------------------------------------------------------------

try
   
   tell application "System Events"
      tell application process "Keyboard Maestro"
         tell window "Keyboard Maestro Editor"
            tell scroll area 2 of splitter group 1 of group 1
               set focused to true
            end tell
         end tell
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

--------------------------------------------------------------------------------

Move Focus to Group or Macro Name Field

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/07/25 03:06
# dMod: 2016/06/21 18:10
# Appl: Keyboard Maestro & System Events
# Task: Select Macro or Group Name Field for Editing
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Select, @Name, @Field
--------------------------------------------------------------------------------

try
   
   tell application "System Events"
      tell application process "Keyboard Maestro"
         
         tell menu item "Start Editing Macros" of menu "View" of menu bar item "View" of menu bar 1
            if it exists then click it
         end tell
         
         tell window "Keyboard Maestro Editor"
            tell scroll area 3 of splitter group 1 of group 1
               if text field 1 exists then
                  tell (first text field whose attribute "AXPlaceholderValue"'s value is "macro name")
                     set focused to true
                     # perform action "AXShowMenu"
                  end tell
               end if
            end tell
            
         end tell
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

--------------------------------------------------------------------------------

-Chris

8 Likes

Hey Folks,

Here are the compiled scripts.

Keyboard Maestro Editor Focus Scripts.zip (12.5 KB)

-Chris

1 Like

Dan Thomas built an excellent macro Reveal Macro Group.

Starting with a macro selected in the All Macros Smart Group – the macro will take you to it in its native group.

Well done Dan!

-Chris

2 Likes

Most everything I know I learned from guys like you. :slight_smile:

2 Likes

Hey Folks,

This AppleScript will navigate to the All Macros group in the Keyboard Maestro Editor.

-Chris

Navigate to the “All Macros” group and Return Focus to the Macros List v1.00.scpt.zip (4.4 KB)

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/06/22 14:14
# dMod: 2016/06/22 14:29
# Appl: Keyboard Maestro Editor & System Events
# Task: Navigate to the “All Macros” group and Return Focus to the Macros List.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro_Editor, @Navigate, @All_Macros
--------------------------------------------------------------------------------

try
   
   tell application "System Events"
      tell application process "Keyboard Maestro"
         tell (first window whose subrole is "AXStandardWindow")
            tell group 1 of scroll area 1 of splitter group 1 of group 1
               perform action "AXPress"
            end tell
            tell scroll area 2 of splitter group 1 of group 1
               set focused to true
            end tell
         end tell
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

--------------------------------------------------------------------------------
1 Like

“AXPress”, huh? Since this made me go hunting, I just realized that “UI Browser” shows more than Attributes in its drawer. Cool!

Is AXPress analogous to “click()”? Or is “click()” more likely a combination of the press and the focus?

Another thing I’ve refused to ask, preferring the head-in-the-sand approach, is whether we can depend on these things always being “group 1 of scroll area 1” (or whatever), at least until a new version of KM is released?

Things like showing or hiding sections, etc - these generally only affect visibility, not whether the objects actually exist or not?

Nothing changes their order?

2 Likes

Hey Dan,

In general click should substitute for AXPress.

When using positional parameters this is always a risk.

When possible I like to use named anchors, but you can’t depend upon developers to never change the names – so this method is not a sinecure.

Here’s an example:

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell (first window whose subrole is "AXStandardWindow")

         tell scroll area 1 of splitter group 1 of group 1
            set allMacrosGroup to first group where its attribute "AXDescription"'s value is "All Macros"
         end tell
         
         tell allMacrosGroup
            perform action "AXPress"
         end tell
         
         tell scroll area 2 of splitter group 1 of group 1
            set focused to true
         end tell
         
      end tell
   end tell
end tell

I didn’t do this in the production script, because the other method is safe (for now) and a little bit faster.

There is also the issue of localized names.

Oh, yes. This sort of thing can give you a really bad day.

It can, and how this happens may not be under the developer’s control.

In the Move Focus to Group or Macro Name Field script I use:

tell (first text field whose attribute "AXPlaceholderValue"'s value is "macro name")

Because the numbering of the field changes when you start adding actions with text fields.

-Chris

1 Like

Ugh. Now I remember why I like the head-in-the-sand approach.

Thanks.

1 Like

Because of all the crabs you find? <gdr,vvf>

2 Likes

@JMichaelTX, @ccstone - Every once in awhile, I forget that I already know how to do something. This was one of those cases. I already do this, in my "Find Macros that use this Sub-Macro" macro.

Once you have the Group's UUID, there is a way to go right to that group in the KM Editor.

This URI works for both macros and groups. So we made this a lot harder than we needed to.

2 Likes

Hey Dan,

The URL method does the same thing as the AppleScript method:

tell application "Keyboard Maestro"
   editMacro "Name or UUID"
end tell

This opens the group for editing, and I was trying to avoid that.

However – that got me thinking – and I can use it to speed up my macro a bit.

tell application "Keyboard Maestro"
   editMacro "All Macros"
end tell
tell application "System Events"
   tell application process "Keyboard Maestro"
      tell (first window whose subrole is "AXStandardWindow")
         tell scroll area 2 of splitter group 1 of group 1
            set focused to true
         end tell
      end tell
   end tell
end tell

-Chris

2 Likes

Chris -

“All Macros” may not exist - I thought I’d mention that. I accidentally renamed my All Macros group yesterday. It wasn’t at the top of the list either. Just thought I’d point that out, in case you didn’t think of it.

1 Like

Hey Dan,

Well – I think Peter should make “All Macros” completely immutable – so there’s a check-point the user can’t screw up.

He’s made “all:” immutable as long as the group is named “All Macros” but allowed the user to change the name. If the user changes the name then “all:” loses its immutability.

Few users are going to goof this up, so I decided not to worry about it.

However I don’t know how it localizes – so there’s something to think about.

-Chris

3 Likes

It’s actually really easy. My thought process was “Search the All Macros group”, so this is what I did, without paying enough attention:

  1. I clicked on All Macros.
  2. I started typing.
1 Like

Hey Dan,

You would have had to have done more than that, because those two steps will simply move you to another group in the groups panel.

This is why I wasn’t content with edit macros "All Macros", because that leaves the focus in the group name field.

My macro moves focus away from the group name field making it more difficult for the user to screw things up.

If you simply must have some error-checking in there:

--------------------------------------------------------------------------------
try
   
   tell application "System Events"
      tell application process "Keyboard Maestro"
         tell (first window whose subrole is "AXStandardWindow")
            tell group 1 of scroll area 1 of splitter group 1 of group 1
               if accessibility description ≠ "All Macros" then
                  error "Your “All Macros” group appears to be missing!"
               end if
            end tell
            selectAllMacrosGroup() of me
            tell scroll area 2 of splitter group 1 of group 1 to set focused to true
         end tell
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try
--------------------------------------------------------------------------------
on selectAllMacrosGroup()
   tell application "Keyboard Maestro"
      editMacro "All Macros"
   end tell
end selectAllMacrosGroup
--------------------------------------------------------------------------------

-Chris

2 Likes

Hey Folks,

Open the Keyboard Maestro Editor Macro Edit History menu for viewing or type-selecting an item.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/11 22:43
# dMod: 2016/08/11 22:53 
# Appl: System Events & Keyboard Maestro Editor
# Task: Access the Macro Edit History Menu of the Keyboard Maestro Editor.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Editor, @History
--------------------------------------------------------------------------------

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell window "Keyboard Maestro Editor"
         tell button 1 of group 5
            perform action "AXShowMenu" -- Open the History Button Menu.
         end tell
      end tell
   end tell
   
   key code 125 -- Select the first menu item.
   
end tell

--------------------------------------------------------------------------------
5 Likes

SHARE THE SELECTED ITEMS IN THE KEYBOARD MAESTRO EDITOR TO THE FORUM


Hey Folks,

I finally got tired of doing this manually. The macro takes you to the point of sharing {Both} to the forum – just as if you manually clicked through the process.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/11 22:43
# dMod: 2016/08/11 23:19
# Appl: System Events & Keyboard Maestro Editor
# Task: Share the Selected Items in the Keyboard Maestro Editor to the Forum.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Editor, @Send, @Forum
--------------------------------------------------------------------------------

tell application "System Events"
   
   tell application process "Keyboard Maestro"
      set frontmost to true
      
      tell window "Keyboard Maestro Editor"
         tell group 2
            tell button 1
               perform action "AXShowMenu" -- Open the Share Button Menu.
            end tell
         end tell
      end tell
   end tell
   
   keystroke "k"
   key code 36
   
   tell application process "Keyboard Maestro"
      tell window "Keyboard Maestro Editor"
         tell button "Both" of sheet 1
            perform action "AXPress"
         end tell
      end tell
   end tell
   
end tell

--------------------------------------------------------------------------------
1 Like

OPEN THE RECENTLY RUN MACRO BUTTON’S MENU

This macro opens the pop-up button and makes its menu available for type-select.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/11 22:43
# dMod: 2016/08/11 23:33
# Appl: System Events & Keyboard Maestro Editor
# Task: Open Recently Run Macro Button's Menu
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Editor, @Open, @Recently, @Run, @Menu
--------------------------------------------------------------------------------

tell application "System Events"
   
   tell application process "Keyboard Maestro"
      set frontmost to true
      
      tell window "Keyboard Maestro Editor"
         tell group 3
            tell button 1
               perform action "AXShowMenu" -- Open Recently Run Macros Menu
            end tell
         end tell
      end tell
   end tell
   
end tell

--------------------------------------------------------------------------------
3 Likes

SELECT THE PREVIOUSLY EDITED MACRO

Hey Folks,

This script presses the back button in the Keyboard Maestro Editor.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/11 22:43
# dMod: 2016/08/11 23:40
# Appl: System Events & Keyboard Maestro Editor
# Task: Select the Previously Edited Macro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Editor, @Select, @Previously, @Run, @Macro
--------------------------------------------------------------------------------

tell application "System Events"
   
   tell application process "Keyboard Maestro"
      set frontmost to true
      
      tell window "Keyboard Maestro Editor"
         tell group 4
            tell button 1
               if enabled = true then
                  perform action "AXPress" -- Select the Previously Edited Macro
               else
                  beep
               end if
            end tell
         end tell
      end tell
   end tell
   
end tell

--------------------------------------------------------------------------------
2 Likes

Hey Folks,

Select the next edited macro.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/11 22:43
# dMod: 2016/08/11 23:51
# Appl: System Events & Keyboard Maestro Editor
# Task: Select the Next Edited Macro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Editor, @Select, @Previously, @Run, @Macro
--------------------------------------------------------------------------------

tell application "System Events"
   
   tell application process "Keyboard Maestro"
      set frontmost to true
      
      tell window "Keyboard Maestro Editor"
         tell group 4
            tell button 2
               if enabled = true then
                  perform action "AXPress" -- Select the Next Edited Macro
               else
                  beep
               end if
            end tell
         end tell
      end tell
   end tell
   
end tell

--------------------------------------------------------------------------------
2 Likes