Hello!
I am hoping to create an action that determines if a variable is 10 digits or more than 10 digits then will execute one action or another based on that factor. Any help is greatly appreciated!
Hello!
I am hoping to create an action that determines if a variable is 10 digits or more than 10 digits then will execute one action or another based on that factor. Any help is greatly appreciated!
There will be many ways to do this. My way is probably not the best or easiest to understand. But my way lets you do it in one action.
Basically it counts the number of digits by piping the Variable through a sed filter in Unix removing anything that isn't a digit then using the wc utility to count how many characters are left. Then it compares the result to 10 and branches as you wish.
It's the way I do things, but if you want a different way, or simpler way, you can ask.
P.S> I didn't test it! I'll be embarrassed if there's a typo.
P.S> Technically you don't need the sed component if we can assume the variable already contains digits.
P.S> another minor variation that might be clearer is to replace the line of text above with this line:
grep -o "[[:digit:]]" | wc -l
P.S. Yet another solution might be this:
I would consider the above solution to be humorous because it calculates the number of digits using a binary calculator.
To be most helpful, we need real-world examples of your source data.
For now, I will assume the digits of interest could be anywhere in the source string. The RegEx will only look for the first set of digits.
Here is an example macro to show you the technique. You will need to adapt for your specific needs.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~ VER: 1.0 2019-06-26 ~~~
Branch (IF-THEN) Based on Number of Digits [Example].kmmacros (9.9 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
Author.@JMichaelTX
PURPOSE:
HOW TO USE
MACRO SETUP
REQUIRES:
TAGS: @RegEx @Strings
USER SETTINGS:
ACTION COLOR CODES
USE AT YOUR OWN RISK
In the interest of alternative solutions, here's what came to my mind to solve this problem:
I wanted to see if I could find another solution. All solutions do more or less the same thing but this one uses AppleScript:
This solution makes no attempt to filter out non-numeric characters. It was never clear if that was a requirement of the original question.
By the way my knowledge of AppleScript is so lame I don't think I'm even qualified to properly explain how this works. I just poked around until it worked.
Here's a new solution that should work but it doesn't quite work, apparently because KM appears to have a limit of about 9.9 decimal digits of accuracy.
The problem is that in KM, LOG10(999999988) gives 8.99999999 and LOG10(999999989) gives 9.0. Of course neither answer is correct, which isn't the problem, because they are rounded answers but the rounding is just slightly to weak to allow this solution to work here. Otherwise it's a pretty neat trick. It doesn't require using any sort of string LENGTH function.
I tried googling what the mathematical precision of KM calculations really is, but couldn't find an answer. Perhaps one of the KM wizards here can answer this. Based on my calculation above, it appears to be just shy of ten digits of precision.
You guys are amazing! I'm sure I'll be able to find a solution from your responses when I have a moment. If I have any questions along the way I know where to look. Thanks again!
This is not technically accurate.
Keyboard Maestro uses “doubles” for calculations, which has about 15 significant figures of accuracy.
However, numeric results only include 8 decimal places of answer, so anything smaller than that will be rounded.
But you can, for example, increase the value to get more precision:
The precision there is about 15 digits (up to the 0). The digits after that are imprecise.
So accurate would be to say Keyboard Maestro has roughly 15 significant figures and up to 8 decimal digits.
Thanks gglick! I think this one is on the right track for me. However I actually probably worded my original question wrong in the first place. I need this regex to count the number of 'characters' not just ' 'digits' because there will be a mix of letters and numbers. Is that possible?
You need to provide us with some real-world examples of your source text, so that we can provide better answers and don't waste anyone's time.
How would you manually identify the start and end of the string of numbers and letters to be counted?
Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?
It's definitely possible, but if you want to count a mixture of characters including but not limited to digits, and the variable you will be using only contains the text you want to count, I would go with either a Character Count Filter based macro like @JMichaelTX showed you, or one like this that takes @Sleepy's idea and modifies it to use a different function explicitly for counting the number of characters in a string:
Switch:Case Based on Number of Characters.kmmacros (2.6 KB)
If this doesn't resolve your question though, I second @JMichaelTX's advice in his last post and encourage you to read the link he shared and provide more details about your workflow and goal.
Thanks Gabe! This solution worked amazingly.
You guys rock!