Count Characters in String Macro (v10.2)

Count Characters in String Macro (v10.2)

Count Characters in String.kmmacros (3.4 KB)

Trying to familiarize with Applescript syntax. I am attempting to get the count of characters in a string and return the result to a variable.

This seems to work just fine if I perform the count in the Applescript using a literal string but having a problem when attempting to pass the string as a variable. It always returns 0 so I know I'm doing something wrong here just not certain what.

Within the script I've tried counting all possible permutations of the variable but hitting a blocker.
I'm sure the answer is pretty simple and I "think" I'm not that far off.

What's missing?

The method you’re using to get the variable into your script works for global variables. Since you’re using a local variable you need to use the slightly different method shown in the KM wiki here AppleScript [Keyboard Maestro Wiki]


Not having much luck trying to implement the logic for using Local variables inside the script and returning the result even after using the examples but admittedly I have zero experience working with AS.

Could the posted macro be modified with the appropriate way this should be done as an example? I'd likely have a better understanding of how this should work.

@tiffle never mind that last request. I just figured it out

Count Characters in String Macro (v10.2)

Count Characters in String.kmmacros (3.7 KB)

Appreciate the lead!

On observation, apparently "Save to variable" has no value in this scenario. So I just changed the Script component to "ignore results"

So in what instance would saving to the variable be of any significance using AppleScript??? :face_with_diagonal_mouth:

Instead of using the set variable statement replace it with

return strCount as string

and that value will then be available in Local__VarName that you’ve set up in the Save to variable field in your Execute AppleScript action.

Alternatively, set the KM variable this way:

setvariable "Local__FromAS" instance kmInst to strCount as string

KM variables can contain text only, so strCount needs to be converted from an integer to a string

1 Like