Automating the Keyboard Maestro Editor with AppleScript

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

Chris, many thanks for this excellent script.

I used it in this macro by @DanThomas that I enhanced :
MACRO: Toggle Between Macros and Maintain Scroll Position VER 1.1


updated 2018-07-27 14:19 GMT-5

  • This macro is no longer necessary
  • The KM Editor has been revised to automatically maintain scroll position.

2 Likes

Hey Folks,

Use a pick-list to choose a group to edit in the Keyboard Maestro Editor.

-Chris


Go-To the Group Specified in the Choose-From Dialog.kmmacros (4.0 KB)

3 Likes

12 posts were split to a new topic: Automating the Keyboard Maestro Forum Editor

I have put Chris' script into a macro here:

1 Like

Hey Folks,

Toggle the enabled/disabled state of the selected macro(s) – no matter what is selected in the Keyboard Maestro editor.

Run from an Execute an AppleScript action.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/12/20 08:00
# dMod: 2016/12/20 08:08
# Appl: System Events & Keyboard Maestro Editor
# Task: Toggle the Enabled-Disabled state of the selected macros.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Keyboard_Maestro, @Toggle, @Enabled, @Disabled, @State, @Selected, @Macros
# Note: Likely to fail on non-English language systems due to localization issues.
--------------------------------------------------------------------------------

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell (first window whose subrole is "AXStandardWindow")
         tell (first button whose accessibility description is "Enable or Disable Macro")
            perform action "AXPress"
         end tell
      end tell
   end tell
end tell

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

-Chris

4 Likes

Me too, I have just always been using "Reference to Selected Element" Comtion (Command+Option (⌥)) + r in UI Browswer.

Thanks Chris, now I have a lot of AppleScripts to update, I didn't know you could reference to the accesablitly name like that instead of this...

activate application "Keyboard Maestro"
tell application "System Events"
   tell process "Keyboard Maestro"
      click button 8 of window "Keyboard Maestro Editor"
   end tell
end tell

Amazing how much we miss in life, isn't it?

To paraphrase "The Wedding Singer", "That was information that would have been useful YESTERDAY!" :slight_smile:

So true, that is why from now on I am not going to make anymore macros, AppleScripts or webpages until I have learned all my options.

I'd Like To Hear My Options.mp3.zip (152.6 KB)


RENAME THE SELECTED ACTION


Hey Folks,

Here’s how to automate the [Rename] contextual-menu item for the selected action.

If more than one action is selected then the contextual-menu pops open to allow you to type-select.

Run it from an Execute an AppleScript action.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/22 14:45
# dMod: 2017/05/22 14:53 
# Appl: Keyboard Maestro Editor
# Task: Select Rename in the contextual menu of the selected action.
#     : If more than 1 action is selected bring up the contextual menu.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @System_Events, @Select, @Rename, @Contextual_Menu, @Menu, @Selected, @Action
------------------------------------------------------------------------------

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell window "Keyboard Maestro Editor"
         tell group 1
            tell splitter group 1
               tell scroll area 3
                  set selectedGroups to groups whose selected is true
                  if selectedGroups ≠ {} then
                     set theAction to item 1 of selectedGroups
                     tell theAction
                        perform action "AXShowMenu"
                        tell menu 1
                           tell menu item "Rename…"
                              perform action "AXPress"
                           end tell
                        end tell
                     end tell
                  end if
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

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

I don’t know what you mean by “Execute an Applescript action.” I couldn’t find any menu option for that.

Hey Leslie,

You have to make a macro with the Execute an AppleScript action.

Similar to this one.

It is possible to run an AppleScript as a text-script or from a compiled-AppleScript-file.

-Chris


Execute an AppleScript action.kmmacros (1.7 KB)