Automating the Keyboard Maestro Editor (pre-KM8)

tools
ui_control
applescript

#21

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.


Shortcut key for next and previous macros?
#22

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)


Would it be possible to make 'go to group' spotlight-like hotkey?
#23

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


Automating the Keyboard Maestro Forum Editor
#24

I have put Chris’ script into a macro here:


Automating the Keyboard Maestro Forum Editor
#25

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


QuicKeys has a simpler technique to Activate/Deactivate a macro
Automating the Keyboard Maestro Forum Editor
#26

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

#27

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:


#28

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)


Automating the Keyboard Maestro Forum Editor
#29

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

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

Automating the Keyboard Maestro Forum Editor
Are there any shortcut keys (hotkeys) in the KM Editor, like one for rename action?
#30

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


Automating the Keyboard Maestro Forum Editor
#31

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)


#32

EDIT: Never mind. Try to imagine the dumbest possible reason this might have failed to work.


This is really useful. I tried to modify it to work for “Enable/Disable” but I can’t get it to work. I put this where you have tell menu item "Rename…":
if menu item "Disable" exists then tell menu item "Disable" perform action "AXPress" end tell else if menu item "Enable" exists then tell menu item "Enable" perform action "AXPress" end tell end if

Any suggestions?


Automating the Keyboard Maestro Forum Editor
#33

Hey Folks,

Peter spent a lot of effort making Keyboard Maestro 8 more AppleScriptable. (Thank you Peter!)

And this makes many things easy to do that were difficult or impossible in previous versions of KM.


When I’m testing something in Keyboard Maestro or creating a new macro I almost always use (or at least start out with) the same test macro:

Generic-Test 01

Therefore I want to get to that macro quickly quite often.

Using the following script I can manage that spectacular feat with a keystroke.

The user is REQUIRED to provide names for their test-group and test-macro — see User Settings in the script.

If the designated group or the macro don’t exist they will be created as necessary.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/08/21 21:37
# dMod: 2017/11/17 04:46
# Appl: Keyboard Maestro
# Task: Edit 'Generic Test 1' Macro of macro group 'Test Group'.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_8, @KM8, @Editor, @Edit, @Macro
------------------------------------------------------------------------------

try
   
   ---------------------------------------------------------------------------
   # User Settings:
   ---------------------------------------------------------------------------
   set macroGroupName to "Test Group"
   set macroName to "Generic-Test 01"
   ---------------------------------------------------------------------------
   
   tell application "Keyboard Maestro"
      
      ------------------------------------------------------------------------
      # Make sure the designated macro group and macro exist.
      # Create one or both of them as necessary.
      ------------------------------------------------------------------------
      
      if (exists of macro group macroGroupName) = false then
         set newGroup to make new macro group with properties {name:macroGroupName}
         select newGroup
      end if
      
      if (exists of macro macroName) = false then
         tell macro group macroGroupName
            set newMacro to make new macro with properties {name:macroName}
            select newMacro
            return --» script ends here IF a new macro is created.
         end tell
      end if
      
      ------------------------------------------------------------------------
      # Select the macro group if necessary.
      ------------------------------------------------------------------------
      
      if selected of macro group macroGroupName = false or length of (get selected macro groups) > 1 then
         select macro group macroGroupName
      end if
      
      ------------------------------------------------------------------------
      # Select the macro for editing.
      ------------------------------------------------------------------------
      
      editMacro macroName
      
      ------------------------------------------------------------------------
      
   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

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

Enjoy.

-Chris


How to make a predefined macro with one Keystroke action and a trigger
Macro group of current app
#34

Hey Folks,

When I write a macro for someone else and sometimes when I’ve accomplished a difficult bit of testing, I often want to save the final result in my “e_Examples” macro group.

It’s named that way, so it sorts a the top of the groups starting with “e”. That way I can hit one keystroke to move the focus in the editor to the group pane and then “e” to get to that group.

I used to have to copy my new creation and then go through the above rigamarole to get to the examples group and then paste.

Now with Keyboard Maestro 8 I can move the selected macros with an AppleScript and one keystroke.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/17 05:58
# dMod: 2017/11/17 06:04
# Appl: Keyboard Maestro
# Task: Move the Selected Macros to a user-designated macro group.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_8, @KM8, @Move, @Selected, @Macros, @Designated, @Group
------------------------------------------------------------------------------

try
   
   ---------------------------------------------------------------------------
   # User settings:
   ---------------------------------------------------------------------------
   set moveToGroupName to "e_Examples"
   ---------------------------------------------------------------------------
   
   tell application "Keyboard Maestro"
      
      set macroSelectionList to selected macros
      
      if macroSelectionList = {} then
         error "No macros are selected!"
      end if
      
      # Disable any macros that are enabled.
      repeat with theMacro in macroSelectionList
         if enabled of theMacro is true then
            set enabled of theMacro to false
         end if
      end repeat
      
      move macroSelectionList to macro group moveToGroupName
      
   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

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

Enjoy.

-Chris


#35

Hey Folks,

Since I’ve made it easier to file things in my e_Examples group, I thought I’d make it easier to access it for searching.

This AppleScript will take you straight to the designated group name in the Keyboard Maestro editor.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/17 01:09
# dMod: 2017/11/17 01:19
# Appl: Keyboard Maestro
# Task: Select the macro group "e_Examples".
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @KM8, @Keyboard_Maestro_8, @Keyboard_Maestro_Editor, @Select, @Macro, @Group, @e_Examples
------------------------------------------------------------------------------

try
   
   ---------------------------------------------------------------------------
   # User Settings:
   ---------------------------------------------------------------------------
   set macroGroupName to "e_Examples"
   ---------------------------------------------------------------------------
   
   tell application "Keyboard Maestro"
      
      if macro group macroGroupName exists then
         select macro group macroGroupName
      else
         error "Cannot select macro group " & macroGroupName & " — it is missing!"
      end if
      
   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

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

Enjoy.

-Chris


#36

I think this macro goes well with your other macro that brings focus to the Macros section of the group :

-------------------------------------------------------------------------------------------
# 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

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

Not sure what the best way to combine these macros together is, perhaps this?

try   
   ---------------------------------------------------------------------------
   # User Settings:
   ---------------------------------------------------------------------------
   set macroGroupName to "e_Examples"
   ---------------------------------------------------------------------------
   
   tell application "Keyboard Maestro"
      
      if macro group macroGroupName exists then
         select macro group macroGroupName
      else
         error "Cannot select macro group " & macroGroupName & " — it is missing!"
      end if
      
   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

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


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

Thanks for sharing these @ccstone