Compare strings?

I can’t find a way to compare two text variables to see if one is “less than” the other. Unless I’m doing something wrong, “if then else” with a calculation doesn’t appear to work for text variables.

I can’t believe this functionality doesn’t exist, so I must just be looking in the wrong place. Ideas?

It was not clear whether you want to compare numeric values in the variables, or compare the string length of the variables. So the below macro does both.

###MACRO: Compare String TEST
####Download:
Compare String TEST.kmmacros (14 KB)

Thanks for the response, but your assumption is incorrect. I’m comparing alphabetical strings. I want to maintain an ordered list.

I have a workaround (which involves files and shelling out a SORT command), and it works OK, but I’m just surprised I can’t find a way to do a simple string comparison like this. KM has SO MANY features - how could it be missing this obvious thing?

See my updated post. I now show a comparison of string lengths:

I don’t want to compare string lengths. I want to know, for example, if “Anderson” is less than “Black”. Alphabetically.

OK. What is your objective? Do you just want to sort a list of text?

OK, here’s the workflow: I put up a prompt with a drop-down list of choices. There’s also a variable to allow a new item to be added to the list. If the new item is entered, I want to stick it into the proper place in the list. The result is saved to a file for use the next time.

I will repeat: I have a workaround which works just fine. I don’t need another sorting solution, unless there’s a KM “sort” command, which I don’t believe there is. I really only wanted to know if there is a way to compare two strings alphabetically. I’m guessing by now that the answer is what I thought it was, which is “no”.

Thanks.

You can use this simple AppleScript:
I’ll leave it up to you to integrate with a KM macro, or whatever, and add any error checking you’d like.

set Test_Var_A to getKMVar("Test_Var_A")
set Test_Var_B to getKMVar("Test_Var_B")

if (Test_Var_A < Test_Var_B) then
	set resultStr to "true"
else
	set resultStr to "false"
end if

return resultStr

on getKMVar(pKMVarName)
	
	tell application "Keyboard Maestro Engine"
		set strKMValue to value of variable pKMVarName
	end tell -- "Keyboard Maestro Engine"
	
	return strKMValue
	
end getKMVar
1 Like

Thanks!

Correct, there is no alphabetical comparison facility in Keyboard Maestro. In part because it is a humungous can of worms to open - which order are any of these:

  • "and <=> “Alice”
  • “The Play” <=> “Rest”
  • “Hello” <=> “こんにちは”

Numerical comparison is well defined - alphabetical comparison is not without a lot more information.

For your workflow, assuming plain ASCII, I think I would use a variable with the list of choices separated by newlines, and use the sort command to sort it, and a search and replace to change the newlines into vertical bars for creating the popup menu.

But there are many possibilities, all equally valid.

Peter, I would think you could do the same way AppleScript and JavaScript do it. The same way the comparison is made for sorting:

  • Generally sorting is done case insensitive
  • So “Alice” would be < “and”
set var1 to "Alice"
set var2 to "and"

set compare to (var1 < var2)
--> true
1 Like

Peter -

I’m a developer; I have been for MANY years. I’m so completely impressed with KM that there are honestly no words to describe it. The amount of work that has gone into it is staggering. I wish I could rave about the technicality of it to my friends, but they’d just stare and their eyes would gloss over. So please understand that all my comments come from a place of deep respect for KM.

IMO, there’s no need to support every possible option. Just simple string comparison, optionally case-sensitive, is really all that’s required. That’s what almost everyone would expect. Don’t worry about character sets and the like. Just use the standard comparison operations the programming language provides, with default options (other than case-sensitiveness).

Or do it like Finder does. Whatever. It doesn’t have to be complicated. I realize that since KM handles SO MANY complicated conditions, there might be the tendency to want to handle every possible situation, but there’s really no need. If people request additional options, put them on the enhancement list - at the bottom if you want. :smile:

With all that said, for my situation, I’m fine. I already save my list out to a text file, so adding a command to shell out and sort the file is a no-brainer (thank you, KM). So, for now at least, I’m good. This is all just blue-skying.