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
--------------------------------------------------------
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.
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.
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?
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
--------------------------------------------------------
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.
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.
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.
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.)