I Am Unable to Get This Action to Work With Instance Variables

Split String into Variables Test Macro (v10.2)

I am unable to get this action to work using Instance variables. It works with both local and global. What am I doing wrong?

Split String into Variables Test.kmmacros (6.2 KB)

The Split Text is a third party plugin, written by a KM user. It may not have been implemented to accommodate instance variables. You need to check this with the plug in's author @ianthekirkland

1 Like

I see you're using a RegEx to replace something with \n. I can't see what it is, but say it's "#", you can access each entry as items in an array. Say you want item 2:

That's interesting and and will work for me.

How would I replace the # to a \n which is a new line

Roger

Unfortunately you can't use line endings as array delimiters.

That plugin is probably the easiest way to "explode" multi-line text into multiple, one line each, variables.

My suggestion was based on you skipping the step where separate your input string by linebreaks and accessing the string itself. What are you replacing with \n in the RegEx?

There is a kind of way to do this in Keyboard Maestro as long as you first do a search and replace.

The search and replace is replacing the actual line endings with the characters "\n". Then "\n" can be used as the custom delimiter to split the Variable where the line endings were.

EXAMPLE Split Lines in Variable.kmmacros (4.7 KB)

1 Like

That's very cool!

1 Like

Well, yes... Plus hope that the original text doesn't have a literal "\n" in it. Admittedly unlikely -- unless you're munging text from this thread :wink:

But my clumsily-made point is that OP isn't doing a search and replace on line endings -- he's exploding a chunk of text into an unknown number of variables, one line in each. You could create a pseudo-array and then iterate through that, creating your variables on the fly, but that's more work.

While I like the simplicity of pseudo-arrays, I like letting someone else do the heavy lifting even more!

Thanks for all of the suggestions.

I have one final questions :innocent:

How do I count the number of lines that have been separated out?
Is there a way to count the number of \\n found in the string?

I tried looking at the LINES but could not understand how to make it work.

The input will be a variable number of lines and I need to know how many there were.

Thanks
Roger

Here's how I usually count lines:

Count Lines in Variable.kmmacros (21 KB)

Macro screenshot

Use the LINES() function on your original variable. So if it's still called Instance_Input:

LINES(%Variable%Instance_Input%)

...in a calculation field. Or in a text field:

%Calculate%LINES(%Variable%Instance_Input%)%
2 Likes

Ooh. I need to learn more about functions.

1 Like

I would add in a KM Filter to count the lines:

EXAMPLE Split Lines in Variable with Line Count.kmmacros (5.4 KB)

Click to Show Image of Macro

1 Like

Hi, All. :grinning:

Since the LINES() function and Line Count Filter action return 1 if a text variable list is empty (or even if it doesn't exist), I've created a simple subroutine.

In the version here, I've added the red actions for the purpose of demonstration.

Download: ๐—Œ.๐—น๐—ถ๐˜€๐˜โ‡พLineCount.kmmacros (11 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

Here's a calling macro.

Download: CALL ๐—Œ.๐—น๐—ถ๐˜€๐˜โ‡พLineCount.kmmacros (10 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

2 Likes

Siding with the others for a moment wrt using a pseudo-array -- @RogerW, how are you going to use these dynamically generated variables after "Split String" spits them out? It's doable, but adds a fair bit of complication.

This is looking more and more like a case where @Zabobon's search-and-replace followed by @noisneil's array access methods might be easier after all -- especially given that you can get the number of items in the array by accessing the 0th item: %Variable%Instance_Input[0]\n% in @Zabobon's example.

So what are you planning to do with your split input values?

1 Like

Your sub probably does more than this, but here's a "simple" action that sets Local_lineCount to the "correct" line count of Local_theText:

True Line Count.kmactions (647 Bytes)

For completeness:

LINES(%Variable%Local_theText%) > 1 ? LINES(%Variable%Local_theText%) : CHARACTERS(%Variable%Local_theText%) > 0 ? 1 : 0

"If there's more than one line, return the number of lines. Otherwise count the characters -- if that's more than 0 there's one line, else there are no lines."

1 Like

Hi, @Nige_S. Correct, it does do a little more: it gives one the option to count whitespace-only lines.

( expand / collapse )

Keyboard Maestro Export


The sub includes this action which achieves the same result since .(CHARACTERS(%Variable%local__List%)>0) returns 0 or 1.

Keyboard Maestro Export

LINES(%Variable%local__List%)*(CHARACTERS(%Variable%local__List%)>0)

Oooh... That's nice! (Puts in back pocket and tries to sneak away unnoticed...)

1 Like

Lol. Actually I've never tried the ternary operator in a Set Variable to Calculation action. That's also good to put in the back pocket.

1 Like