Open parameter list?

Does this make sense, or is it just a theoretical exercise?

I often use macros with a long list of parameters and have wondered if they would be easier to manage. That's why I tried to design something like an open parameter list. I'm interested in finding out if this makes sense.

As a hypothetical example, here's this macro:

I created a frame for it:

It can be called like this:

I am aware that there is one drawback to this method: the parameter names must be correct.

This is the Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions

set kmInst to system attribute "KMINSTANCE"

(* get parameters from Keyboard Maestro, return if empty *)

tell application "Keyboard Maestro Engine"
	set KM_ParamStart to getvariable "instance_paramStart" instance kmInst
	set KM_Text to getvariable "local_Params" instance kmInst
	if KM_ParamStart is "" then
		return "ERROR: Variable 'instance_paramStart' is empty"
	end if
	if KM_Text is "" then
		return "ERROR: Variable 'local_Params' is empty"
	end if
end tell

try
	set theText to KM_Text
	set theLineSplit to KM_ParamStart
	set theLines to my splitText(theText, theLineSplit)
	repeat with aLine in theLines
		set aLine to aLine as string
		set aLine to my trim(aLine)
		if aLine is not "" then
			set theSplitted to my splitText(aLine, {": ", ":"})
			set theKey to item 1 of theSplitted as string
			set theValue to (rest of theSplitted) as text
			set theResult to theKey & "," & theValue
			set theVarName to "local_" & theKey
			tell application "Keyboard Maestro Engine"
				setvariable theVarName to theValue instance kmInst
			end tell
		end if
	end repeat
on error errorMsg
	return "ERROR '" & errorMsg
end try

return

(* ################################# trim ################################## *)

on trim(aText)
	if aText is "" then
		return aText
	end if
	local theResult
	set theResult to aText
	set theChar to first character of theResult
	repeat while theChar = space or theChar = tab or theChar = return or theChar = linefeed
		if length of theResult = 1 then
			return ""
		end if
		set theResult to text 2 thru (length of theResult) of theResult
		set theChar to first character of theResult
	end repeat
	set theChar to last character of theResult
	repeat while theChar = space or theChar = tab or theChar = return or theChar = linefeed
		set theResult to text 1 thru ((length of theResult) - 1) of theResult
		set theChar to last character of theResult
	end repeat
	return theResult
end trim

(* ############################### splitText ############################### *)

on splitText(theText, theDelimiter)
	set AppleScript's text item delimiters to theDelimiter
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to ""
	return theTextItems
end splitText

The three macros:

Open parameter list.kmmacros (15.9 KB)

Not sure if this is of use... but when I have a subroutine that needs to return several Parameters I always separate the returned individual items with the Pipe symbol "|" like this:

The Subroutine in the calling Action saves the results to a single Variable:

Then I use Keyboard Maestro's custom delimiter function with the Pipe Symbol "|" as the delimiter to extract those items to Variables in the calling Macro:

I used this approach as well. What bothered me about it was that I had to follow a specific order and list all the parameters.

KM can write a set of variables of your choosing to a JSON dictionary string with the %JSONFromVariables%prefix% token.

JSONFromVariables

If you pass the resulting JSON string into a single subroutine parameter named something like, localJSONDictionary, you could pass in just the named parameters you want.

Then, in the subroutine you could use "Set Variables to JSON with Prefix” action to expand the named values of localJSONDictionary back into variables, where you can use them as usual. (I think that is what your AppleScript is doing.)

Set Variables to JSON

Details:

In your calling macro, limit which set of variables to write by prefixing the desired variables with the same prefix, eg. localP_

Name the variable to receive the JSON dictionary string to something like localJSON. Set its value to the token, %JSONFromVariables%prefix%.

If the names of variables you want to use as parameters for the subroutine all begin with “ localP_”, your token would look like: %JSONFromVariables%localP_%.

KM will write the keys of the dictionary using the variable names minus the identifying prefix, here LocalP_

localJSON will then contain the JSON dictionary string.

So:

{"FirstName":"Paul","LastName":"Smith","Location":"London"}

Any variables that that have an empty value—such as might occur in a Custom User Input Form—will be left out of the dictionary (which is nice bonus, if you want to use "Is Empty" in an If/Then/Else action to validate a variable).

Pass localJSON into the a subroutine looking something like this:

In the subroutine unpack your variables by passing the input parameter containing the JSON string—which will always be the required JSON dictionary—to "Set Variables with Prefix to JSON” action. Choose a prefix to prepend to the all of the variables, such as "local_"

You will now have variables like, local_FirstName, local_LastName, local_Location, ready to use as usual.

Here's the full example.

Image

Subroutine with JSON Parameter.kmmacros (7.5 KB)

That's an interesting approach—thanks!
I didn't realize that “%JSONFromVariables%localP_%” could be used that way.

You're welcome!

More here:

JSON Manual

A nice little tutorial is here:

[How to] Pass Multiple Parameters to a Macro