How Do I Insert an Action as the First Action in 206 Macros Automatically

Hey @troy,

  • Sort your macros by NAME.
  • Select the ones you want to add the action to.
  • Run the AppleScript from an Execute an AppleScript action.

Be smart and test on 1 or 2 before going whole hog...

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/28 16:54
# dMod: 2022/12/28 16:54 
# Appl: Keyboard Maestro
# Task: Add a 0.1 Second Pause Action To Every Selected Macro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Add, @Pause, @Action, @Selected, @Selection
--------------------------------------------------------

try
   
   set actionXML to text 2 thru -2 of "
   <dict>
      <key>ActionUID</key>
      <integer>12794614</integer>
      <key>MacroActionType</key>
      <string>Pause</string>
      <key>Time</key>
      <string>0.1</string>
      <key>TimeOutAbortsMacro</key>
      <true/>
   </dict>
   "
   
   tell application "Keyboard Maestro"
      set selectedMacroList to selected macros
      if length of selectedMacroList > 0 then
         repeat with theMacro in selectedMacroList
            tell theMacro
               set newAction to make new action with properties {xml:actionXML} at beginning
               # select first action
            end tell
         end repeat
      end if
   end tell
   
   display notification "Action Insertion Job Complete!" with title "AppleScript" subtitle "······················" sound name "Tink"
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg 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 errMsg
      end try
   end if
end try

--------------------------------------------------------
7 Likes

Oh yeah...

Create a pause action the way you want it and then Copy it as XML using the Keyboard Maestro Editor.

Paste that into BBEdit or a similar text editor and look at the difference between the original XML and what I've pruned it down to in the AppleScript.

By learning how to do that you can perform this task yourself in future.

2 Likes

Hello, may I suggest that you use a variable inside the pause action instead of a number. Setup the variable value in a separate macro. Likewise you can change the value of the delay on demand.

3 Likes

crazy man! thank you

1 Like

If I wanted to make all those .1 sec actions the color green, what would I enter as the code?

I tried a couple variations of course with no success.

Here of course is the xml of the initial code and following it is the Green actions' xml.

      <key>ActionUID</key>
      <integer>12794614</integer>
      <key>MacroActionType</key>
      <string>Pause</string>
      <key>Time</key>
      <string>0.1</string>
      <key>TimeOutAbortsMacro</key>
      <true/>
   </dict>
   
   

	<dict>
		<key>ActionColor</key>
		<string>Green</string>
		<key>ActionUID</key>
		<integer>11193711</integer>
		<key>MacroActionType</key>
		<string>Pause</string>
		<key>Time</key>
		<string>Text_Expansion_Delay</string>
		<key>TimeOutAbortsMacro</key>
		<true/>
	</dict>
</array>
</plist>

I do not want to add a new action, but make the existing action the color green.

You don't.

  1. Follow my instructions about creating a new action the way you want it and then copying the XML.
     
  2. Replace the XML text in the AppleScript with the new XML.
    • Make sure you reproduce the formatting of the XML.

Maybe you missed my last sentence in the previous post.
"I do not want to add a new action, but make the existing action the color green."

If I do as you suggested above, would that not make a new action?

Hi, @troy. To be safe, wait for @ccstone to reply. But I think a simple modification to his script would do the trick:

3 Likes

perfect, thank you both!

1 Like

My preference would be to manage the color in the XML used to create the action(s).

However – since @troy wanted to change the actions' color after their creation – this is the simplest method to get that done.

2 Likes

Hi, @ccstone. As always, thanks for sharing all of your AppleScript wisdom!

In this line:

set newAction to make new action with properties {xml:actionXML} at beginning

The at beginning obviously adds the action at the beginning of the selected macro. After testing I see that if it is not included, the action is added to the end of the macro. Do you know if there is any specifier that can be used to add it below or above the selected action?

Here's all I see in the wiki: Scripting the Keyboard Maestro editor

Hey @_jims,

Here's an example.

I've demonstrated the before.

