I want to transfer my tasks in text form from one app to another and that is really easy to do with KM. However the text that I get contains certain characters that I wish to remove. Here is the macro I have right now, all I need is to make the regex edit :
This essentially copies a task from my 'today' or some other list in 2Do to Unclutter. The task I get from the app is often of the form :
'- (!) this is just a task copied (Today)'
I want to remove '- ' from the beginning and the trailing ' (Today)' at the end to only paste the text I need. However I want to remove the '- ' only if there is (!) or (!!) or (!!!) that follows it. Here are some examples :
copied text : - (!) this is a task (Today)
I want : (!) this is a task
copied text : - this is a task (Today)
I want : () this is a task
copied text : - (!!!) this is a new task (Tomorrow)
I want : (!!!) this is a new task
As you may have guessed, the explanation marks are responsible for priority of the task. There is also a starred projects that I also copy and their structure is as follows :
text I get to clipboard :
✮ work on a project (Today)
do the first thing (2016-06-15)
do this later (2016-06-15)
then do this (2016-06-15)
I want to turn this to :
✮ work on a project
do the first thing
do this later
then do this
I am very new to Regex and I am certain this should be pretty trivial to do but I found the examples provided on the internet for a solution to my problem quite huge and they didn't seem to work. I will appreciate any help on this. It would be amazing if it will work as I wish, I really want to learn regex.
This is amazing. Thank you Dan. I just realised how nice it would be to have multiple windows in Keyboard Maestro because right now I had to switch between your macro and mine and copy things. Would be cool if I could be able to split it and see two macros at the same time. Perhaps there is a good alternative to this that is also efficient. I will try to figure out how to remove the trailing dates when copying the whole project, with your macro and regex101’s help I should be able do it. Thank you again.
One thing you could do, rather than have multiple windows open, is copy an action or actions and paste them into your macro. Then either use it like it is, or copy from the action that’s now in your macro. No switching needed.
I hope that makes sense.
Regular expressions are way cool, but it’s taken me years and years to get to where I am now, and I’ve just barely broken the surface. I didn’t know about regex101 until a month or two ago. Would have been nice to know it earlier!
Never say never, but currently it would be a horrendous amount of work to make that happen (it just was not initially designed for that and that choice has been hammered home over many years, so it will take a lot of work to make multiple windows or some sort of split view).
Note that you can swap back and forth between two macros by clicking the recently edited history button (or the arrows).
One thing you should always do is put a pause between a “Copy” action and accessing the clipboard. Pause for .3 seconds, or something like that. I doubt that’s your problem, though, but you never know.
There’s no mystery to how this works, once you got it working in the test code. So ask yourself what could be different, and test each assumption.
For example, put a “display text” right after getting the value from the clipboard. Is it the expected value?
Is it possible there’s some hidden characters in the text somewhere?
Did you type the name of the variable wrong somewhere?
Like I said, once it’s working in the test code, if you’re using the same input and regex action, then it has to work.
If you still have issues after looking at it for a while, select the actions in question, export them, post the file here, and I’ll look at it. Make sure you use the exact same actions as in your code, otherwise I’m not testing the right thing.
Yes, you are right about the string being different.
This is the string that gets copied '- write some emails (Today)' with a delimiter on the next line like so :
My regular expression doesn't take that into the account. I have found this resource and am attempting to really get how to make use of these things but it is still a mystery for now.
Can you help me modify the regular expression so that I just get bare text out of it. Bitbar only accepts simple strings that it can pass into the menu bar.
Thank you a lot if you do decide to help me with it and for all the help you have given already. I actually really like how easy it is to debug code in Keyboard Maestro with various pauses and 'display text' popups.
Btw, just for context, I want to have a dynamic menu bar that I can pass in tasks that I want to do and I am so close to making it happen with your help.
is not a regular expression (or, well, technically it is, but not for what you think it is.
What it is is some sort of search and replace expression in some sort of language, one part of which (the bits between the first and second /) is a regular expression.
Keyboard Maestro is not a text language (like perl), it is a graphical language, so the “s/”, “/”, “/” parts that are not part of the regular expression are done graphically in the Search & Replace variable action.
Also, the replace part, the \1 you use there is not a regular expression, it is an indicator in the search & replace in that language that you wish to insert the first (1) capture group (bit that has brackets around it in the regular expression). In Keyboard Maestro, that is represented in the replace section with $1, not \1 (roughly have of systems with search & replace for regular expressions use \1 and half use $1 - that’s life for you unfortunately).
So, with that in mind you need to:
Use the Search & Replace variable action
Configured for Regular Expression searching
User the part between the first and second / as the regular expression
Use the part between the second and third / as the replace expression
Use $1 instead of \1
Understand about capture groups so you know what the $1 means.
Your test case had the (!!) at the start, the copied text does not. And since your regular expression matches against the ) character (ie a literal closing bracket) and that is not there, no match occurs.
Sometimes the string that comes in my clipboard can have multiple lines like this :
‘✮ make tea (Today)
use the new tea you ordered’
Since Bitbar only accepts one string to display, I want to have a conditional statement that checks if the string in the clipboard is one or two lines and if it is two lines, I guess I can split the string in two, so the above string will be split like so :
first string : ''✮ make tea (Today)'
second string : ‘use the new tea you ordered’
How can I make this split and also check if the original string that I get contains one or more lines?
The regex to match one or more line breaks would be [\r\n]+
“.” does not match line break characters.
So you can match against (.+)[\r\n]+(.+) to match two lines. And you can use the Search Variable action to get the results of the two capture groups to two variables. that action will fail if the search does not match, so you can turn off displaying and aborting for the failure, and test the %ActionResult% token after the failed search, or you can test for a match first and then do two different things based on one line or two. Ie, either:
if the variable matches (.+)[\r\n]+(.+)
Search Variable for (.+)[\r\n]+(.+) into Line 1 and Line 2
do stuff with both lines
do stuff with one line
Search Variable for (.+)[\r\n]+(.+) into Line 1 and Line 2 - do not abort or notify on failure