Inserting Action via XML error

I am wanting to insert an action at the end of all the selected macros.
I am close, but getting an "Invalid XML From AppleScript" action inserted in the selected macro when I run the following.

try
	set actionXML 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\">
   <array>
      <dict>
         <key>ActionUID</key>
         <integer>16142225</integer>
         <key>Conditions</key>
         <dict>
            <key>ConditionList</key>
            <array>
               <dict>
                  <key>Application</key>
                  <dict>
                     <key>BundleIdentifier</key>
                     <string>com.apple.TextEdit</string>
                     <key>Name</key>
                     <string>TextEdit</string>
                     <key>NewFile</key>
                     <string>/System/Applications/TextEdit.app</string>
                  </dict>
                  <key>ApplicationConditionType</key>
                  <string>Active</string>
                  <key>ConditionType</key>
                  <string>Application</string>
               </dict>
            </array>
            <key>ConditionListMatch</key>
            <string>All</string>
         </dict>
         <key>ElseActions</key>
         <array/>
         <key>MacroActionType</key>
         <string>IfThenElse</string>
         <key>ThenActions</key>
         <array>
            <dict>
               <key>ActionUID</key>
               <integer>16142241</integer>
               <key>KeyCode</key>
               <integer>36</integer>
               <key>MacroActionType</key>
               <string>SimulateKeystroke</string>
               <key>Modifiers</key>
               <integer>0</integer>
               <key>ReleaseAll</key>
               <false/>
               <key>TargetApplication</key>
               <dict/>
               <key>TargetingType</key>
               <string>Front</string>
            </dict>
         </array>
         <key>TimeOutAbortsMacro</key>
         <true/>
      </dict>
   </array>
   </plist>"
	
	tell application "Keyboard Maestro"
		set selectedMacroList to selected macros
		if length of selectedMacroList > 0 then
			repeat with theMacro in selectedMacroList
				tell theMacro
					make new action with properties {xml:actionXML}
				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

Don't include the xml, plist, or initial array tags at the start, nor the closing array and plist tags at the end.

try
	set actionXML to "<dict>
         <key>ActionUID</key>
         <integer>16142225</integer>
         <key>Conditions</key>
         <dict>
            <key>ConditionList</key>
            <array>
               <dict>
                  <key>Application</key>
                  <dict>
                     <key>BundleIdentifier</key>
                     <string>com.apple.TextEdit</string>
                     <key>Name</key>
                     <string>TextEdit</string>
                     <key>NewFile</key>
                     <string>/System/Applications/TextEdit.app</string>
                  </dict>
                  <key>ApplicationConditionType</key>
                  <string>Active</string>
                  <key>ConditionType</key>
                  <string>Application</string>
               </dict>
            </array>
            <key>ConditionListMatch</key>
            <string>All</string>
         </dict>
         <key>ElseActions</key>
         <array/>
         <key>MacroActionType</key>
         <string>IfThenElse</string>
         <key>ThenActions</key>
         <array>
            <dict>
               <key>ActionUID</key>
               <integer>16142241</integer>
               <key>KeyCode</key>
               <integer>36</integer>
               <key>MacroActionType</key>
               <string>SimulateKeystroke</string>
               <key>Modifiers</key>
               <integer>0</integer>
               <key>ReleaseAll</key>
               <false/>
               <key>TargetApplication</key>
               <dict/>
               <key>TargetingType</key>
               <string>Front</string>
            </dict>
         </array>
         <key>TimeOutAbortsMacro</key>
         <true/>
      </dict>"
	
	tell application "Keyboard Maestro"
        ...etc

Keyboard Maestro got an error: Canโ€™t make or move that element into that container.
When using the following.
And, is there a way to make it loop thru the selected macros?

property LF : linefeed
--------------------------------------------------------
try
	set actionXML to "<dict>
         <key>ActionUID</key>
         <integer>16142225</integer>
         <key>Conditions</key>
         <dict>
            <key>ConditionList</key>
            <array>
               <dict>
                  <key>Application</key>
                  <dict>
                     <key>BundleIdentifier</key>
                     <string>com.apple.TextEdit</string>
                     <key>Name</key>
                     <string>TextEdit</string>
                     <key>NewFile</key>
                     <string>/System/Applications/TextEdit.app</string>
                  </dict>
                  <key>ApplicationConditionType</key>
                  <string>Active</string>
                  <key>ConditionType</key>
                  <string>Application</string>
               </dict>
            </array>
            <key>ConditionListMatch</key>
            <string>All</string>
         </dict>
         <key>ElseActions</key>
         <array/>
         <key>MacroActionType</key>
         <string>IfThenElse</string>
         <key>ThenActions</key>
         <array>
            <dict>
               <key>ActionUID</key>
               <integer>16142241</integer>
               <key>KeyCode</key>
               <integer>36</integer>
               <key>MacroActionType</key>
               <string>SimulateKeystroke</string>
               <key>Modifiers</key>
               <integer>0</integer>
               <key>ReleaseAll</key>
               <false/>
               <key>TargetApplication</key>
               <dict/>
               <key>TargetingType</key>
               <string>Front</string>
            </dict>
         </array>
         <key>TimeOutAbortsMacro</key>
         <true/>
      </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

thanx man, I found my old code and modified, and with your help it's working.
It's looped over the selected macros and makes it green.

try
	
	set actionXML to text 2 thru -2 of "
   <dict>
		<key>ActionColor</key>
		<string>Green</string>
		<key>ActionUID</key>
		<integer>16142412</integer>
		<key>Conditions</key>
		<dict>
			<key>ConditionList</key>
			<array>
				<dict>
					<key>Application</key>
					<dict>
						<key>BundleIdentifier</key>
						<string>com.apple.TextEdit</string>
						<key>Name</key>
						<string>TextEdit</string>
						<key>NewFile</key>
						<string>/System/Applications/TextEdit.app</string>
					</dict>
					<key>ApplicationConditionType</key>
					<string>Active</string>
					<key>ConditionType</key>
					<string>Application</string>
				</dict>
			</array>
			<key>ConditionListMatch</key>
			<string>All</string>
		</dict>
		<key>ElseActions</key>
		<array/>
		<key>MacroActionType</key>
		<string>IfThenElse</string>
		<key>ThenActions</key>
		<array>
			<dict>
				<key>ActionUID</key>
				<integer>16142413</integer>
				<key>KeyCode</key>
				<integer>36</integer>
				<key>MacroActionType</key>
				<string>SimulateKeystroke</string>
				<key>Modifiers</key>
				<integer>0</integer>
				<key>ReleaseAll</key>
				<false/>
				<key>TargetApplication</key>
				<dict/>
				<key>TargetingType</key>
				<string>Front</string>
			</dict>
		</array>
		<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 end
					# 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

Yay!

Get rid of those extra new lines front and back of your XML text and you won't need to substring it.

1 Like

ok, I replied with a like, but honestly, I have no idea what to do ... regarding your suggestion.... erg.... =)

You've got

   set actionXML to text 2 thru -2 of "
      <dict>
      ...
      </dict>
   "

...but you can get rid of the leading and trailing new-lines in the string so you don't have to substring it:

   set actionXML to "<dict>
      ...
      </dict>"
2 Likes