Wait for a string of numbers (in my case a formatted date) to be entered, then continue executing the remaining actions

Hi there, I'm having to do a huge repetitive task which involves entering dates in a file > save as menu pop-up, and as soon as I finish typing the date, I would like it to add an underscore and press enter to save and close the window.

The date format I'm using is a pretty standard ISO 8601 - YYYY-MM-DD.

My macro is looking good otherwise so far, I've set it up to do a few other things at the opening of the popup (select to save as PDF etc) and add some additional text at the beginining which I need. Technically I'm actually having it type a string which starts me off with the year plus the hyphen (as in YYYY- since I'm doing extended batches per year at a time). So in other words the cursor leaves off right where I can enter the month and days which is exactly what I need to manually do for hundreds of files.

My question is, how can I have the macro wait until it sees that I've typed two numeric digits, a hyphen, and another two numeric digits? Then continue on with the remaining actions (add underscore, press enter to save).

I played around with the "pause until" conditions menu and tried various things, but nothing seems to work right. A date token isn't what I need, because in my case I'm manually entering numbers. As far as KM is concerned, I imagine, all it needs to see is that I've entered ##-## of any kind and then continue on with the rest.

Any ideas if this is possible? Thanks!

This topic has come up before, here. You might find useful advice in this thread....

However I'm still reading your post to see if I can give more direct help. It's a little tougher when we can't see your macro.

There is a feature in KM designed to help with this... here's what it looks like. Notice how it accepts digits only...

which comes from this action:

image

Hi, that's interesting. I've been using Keyboard Maestro for many years, but mostly for simplifying steps for common macros or text entry in my creative programs – I'm still just scratching the surface after all these years and I had no idea there's a prompt box available - that's pretty neat!

My question, then, is how to take data entered from that prompt and add it as text to the filaname where the cursor is currently located? Because I played with the date example above, and while I got it to pop up for date entry, after clicking OK it didn't do anything with this data.

Nonetheless, what I felt would be simpler, is for it to simply watch until certain conditions are met, which in my case, it sees I've typed ##-## (intergers and hyphen in this exact format).

I'm attaching the macro here. Note it's just for use inside Safari. The goal behind it is for saving invoices from various websites more quickly for filing away. So far in order to achieve this I have cmd+P for opening the print menu, it selects PDF as an option to save as, which then brings up the file save menu. Left arrow to start at the top of filename. Enter "EXP_YYYY-" which is my code for Expenses and the year I am just manually adding on a case-by-case basis (adjusting the macro when I need to), because I'm doing each year separately over a longer period of time.

The next step is manual entry. I look at the date on the page and type it exactly as MM-DD

When KM sees that I've completed this string format ##-##, I'd like it to add the underscore and press enter to save.

So far all those elements are working fine except the waiting for that exact interger format thing. Lemme know what you think, thanks!

Invoice to PDF.kmmacros (5.3 KB)

P.S. Go easy on me, I'm not a coder of any sort - I can figure out basic logic operations but that's about it :wink:

I'm always trying to be an easy going and polite individual. Perhaps I don't always succeed. This afternoon I'm dealing with family situations, so I can't respond right away.

1 Like

Hi no worries, I was just saying that really to the broader community that you may have to break things down into layman's terms and be patient with me - I can understand logical operations but code is somewhat over my head so I just wanted to add that as a disclaimer. No rush, this macro is designed for a long-term project which will also be for ongoing and future as well. Thanks!

Okay. I see what you're doing. I need a minute to find the best solution.

The "right" (safe) way to solve this problem is to have two triggers. The first one does the half of the process that occurs before the user does the typing, the second one does the half that occurs after the user does the typing. Remember, in some cases the user could start typing and not finish for a very long time, including switching apps, checking for Mail, playing a game, etc. So it's not easy to find an unfailing solution for those cases. But I'm pondering a couple of ways to give you what you want.

Okay I have a cool solution. My solution is predicated on the fact that your macro enters the second phase upon the entering of an underscore. I think I can set that up for you. Give me a few minutes.

Okay, the first four actions of your existing macro are keepable. That is, keep these:

Screenshot 2023-11-11 at 6.11.57 PM

Then you will need to create a second macro that contains this one action:

You should place this new macro in the same location as your first macro, so that it runs only when Safari is active. You can name it anything you want. Notice that I unchecked the checkbox that says "Simulate."

I think that's all you have to do. Give it a try. If it doesn't work, let me know. I'm responsible to help you.

Give this a try:

Invoice to PDF.kmmacros (6.5 KB)

Macro image

Note: It seems like your top priority is to minimize keystrokes, so this requests the format MMDD. This could be adjusted and simplified if you'd prefer to type the hyphen yourself.

1 Like

You can have a macro with the Typed String trigger with a regex of:

(\d{4}-\d{2}-\d{2})

that will match when you type the ISO date. Then in the trigger, turn off deleting (because you want the date) and then simply add a couple Type a Keystroke actions to type the _ and Return.

3 Likes

Hi there, this is the winner! Exactly what I had in mind and honestly even simpler with the pop-up window asking for date.

I really appreciate you taking the time to build this. It's actually a great learning tool, as I'm able to study every step in detail to better understand what it does and how it works, for modifying it in the future as well as creating new macros with similar ideas.

There's a few things I didn't realize I could do in KM until now - Pause until save button is enabled is going to be very handy. Also the use of Prompts which you can then define variables and then call those variables later on - like I said above I'm not much of a coder but these small items I can understand just by analyzing them a bit. Cool stuff, and this works great! I even just made a duplicate version for the full short entry (based on my US formatting - MMDDYY) which will then automatically reformat it to YYYY-MM-DD and proceed as the rest. I just used your substring setup to create a year, added it to where I need at the beginning. Got it working great. It's technically only adding YY, and I have "20" leading that variable of text, because in my particular use case, there is no reason or possibility I will need 19 or 21, so that works fine.

I deal with manual date entry all the time, I can definitely see myself using this for when I'm creating files and need to save with a specific date or maybe a specific ID or Category I use frequently, saving myself tons of time! Thanks again.

2 Likes

Wow oh wow, these prompts and variables are so powerful now that I've been playing around.

Already updated a secondary macro which is more advanced and executes the following (if it's not clear I'm using this to save receipts for taxes and other reasons):

Print/Save as PDF > Type EXP_ > Prompt for Date (MMDDYY) > Reformat and type as YYYY-MM-DD_ > Prompt for Receipt name > type that [result]_ > Prompt for Category (created a selection drop down of expense types) > Type that [result]_Receipt.pdf Wait for email menu to return > Archive based on a specific gmail label

In other words just as an example from when I bought KM, all I have to do is select that invoice and it gets saved as "EXP_2017-12-08_KeyboardMaestro_Software_Receipt.pdf" which is simply how I'd like to organize all my invoices.

All of that executed in record speed from a single shortcut and working great! :nerd_face: I guess the only dream I'd have would be to find some way in which I could extract the date of the email, plus the sender's name, rather than having to type even that manually. Is that even possible? If so lmk, if not that's all good, I'm sounding fairly lazy over here but I'm excited how much time this is going to save me when preparing for taxes (and other areas of my work too).

1 Like