Help editing AppleScript by @ccstone to create macro in Global Macro Group

I have this applescript that @ccstone kindly shared at some point, can't find original thread now.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/01/25 06:15
# dMod: 2019/01/25 07:07
# Appl: Keyboard Maestro
# Task: Create and Select a New Global Macro in Group "Global Macro Group"
#     : then Select the Macro Name Field .
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Create, @Select, @New, @Global, @Macro, @Name, @Field
----------------------------------------------------------------

try
	
	tell application "Keyboard Maestro"
		activate
		
		tell macro group "Global Macro Group"
			set newMacro to make new macro with properties {name:"New Global Macro"}
		end tell
		
		select macro group "Global Macro Group"
		select newMacro
		
	end tell
	
	tell application "System Events"
		tell application process "Keyboard Maestro"
			tell window "Keyboard Maestro Editor"
				tell group 1
					tell splitter group 1
						tell scroll area 3
							
							tell scroll bar 1
								if value ≠ 0 then set value to 0
							end tell
							
							if exists (first text field whose accessibility description is "Macro Group Name") then
								tell (first text field whose accessibility description is "Macro Group Name")
									set focused to true
								end tell
							else
								tell (first text field whose accessibility description is "Macro Name")
									set focused to true
								end tell
							end if
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell application (path to frontmost application as text) to set ddButton to button returned of ¬
				(display dialog e 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 e
		end try
	end if
end try

----------------------------------------------------------------

Which I ran like so:

The problem is that although it works, every time it throws this error:

System Events got an error: Can’t get window "Keyboard Maestro Editor" of application process "Keyboard Maestro".

Num: -1728

I tried ask GPT-4, it seems maybe KM window name changed?

I think the problem is that the window name now contains the name of the selected group or macro, so there is no window simply named "Keyboard Maestro Editor."

Unfortunately, I'm not enough of an AppleScript wizard to fix the problem :(. As a guess at a fix, what if you change the reference from a name reference to a number reference? Like this:

...
tell application process "Keyboard Maestro"
			tell window 1
...etc

Someone better at AppleScript than I will probably have a real fix soon :).

-rob.

I assume you mean this

	tell application "System Events"
		tell application process "Keyboard Maestro"
			tell window 1
				tell group 1
					tell splitter group 1
						tell scroll area 3
							
							tell scroll bar 1
								if value ≠ 0 then set value to 0
							end tell
							
							if exists (first text field whose accessibility description is "Macro Group Name") then
								tell (first text field whose accessibility description is "Macro Group Name")
									set focused to true
								end tell
							else
								tell (first text field whose accessibility description is "Macro Name")
									set focused to true
								end tell
							end if
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell

It fails with error too :frowning:

Oh well, that was my one suggestion :).

-rob.

See, for example:

1 Like