Help make work applescript macro to `create new global macro` in KM

Original code by @ccstone

----------------------------------------------------------------
# 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

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

If I run above like this:

It creates new macro, but I get this error too:

I tried editing the code but can't get error to go away and still have logic be right.

What is the problem which you are aiming to solve ?

What aspect of the behaviour of @ccstone's macro did you want to modify ?


FWIW I would personally skip the GUI interactions and keep to the "Keyboard Maestro Editor" Application model that's available to osascript (i.e. AppleScript | JavaScript for Automation)

Using a Keyboard Maestro Execute JavaScript for Automation action, or testing in Script Editor.app with the language selector at top left set to JavaScript (instead of AppleScript), it might look like something like this:

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    // --------------------- OPTIONS ---------------------
    const groupName = "Global Macro Group";
    const newMacroName = "Some New Macro";

    // --------- NEW MACRO CREATED AND SELECTED ----------
    const
        kme = Application("Keyboard Maestro"),
        macroGroup = kme.macroGroups.byName(
            groupName
        );

    return macroGroup.exists()
        ? (() => {
            const newMacro = kme.Macro().make({
                macroGroup
            });

            return (
                newMacro.name = newMacroName,
                kme.select(newMacro),
                kme.activate(),
                JSON.stringify(
                    {
                        groupName: macroGroup.name(),
                        macroName: newMacro.name(),
                        macroUUID: newMacro.id()
                    }, null, 2
                )
            );
        })()
        : `Parent group not found: '${groupName}'`;
})();


New Global Macro Created and Selected.kmmacros (2.7 KB)

2 Likes

For me, at least, there isn't a window titled "Keyboard Maestro Editor" -- the title is always postfixed with whatever is selected, eg "Keyboard Maestro Editor -- Global Macro Group".

So you might get away with changing:

tell window "Keyboard Maestro Editor"

to:

tell item 1 of (get every window whose name starts with "Keyboard Maestro")

But all that part of the script is doing is setting the current focus to the "name" field of the new macro -- and that'll happen anyway if you end by telling KM to edit the macro:

tell application "Keyboard Maestro"
	activate
	tell macro group "Global Macro Group"
		set theMacro to make new macro with properties {name:"New Global Macro"}
		edit theMacro
	end tell
end tell

If you know the name you want to use you could put the name in when the macro is created:

set theName to text returned of (display dialog "Name your new macro:" default answer "")

tell application "Keyboard Maestro"
	activate
	tell macro group "Global Macro Group"
		set theMacro to make new macro with properties {name:"New Global Macro"}
		edit theMacro
	end tell
end tell

...or you could pass that in as a variable from a KM prompt.

1 Like

this is beautiful and works, thank you

is it possible to make it focus on the macro name input after?

I tried your trick with tell item 1 of (get every window whose name starts with "Keyboard Maestro").

It still fails for me.

I have this applescript

try
	set macroGroupName to "Global Macro Group"
	
	tell application "Keyboard Maestro"
		
		if macro group macroGroupName exists then
			select macro group macroGroupName
		else
			error "Cannot select macro group " & macroGroupName & " — it is missing!"
		end if
		
	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

try
	
	tell application "System Events"
		tell application process "Keyboard Maestro"
			tell window "Keyboard Maestro Editor"
				tell scroll area 2 of splitter group 1 of group 1
					set focused to true
				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

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

Fails with same error:

I tried to change it to:

try
	set macroGroupName to "Global Macro Group"
	
	tell application "Keyboard Maestro"
		
		if macro group macroGroupName exists then
			select macro group macroGroupName
		else
			error "Cannot select macro group " & macroGroupName & " — it is missing!"
		end if
		
	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

try
	
	tell application "System Events"
		tell application process "Keyboard Maestro"
		tell item 1 of (get every window whose name starts with "Keyboard Maestro")
				tell scroll area 2 of splitter group 1 of group 1
					set focused to true
				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

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

Now it fail with

This System Events tell block is working for me:

	tell application "System Events"
		tell application process "Keyboard Maestro"
			tell item 1 of (get every window whose name starts with "Keyboard Maestro Editor")
				tell scroll area 3 of splitter group 1 of group 6
					set focused of text field 1 to true
				end tell
			end tell
		end tell
	end tell

But I wouldn't waste any more time on it -- using edit theMacro as above, rather than going via System Events, should be more reliable.

if anyone knows how to do this, would be great, otherwise hacking it in with mouse movement.

ok answer to that is is Rename

image