Add Clipboard Lines to a Variable

Hi!
I have this clipboard:

Conform evangheliei după Matei, cu ce era îmbrăcat Isus când a fost dus spre răstignire?
a) Haina stacojie;
b) Hainele Lui;
c) Dezbrăcat;
d) O haină de in.

I want to add each line to a variable in order to be able to use it later.

P.S. clipbord (the text) wil change, but the structure remains the same.

Eg.
FirstVariable = Conform evangheliei după Matei, cu ce era îmbrăcat Isus când a fost dus spre răstignire?
SecondVariable = a) Haina stacojie;
...
...
fifthVariable = d) O haină de in.

Ive tried many macros... no succes

Are there always 5 lines? If so, it's easy. If not, it's much more complicated.

BTW, you are not sharing a macro, the post should be put in the Question category.

1 Like

First line(the question) sometimes is 3 lines. but the a b c.d pretty much the same

Does this meet your need? It sets the lines before a) to one variable.

MACRO:   RegEx

DOWNLOAD Macro File:

RegEx.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Result:
image

1 Like

Judging by the results you get, yes. I will try it.
Thank you!
Is there a way to learn that regex thing?

Yes. I knew nothing about RegEx a while ago. Now I can complete most job.

Some resources are mentioned in this post:

How Did You Learn RegEx (Regular Expressions)? - Questions & Suggestions - Keyboard Maestro Discourse

1 Like

I’m surprised nobody has suggested using the third party plug in “Split Text” for this problem. I’ve used it on a number of occasions. It’s by @ianthekirkland and you can find it here;

1 Like

Thanks for recommending it. I did not know about it. I know a similar plugin here:

The problem with the OP's request, however, cannot be solved by these plugins without further twist, because as he said:

1 Like

I will try it. Sometimes when a b c d is longer the KM action by martin doesn’t work.
But most of the time it does the job.

Hey Ciprian,

Since you're dynamically assigning a variable number of lines in the clipboard to individual variables RegEx is not really the best tool for this task. RegEx cannot dynamically change the number of captures in a pattern.

Here's a relatively simple macro that will assign each line in the clipboard to a numbered variable.

textVar_1
textVar_2
textVar_3
...

  • Copy your text.
  • Run the macro.
  • Examine the Variables panel in the KM preferences for the listed variables.

==EDIT – New version v1.03 posted 2021/03/27 16:51 CDT==

Dynamic Assignment of Variables v1.03.kmmacros (10 KB)

Now then – the pitfall with this is that the variables are not ever deleted.

That means that subsequent runs of the macro will overwrite the existing variables, and that can be problematic if it doesn't overwrite ALL of them – you can end up with extraneous variables.

So – we need to know a bit about your workflow to decide the best place to do variable housekeeping.

To delete them en-mass you can do something like this:

Execute an AppleScript.kmactions (788 B)

But not until you've processed them.

-Chris

2 Likes

I think what you try to do is copy the text and then reuse each line from a selection later ? If so , try, this you can select from popup instead.

The Prompt List action automatically break down each line as choice in Popup.

1 Like

Well, just a guess but I’d apply the plug in twice: the first to split the input at (a) to get two variables as output and then apply it again on the 2nd variable to get the (a) (b) (c) etc parts. That’s possibly easier to achieve for someone who isn’t familiar with regex.

YMMV

2 Likes

I don't really get it what each step is doing. I see that you've create a variable from clipboard, but I don't understand the var_VariableIndex and var_ClipboardLine what it does? in fact the entire For Each thing.
I Would like to understand each step.

What I do with the macro? I create online quizzes and it's quite exhausting to repeat for each question - Select first line... COPY... SELECT... PASTE... so on.

Okay, hopefully this will explain it adequately:

First and foremost I think you need to explain in more detail what your workflow is. The more we know about it, the more likely we'll be able to offer fruitful suggestions.

If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?

-Chris

2 Likes

I have written this macro in response to your request:

MACRO: Extract Q&A With Multiple Lines and Multiple Answers [Example]

It deals with all of the issues you have found above.

  • Provide solution to a dynamic string of Question and any number of Answers
  • The Question and Answers may contain multiple lines
  • Each Answer will be put in a separate KM Variable
  • You may have any number of answers, from 1 to 26

Please let us know if this meets your needs.

1 Like

Thank you. Very useful video. :pray:
From there I’ve managed to find a solution by my self. That was the hard part. And you guys provided so many solutions. Great community!

2 Likes

Guys you are great!
When I posted, it I never thought that I will receive so many solutions.
Great community here. Thank you!

2 Likes