Subroutine Needs to Pass Back Two Variables

I built a subroutine that needs to pass back two variables while the subroutine action can only pass back one variable.

In reading, I have learnt that there are four ways of passing back multiple variables:

  1. The use of instance variables.

  2. The use of Jason strings.

  3. The use of indexed arrays.

  4. Concatenate the variables into one text string with a common delimiter — such as | —, pass back the concatenated variable and then separate using a RegEx.

I am curious as to what the best practice / preferred approach is which I understand may depend on the situation and use case.

I would appreciate an explanation as to the particular circumstances what the preferred approach is and why.

Thank you.

Depends what you’re doing with it on the receiving end, but I often use variable arrays with a custom delimiter, like:

Pass someVariable: item1§item2

%Variable%someVariable[1]§%item1

%Variable%someVariable[2]§%item2

One advantage of a custom delimiter is being able to pass values that include commas.

2 Likes

Totally data dependent and situational as to which is "best".

Instance variables save you from returning a combined value and "decoding" it in your main macro, but break a fundamental philosophy of subroutines -- independence. You need to use the same variable names in both main and sub which not only means you have to remember/find out the names to use but can, for subs you use across multiple macros, lead to misleading rather than descriptive variable names in your various mains.

JSON can solve many problems -- but adds the problem that you have to write, then read back, JSON :wink:

Pseudo arrays are easy but, as @evanfuchs points out, you need to be sure your data won't contain your array delimiter. Remember that the delimiter is a string, not a character, so you can use something like ==myKMSeperator==. If Local_myArray is:

cat==myKMSeperator==dog==myKMSeperator==fish

then:

%Variable%Local_myArray[2]==myKMSeperator==%

...gives you dog.

There's no point incurring the extra overhead of regex when you know the delimiter and that it won't also occur in your data -- that's what pseudo arrays are for.

2 Likes

Yes, it's in the manual.

No, I had no idea.

1 Like

@evanfuchs and @Nige_S

Appreciated and excellent.

I went with the Array approach and it worked great.

In trying to make things are easy (and as fast) as possible, I am in now in the habit of reading the WikI to get details of the action I want to use. It was clear in the manual that the delimiter is a string.

It worked great, thanks to both!!

3 Likes

You might find this macro handy:

I wouldn't use my original version (which uses a subroutine as I use that bit elsewhere in other macros), but @noisneil's modified all-in-one version. You can easily add more Wiki sections to the pop-up menu just by adding them to the entries in the Prompt With List action.

I love having the Wiki page I need just a click or two away, and usable while I edit.

-rob.

1 Like

@griffman

Greatly appreciated!!I will add it first thing this afternoon, I need to head out for a bit! Thanks!!

@griffman

Great macro, I will make very good use of it. Just need to change the co-ordinates a but in make it better fit my screen.

I tried to reposition it using Moom. It responded to the Center command but not the Move Right command. Thinking it is because the window is a fixed size but will reposition things molar it work!

Will save it as a layout once I get the window properly sized to make it work.

Thanks for sharing.

If you're using @noisneil's version, just change the size as needed in the AppleScript.

-rob,.

Agreed, I saw that, I just need to figure out the positioning on my screen as to what those co-ordinates are.