Pasting a number in pairs of 2

Hey all!

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?

Thanks for your help!

Best,

Stevie

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.

When finished, you will:

  1. Copy the time code text to the clipboard
  2. Switch to the Filemaker app
  3. Click in the first field
  4. Run the KM Macro

1 Like

Hey JMichael!

Wow, thanks a lot, that get’s me more than started!

Cheers,

Stevie

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?

EDIT: solved it with this workaround!

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.

@peternlewis, is this 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)

I'm working on a RegEx solution, which will be much better.
Just have a few KM kinks to work out. :smile:

Awesome! I really should dig into RegEx, seems to solve most problems in an elegant way :smiley:

A little regex.

-Chris

Generic-Test 01.kmmacros (2.1 KB)

2 Likes

Wow, awesome, that worked! Thanks a lot guys! :nada:
However, I have no idea how that regex works!
I tried to get behind it. Can you elaborate?

Yes, it is by design. It is a calculation that you are performing, and so 5+6 would be evaluated to 11, and 0005 would be evaluated to 5.

Hey Stevie,

The whole pattern:

(\d{2}):(\d{2}):(\d{2}):(\d{2})

The components of the pattern:

(…)   ==  Capture
\d    ==  Digit
{…}   ==  Enumeration
\d{2} ==  2-digits

Breakdown of the capture group followed by the literal colon character:

(    # begin capture
\d{} # 2-digits
)    # end capture
:    # literal character (colon)

In the Keyboard Maestro search variable action each capture-group may be assigned to a variable.

-Chris

1 Like

Chris, what would I use if I wanted all characters between the ":" delimiter?

Usually you would use: [^:]

[] is a character set.
^ means "everything except"
and colon is standing for itself.

So [^:] means “match any character except colon”

So ^([^:]+):([^:]+):([^:]+):([^:]+)$

The first ^ means “start of line” and the $ at the end means "end of line"
Each () means a capture group, capturing one or more non-colons

1 Like

Peter, this seems to work:

###But, when I add to the input/source string, things seem to fall apart:

So, I'm confused. Please clarify.

The second, failing, example has source text with six fields, but you only allow four fields. There is no way for all five colons to be matched.

Hey JM,

Any character NOT ":". one or more.

[^:]+

-Chris


For Each Substring Match → Colon-Delimited Fields.kmmacros (4.1 KB)

2 Likes

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?

Hey JM,

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.

-Chris

@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.