Help with Notes shortcut - conditional append

I’m late to the game but finally dipping my feet into using Shortcuts. I just find these kind of visual automation builders confusing to wrap my head around compared to something more like a scripting language. Anyway…

I have a Note that is a list of times. I have a shortcut that, when its home screen icon is tapped, appends the current time to the Note. So far, so good.

Jere’s where I trip up: I want to conditionally append the current date if the last time recorded was PM and the current time is AM and then append the current time.

So, if the Note looks like this:

... a lot of previous times
11:48 PM
11:53 PM

and the current time is 11:57 PM, then the result would be:

... a lot of previous times
11:48 PM
11:53 PM
11:57 PM

But if the current time is 12:03 AM, we are obviously in another day and I would want the result to be:

... a lot of previous times
11:48 PM
11:53 PM

12:03 AM

I just can’t figure out how to get this to work. Any help would be greatly appreciated.

(The date format doesn’t have to be yyyy-MM-dd, that’s just how I was entering it manually.)

1 Like

You would either need to store or read back the previous time Stamp and compare it to the current time stamp and match against those dates.

Note, your 12:03 AM entry may not be your first that day, so it needs to have a check and not just an 'if posted between X&Y. Likewise a 11:59 PM could be the first posting that day.

The best starting point for people to be able to help you is to share a link to the Shortcut you have built so far. That will show us where your note is (Apple Notes, another notes app, a file in iCloud,…) and help understand the options you might have available.

Also, if you do need to store the last appended time stamp for a check outside of the note content, having an app like Data Jar or Toolbox Pro can help to make it easier.

Additionally, you might just be able to search the the whole note for today’s date. Then, if it exists, you skip the date writing portion, if it doesn’t you print out that line. It might save you from having to continuously check each line until you’ve found it.

Agreed with Sylumer about posting a sample shortcut, it could definitely help us help you :slight_smile:

Is that not the same as this bit in bold of the two source options I noted above? The OP has split the time stamp to the date part being like a heading and the time part being an entry beneath it.

I interpreted that as “procedurally go back line by line until you find a date stamp”. Here’s a basic code based example to highlight what I mean:

let lines = note.split("\n");
for (let line of lines) {


let text = note;
let count =<date string>);
if (count != 0) {
    // append date

Of course this would need to be done in Shortcuts, which I can’t dig into right now, but I know there are some string search methods you could use which would allow you to avoid a repeat loop. Maybe this was what you had in mind.

It depends on what options are available to interact with the note, but I’d imagined doing a regular expression match for the last date format line match or matching for the current date. Something like this maybe?

1 Like

Here is a link to the shortcut I have so far

This uses Apple Notes.

11:59 PM will never be the first posting of the day, guaranteed. The first posting of each day will always be an AM time and the last posting of the day will always be a PM time. That’s why I figure if the current time is AM and the previous time is PM, it’s a new day. I tried to use a regex to see if the current body ended with “PM” but couldn’t get it to work.

Thanks, all!

I’ve taken what your existing shortcut does, simplified the variable use, and put it into the structure I set out in the example shortcut above.

Back up your original note and then give it a try.

Here’s what I came up with shortly after my post yesterday. Had to wait until this morning to test it and it works fine.

@sylumer Thank for your help. Seeing the example screenshot you posted before helped me figure out what I was doing wrong with the regex matching.

@Samuelhn you have posted a link to a shortcut, but with no information. I can see what the Shortcut is doing, but I’m still unclear as to why you have posted it. Can you add some information as to the purpose and what it brings to the discussion?