Editing Currently Selected Macro with Applescript

Hi there, I know it is possible to create macros using AppleScript to create the XML but is it possible to edit existing macros using a similar method?

For instance, at the moment I am quite often changing the typed string trigger to be case insensitive and to remove the option to simulate deletes after typing. I have been using the found image trigger to achieve this but it's pretty iffy, so a more programatic approach might be better. It might also be useful to be able to add a frequently used actions with this method (although this can already done by simply pasting in the XML).

Based on this macro I'm assuming that it could be possible to edit an existing macro's triggers or actions by inserting new XML or modifying it or something, but any help would be appreciated - as a noob, I find basically all documentation of AppleScript to be pretty unfriendly.

Thanks very much!

You can adjust the currently edited macro.

Assuming you have selected a macro, and assuming it has at least one trigger, this will set the first trigger to a Typed String trigger with text “testing”, case insensitive, no deletes.

set x to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
	<key>Case</key>
	<string>Ignore</string>
	<key>MacroTriggerType</key>
	<string>TypedString</string>
	<key>SimulateDeletes</key>
	<false/>
	<key>TypedString</key>
	<string>testing</string>
</dict>
</plist>
"

tell application "Keyboard Maestro"
	set ms to selected macros
	set m to item 1 of ms
	tell m
		set xml of trigger 1 to x
		-- xml of trigger 1
	end tell
end tell
3 Likes

Hi Peter, this is great! I had to delete the three quote marks at the end to make it work, but work it does! Is there a good resource for finding out more about the various AppleScript options available for KM? Like a list of all the different parameters you can alter/syntax used?

1 Like

This will take the selected macro's existing trigger, retain the typed string and the "following" and "diacriticals" settings, but make it a case-insensitive match without deletion. It's a bit long-winded to make it clear what's happening:

tell application "Keyboard Maestro"
	set theMacro to item 1 of (get the selected macros)
	tell theMacro
		set theXML to xml of trigger 1
		set AppleScript's text item delimiters to "<dict>
"
		set xmlPre to text item 1 of theXML
		set xmlPost to text item 2 of theXML
		set theXML to xmlPre & "<dict>
	<key>Case</key>
	<string>Ignore</string>
" & xmlPost
		set AppleScript's text item delimiters to "	<key>SimulateDeletes</key>
	<true/>
"
		set xmlPre to text item 1 of theXML
		set xmlPost to text item 2 of theXML
		set theXML to xmlPre & "	<key>SimulateDeletes</key>
	<false/>
" & xmlPost
		set xml of trigger 1 to theXML
	end tell
end tell

It does rely on the first trigger being a "Typed string" trigger, though!

Hey Folks,

Keep in mind that Search & Replace in AppleScript is much more flexibly done via Keyboard Maestro's own search command.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2023/03/30 18:13
# dMod: 2023/03/30 18:13 
# Appl: Keyboard Maestro
# Task: Search and Replace Text Using Keyboard Maestro with Optional RegEx.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @Search, @Replace, @Text, @RegEx
--------------------------------------------------------

set dataStr to "Now is the time for all good men to come to the aid of their country."

set newStr to my kmCng:"men" inStr:dataStr withStr:"people" usingRegEx:false ¬
   usingCase:false usingTokens:false

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
--» kmCng()
--------------------------------------------------------
--  Task: Find and Replace with RegEx using Keyboard Maestro's AppleScript search command.
--  dMod: 2019/01/19 04:33
--------------------------------------------------------
# Uses the Keyboard Maestro Engine for RegEx Support.
on kmCng:regExPat inStr:dataStr withStr:replacePat usingRegEx:regExBool usingCase:caseBool usingTokens:tokensBool
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for regExPat replace replacePat ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmCng:inStr:withStr:usingRegEx:usingCase:usingTokens:
--------------------------------------------------------

The AppleScript sdef Library is viewable in the Script Editor and has all the details of the Keyboard Maestro scripts (and any other scriptable application you add to the Script Editor Library).

Ah thanks, didn't know this. Much appreciated!

1 Like