I’m new to Keyboard Maestro but I have some experience with Autohotkey.
However, I need a macro on my mac.
I have a “number” (it’s actually a video timecode separated by colons) which looks like that:
10:12:34:11
Now, what I want to do is, to copy that whole “number” from a text file and paste it in 4 different fields in Filemaker (via tab). Like: 1st field: 10, 2nd field 12, 3rd field 34 and 4th field 11.
Is there an elegant way to solve this?
This is NOT the complete macro, but should serve to illustrate the basics of what you need. Basically, convert the input text into an KM array by replacing the colons with commas. Then you can refer to each array element using the KM Calculate function: %Calculate%Test1[1]%
Then using the Insert Text by Pasting, and Type a Keystroke (TAB), you can enter each field.
There's a small problem that I just noticed. When I got a TC like 10:00:00:00, KM will replace the two 0s with one 0. Is there a way to force KM to keep the double 0s?
If you’re happy with your fix, then stick with it.
I just did a test and I see that it also converts “05” to “5”, so it is evaluating array elements as numbers. I’m not sure if that is a bug or by design.
But if you really wanted to go the extra trouble, you could do a RegEx with the matching results going into 4 separate KM vars. This would treat the “00” as characters.
Yep you are right, I completely forgot about the numbers with a leading zero. Yep, treating them as variables might be the only way to solve this. I'll see what I come up with.
EDIT: okay fixed!
Since I'm such a noob with RegEx, I tried something else.
Btw, since the leading 10 always stays the same, I'm only pasting MIn (minute in), SIn (second in), FIn (frames in)
Chris, this is awesome. Thanks for going to so much trouble.
One question, if I wanted to set each of the matches to s different KM var, would I need to call a script to do that?
BTW, I’m assuming that based on @peternlewis’ post elsewhere that the %Calculate% function will always return an array element as a number, that I need to put each match into a separate KM var.
It would be awesome if I could put into a KM array, and then paste each array element separately as text. Is there a way to get an array element, like MyVar[3], which is “05”, and have it return “05” and not the number 5?
As I understand it Keyboard Maestro’s arrays are not real arrays. KM just intelligently splits strings based upon certain rules. (Peter will correct me if I’m wrong.)
One thing you can do is search/replace your delimiter to split your text into separate lines and then manage each of the lines with your method of choice.
I don’t think you can get a specific paragraph in KM with a substring. You can with a regular expression of course, or you can iterate through the paragraphs with a For Each action.
When I want to get fancy I’m going to use AppleScript and the Satimage.osax.
Requires the Satimage.osax.
set theInput to "10:12:34:11"
try
set myList to find text "\\d+" in theInput with regexp, all occurrences and string result
on error
return false
end try
--> {"10", "12", "34", "11"}
set myList to splittext theInput using ":"
--> {"10", "12", "34", "11"}
set theInput to "10 : 12 :34: 11"
set myList to splittext theInput using "[[:blank:]]*:[[:blank:]]*" with regexp
--> {"10", "12", "34", "11"}
# How to get the length of the list.
set listLength to length of myList
set val01 to item 1 of myList
set val02 to item 2 of myList
set val03 to item 3 of myList
set val04 to item 4 of myList
Now then – a simple delimiter is easy to handle with vanilla AppleScript.
set theInput to "10:12:34:11"
set AppleScript's text item delimiters to ":"
set myList to text items of theInput
--> {"10", "12", "34", "11"}
If you want more focussed advice then give me a clear idea of what you’re trying to do.
@ccstone is correct - Keyboard Maestro’s calculation engine supports arrays primarily to allow for things like points an rectangles. There are only numeric arrays, which are just strings with numbers separated by commas. There are no text arrays - if you want that you need to use a different method, either a script or a For Each action or some sort of regex or whatever.