Scripting Error: Invalid Object when selecting action

So I have a macro set up to insert customized actions into my macros, it's a sub macro that is called by other macros that pass along the custom xml so that this macro can insert the custom action into the action list. It works great, but the action is not "selected" after it is inserted and for the life of me I can't figure out how to get that guy selected!

Ideally I would just use the select command from the Dictionary but locating the newly created action ID so it can be selected seems to be my issue.

Here's the sub macro I've been using without attempting to select the action.

Here's another version I've been working on to try to select the action after its inserted (similar but I thought creating a variable to represent the action would do the trick).

But I get this error in Script Debugger:

The thing that's driving me nuts is if I copy the action id directly from the action's properties, and do the command "select action id blah blah", that works fine, but when I try to get the script to locate the ID on its own directly after the action is inserted, it acts as if it doesn't exist.

It's very likely I'm missing something here, but after like 3 hours I figured I'd ask. Any thoughts?

Hi Kevin,

Before we try to solve this scripting issue, I'd just like to ask if you've come across @DanThomas's excellent KMFAM before:

It's designed to let you store and insert custom actions, and is much faster than writing a new script or tweaking an existing one every time you want to make a new one. It also selects the newly inserted action(s) in the way you describe. If you haven't already, I highly suggest giving it a try and seeing if it doesn't work better for your needs.

I have!
Ive used it for probably a year or so and It is absolutely superb and far better than what I can do, this is more of a pet project in a way, just seeing what I can do and how to better interact with Keyboard Maestro, know what I mean?

I think you first need to select:

  • macro group
  • macro
  • action

I think you first need to select:

  • macro group
  • macro
  • action

Something like:

set mg to macro group id "..."
select macro 1 of mg
select action -1 of item 1 of (get selected macros)

So, probably something like this could work:

tell application "Keyboard Maestro"
	set mg to make new macro group with properties {name:"Multiple Clipboards"}
	tell mg
		repeat with i from 1 to 9
			set m to make new macro with properties {name:"Copy " & i}
			tell m
				make new trigger with properties {xml:"<dict>
						<key>FireType</key>
						<string>Pressed</string>
						<key>KeyCode</key>
						<integer>8</integer>
						<key>MacroTriggerType</key>
						<string>HotKey</string>
						<key>Modifiers</key>
						<integer>4096</integer>
					</dict>"}
				make new action with properties {xml:"<dict>
						<key>Action</key>
						<string>Copy</string>
						<key>MacroActionType</key>
						<string>ClipboardSwitcherMacroAction</string>
						<key>RedundandDisplayName</key>
						<string>Clipboard " & i & "</string>
					</dict>"}
			end tell
			set m to make new macro with properties {name:"Paste " & i}
			tell m
				make new trigger with properties {xml:"<dict>
						<key>FireType</key>
						<string>Pressed</string>
						<key>KeyCode</key>
						<integer>9</integer>
						<key>MacroTriggerType</key>
						<string>HotKey</string>
						<key>Modifiers</key>
						<integer>4096</integer>
					</dict>"}
				set mo to make new action with properties {xml:"<dict>
						<key>Action</key>
						<string>Paste</string>
						<key>MacroActionType</key>
						<string>ClipboardSwitcherMacroAction</string>
						<key>RedundandDisplayName</key>
						<string>Clipboard " & i & "</string>
					</dict>"}
			end tell
		end repeat
	end tell
	select mg
	select m
	select action -1 of item 1 of (get selected macros)
end tell

So you were on the right track! But the only problem is this last line will select the action just added only when you add an action to the end of an action list.
So for instance if you were adding an action in the middle of a list of actions, it will then select the last action in the action list, rather than action just added.

But after playing around a little more, I found a way to get it to select the action just added no matter where the action is inserted. The solution isn’t as elegant as I would like, but the key is it works.

So the way I got it to work was to select the last item of the original selection (because the selection could be multiple actions), and then use system events to press the down arrow to select the proceeding action we just added.

tell application "Keyboard Maestro Engine"
   set thexml to getvariable "thexml"
end tell

tell application "Keyboard Maestro"
   set selList to selection
   
   set mg to first macro group whose selected is true
   tell mg
      set m to first macro whose selected is true
      
      tell m
         
         if class of item 1 of selList is action then
            set a to make new action at after last item of selList with properties {xml:thexml}
         else
            set a to make new action with properties {xml:thexml}
         end if
         
      end tell
   end tell
   select last item of selList
end tell

delay 0.1

tell application "System Events"
   tell process "Keyboard Maestro"
      key code 125
   end tell
end tell

if anyone would like to weigh in on why there appears to be a scripting error when after an action is created via scripting, that action is unable to be selected by using the command “select” I’d love to hear it!

Glad it helped a little.

When I run this code in Script Debugger, I get "Invalid Object Reference". Apparently, make command is not returning a valid object reference to the newly created action.

13 copia 3

Maybe @peternlewis will know what's happening.

26 copia

As a workaround, you could set name of the new action, as in:

make new action with properties {name: strName}

Then, retrieve that action with a statement like:

first action of macro 1 of macro group 1 whose name = strName

Hey Kevin,

This is a bug and will soon be fixed.

-Chris

Ah! Good to hear I’m not just crazy!