Execute Macro Action: 2 suggestions

Hi Skillet,

Thank you for giving the script a whirl!

The script was meant to emulate clicking on the" Go To Macro" submenu of the drop down menu in Execute a Macro, Execute a Subroutine without waiting for the main menu to load. It does only that. (Additionally, for an Open URL action selection as well when set to Edit Macro style URL Scheme)

I know you wanted an option to Go to Macro in New Window.

The script's logic makes that more tricky than it should be. Sorry about that.

We can definitely change things to make it a bit more plug 'n play, while both simplifying the logic and keeping the AppleScriptObjC's verbosity down to a dull roar. :sunglasses:

If you can count on the user to run the macro only when one of the three actions is selected, follow @Nige_S advice and you're done.

If not, we'll need to separate the condition checking from the "Go To Macro" implemenation (open location, in this case)

That way you can perform the Go To Macro action only AFTER you've opened a new window, only IF conditions are met.

To do that, let's change the script:

  1. No more Go To Macro via "open location".

  2. Return only the macro id or "" to "Execute the AppleScript" action when conditions fail (or just place "error" in front of all of your display dialogs and set the "Execute an AppleScript" to Failure Aborts Action).

AppleScript
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property author : "@CRLF"

--┏━━━━━━━━━━━━━━━━━━━━━━━
--┃ PURPOSE: 
-- | In the Keyboard Maestro Editor,Extract a macro id from the first selected
-- | Execute a Macro, Execute a Subroutine or Open URL action.
-- | OUTPUT: macro id or empty string
--┗━━━━━━━━━━━━━━━━━━━━━━━

tell application id "com.stairways.keyboardmaestro.editor"
	if (count of (selection as list)) = 0 then return -- "Nothing is selected"
	set selection1 to item 1 of (selection as list)
	if class of selection1 ≠ action then return -- "The first selection is not an action"
	set dict to (current application's NSString's stringWithString:(xml of selection1))'s propertyList()
end tell

set {MacroActionType, MacroUID, URLScheme} to (dict's objectsForKeys:{"MacroActionType", "MacroUID", "URL"} notFoundMarker:"not found") as list
if MacroUID is not in {"not found", ""} then
	return MacroUID
else if URLScheme is not in {"not found", ""} and MacroActionType is "OpenURL" then
	if URLScheme begins with "keyboardmaestro://m=" then
		set MacroUID to macroUIdFromURLScheme(URLScheme)
		return MacroUID
	end if
end if

-- Handle extracting the macro id from a "Edit a Macro" style URL scheme
on macroUIdFromURLScheme(inputString)
	set inputString to current application's NSString's stringWithString:inputString
	-- Define the regex pattern
	set pattern to "[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}"
	
	-- Create the regular expression object
	set regex to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value)
	
	-- Find the first match
	set firstMatch to regex's firstMatchInString:inputString options:0 range:{location:0, |length|:(inputString's |length|())}
	
	-- Check if a match was found
	if firstMatch is not missing value then
		-- Extract the match from the input string using the match's range
		return (inputString's substringWithRange:(firstMatch's range)) as text
	else
		-- Return empty string if no match is found
		return ""
	end if
end macroUIdFromURLScheme

(Be sure the Save Results to Variable option is set.)

This way you use the script's results to:

  1. Decide what to do next. (Open a new window, display errors etc)
  2. Implement and run a "Go To Macro" action.

SO:

If empty string --> error dialogs and/or stop the macro.

If Non-empty string -->

  1. open new window
  2. make an URL Scheme variable: keyboardmaestro://m=%Variable%Non_empty_string%
  3. Open URL action using the URLScheme
Macro Screenshot

The macro with the script is at the bottom.

@Nige_S's the-"'old' way"-characterization of your method of setting the output variable works best for me.

The nuances of its behavior and usage are puzzled out in a long informative thread started by none other than forum's very own, well, you. :wink:

Link

Save an Empty Variable - #18 by skillet

Here's a cheatsheet (with no claims of completeness) of ways to output a value from AppleScript to Keyboard Maestro.

AppleScript examples
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aVar to "and more text"

--Output and exit the script.  
return "some text" & linefeed & aVar -- (👉🏽: https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript)

tell application id "com.stairways.keyboardmaestro.engine"
	--Set a km global/persistent variable and continue the script
	setvariable "aKMGLobalVariable" to "some text" & linefeed & aVar
	--Set a km "local" variable and continue the script
	setvariable "local_aKMVariable" instance (system attribute "KMINSTANCE") to "some text" & linefeed & aVar --prefixes "local" 
	--Set a km "instance" variable and continue the script
	setvariable "instance_aKMVariable" instance (system attribute "KMINSTANCE") to "some text" & linefeed & aVar --prefixes "instance" 
	--Make a km global/persistent var, and set its value--👉🏽
	set v to make new variable with properties {name:"aNewKMGLobalVariable"}
	set value of v to "some text" & linefeed & aVar
end tell

Go To Macro of Execute a Macro, Execute a Subroutine or Open URL action in New Window.kmmacros (8.0 KB)