For after you need to get the last item of the selected actions and then use after instead of before.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2022/12/31 21:29
# dMod: 2022/12/31 21:29 
# Appl: Keyboard Maestro Editor
# Task: Create a New Action From XML Text Before the Selected Action(s).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Create, @New, @Action, @XML, @Selection, @Selected
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

try
   
   set actionXML to text 2 thru -2 of "
      <dict>
         <key>Action</key>
         <string>ByTyping</string>
         <key>ActionUID</key>
         <integer>12794084</integer>
         <key>MacroActionType</key>
         <string>InsertText</string>
         <key>TargetApplication</key>
         <dict/>
         <key>TargetingType</key>
         <string>Front</string>
         <key>Text</key>
         <string>%ChromeReadyState%</string>
      </dict>
"
   
   tell application "Keyboard Maestro"
      
      set selectedMacroList to selected macros
      set numberOfSelectedMacros to length of selectedMacroList
      
      if numberOfSelectedMacros = 0 then
         error "Zero Macros Are Selected!" & LF & LF & "Please select just 1..."
      else if numberOfSelectedMacros > 1 then
         error "Too Many Macros Are Selected! (" & numberOfSelectedMacros & ")" & LF & LF & "Please select just 1..."
      end if
      
      set workingMacro to item 1 of selectedMacroList
      set selectedActionList to selection
      set activeAction to item 1 of selectedActionList
      
      tell workingMacro
         set newAction to make new action at before activeAction with properties {xml:actionXML}
         select newAction
      end tell
      
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg 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 errMsg
      end try
   end if
end try

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

Thanks so much, @ccstone.

Curious, how/where did you find that qualifier (at before activeAction)?

Syntactical stuff picked up from 26+ years of writing AppleScript.


Standard Suite in the Keyboard Maestro Editor's AppleScript Dictionary


image


Script Debugger makes looking up keywords like this simple.

Select the keyword and press f

3 Likes

Chris, I always appreciate your generosity sharing the knowledge you have acquired.

Ah, that's a great tip. I just tried it. Interestingly, when I click on the location specifier, I don't see any additional information. Do you?


1 Like

As usual Chris, you’re the man.

I used your AppleScript to replace a macro I had been using (albeit quite successfully) to insert a “follow up comment” above the selected action.

Before, I was setting the pre-formatted action’s XML to the clipboard and pasting it in, and while that worked well, the positioning wasn’t reliable. But this AppleScript ensure it is inserted before the selected action which is what I like.

Again, thanks for always sharing your experience with the forum.

3 Likes

Same here, @cdthomer. I’ve applied @ccstone‘s guidance in…

2 Likes

Nyet. Nor do I know why it shows as linked but has no destination.

These dead links occur sometimes and can be quite frustrating. The best place to ask about that would be on the Script Debugger Forum and to flag @alldritt & @ShaneStanley.

The horse's mouth as it were...

Keep in mind that Script Debugger does a whole bunch of things under the hood to make AppleScript dictionaries more intelligible and useful.

Thanks for that tip, @ccstone.

With Script Debugger it's amazing what's provided for free. Even though I'm far from a power user, it just seemed right to purchase a v8 license. So I did.

Yes, the information quantity is much greater and the organization is impeccable.

I'll be nice when I ask. :wink:


On a related note...

When I was recently using Script Debugger and scripting the Keyboard Maestro editor, I encountered an issue that maybe you or others on this forum have encountered: with File > Open Dictionary > Keyboard Maestro and using the Explorer tab, it starts up as expected by after a few minutes of use indicates Application Not Running. Sometimes I subsequently see a dialog to Launch Keyboard Maestro (even though it is open) but that never seems to have any effect. However, if I manually Quit are reopen KM, the SD Explorer tab seems to normally, but not always, reconnect.

@ccstone (or anyone else), have you seen this issue? (Maybe another topic for the SD forum.)

1 Like

I don't recall seeing this one...

I'd make a movie of the issue in action (if possible) and ask on the SD forum.

1 Like