Why is ‘prepending’ so difficult?

I want to make what in my mind should be an easy shortcut/draft action. Apparently it’s not so easy.
All I want to do is write some text with todays date as a header and stick it at the top of the page of an existing apple note or existing draft.
Eg -
July 2, 2023
Today I did blah blah

I’ve been all over the drafts and shortcuts forums and all I find is ‘append to’. I wouldn’t have thought this was such a challenge…
Can anybody explain why this is so difficult (or Shame me by showing me how simple it is- I’m ok with that too)?
Of course, I’d love a solution if anyone can provide one

There are various ways to do this. Creating a Shortcut using the Update Draft Drafts action would be the simplest way as it has a built-in ‘Prepend’ option.

I’ve set this to ‘Ask each time’ to select the draft.

Is that what you want to do?

I don’t think Notes has a prepend option, so it would involve more steps to get that working. My initial thought would be to get the contents of the note, create a text object with the date and the note contents in, then delete the note and create a new one with the combined contents.

Mac and iOS documents may not be stored as one contiguous block of data. They can be scattered blocks of content that are linked together, one after another, from the beginning of a document to its end. The operating system keeps track of this and provides low level file operations: open and read from the beginning, open and write from the end, seek to specific file location, and close. What you want to do (prepend) requires more than basic file operations. You need an editor of some sort that (behind the scenes) can either work through a file sequentially or that can load an entire file into a memory buffer (at least conceptually) for modification. And here’s the important part – then write the modified file to a new location and get rid of the original file.

That’s why prepend is not as simple as it might seem.

Thank you both. I feel that everyone I think of an automation that would help me it turns out to be really complex. If you ever need something broken let me know!
Thanks again- I’ll try the above shortcut

1 Like

Follow up question if I may; appending doesn’t seem to be as difficult as prepending. What is the difference?

The operating system can append to the end of a file without having the disturb the rest of the existing file. It just updates its list of content blocks with a link to another content black at the end. And the operating system provides a low-level command to do this. But it is different when you want to insert something into a file. There is no low-level command.

Think of a list of birthdays for everyone in your family that you perhaps have carefully written by hand on paper, in order by date. If you later remember someone’s birthday that happens to occur late in December, you may be able to add it as another line at the end of your list and still keep everything in order.

But if the forgotten birthday is in February, and you don’t want to write it in tiny letters squeezed between two existing lines where it belongs, you would have to re-write the entire list by hand to keep it neat and presentable.

This is an overly simplified illustration, but I hope you see the difference between simply adding onto the end of a list versus trying to insert one (or many) additional items somewhere in the middle of an ordered list of words.

It’s been a long, long time since I’ve written any filesystem code, but shoudn’t prepending/inserting be as simple (or difficult) as prepending/inserting into a (maybe doubly) linked list?

I agree. Prepend is never as straightforward as append.

I don’t have an example handy, but the approach I have used when prepend isn’t baked in is to get/store the original body of the document, then replace the entire body with my new text followed by a line break and the original body.

Not sure if that’s helpful!

2 Likes

Sure, sophisticated word processors have tricks to make insertion more efficient. But using doubly linked lists is a technique I would associate more with an in-memory data structure for a database than with a naive implementation of a simple text editor.

I was trying to give a simple example to someone without much experience handling files on the Mac (which has UNIX underpinnings), not teach a class on data structures. :slightly_smiling_face:

I used to use Drafts a lot to prepend to different cloud files.

Iirc, I had Drafts read the file, split it at the point I wanted to insert something (usually a hearing), concatenate the three strings in order (original file up to and including the header, new material, original file after the heading) and then write the whole thing back to the document (overwriting it).

Maybe not elegant, and I wouldn’t want to do it with a truly large document, but it worked for my purposes.

As far as I remember, no Unix file management operations include “prepend” at least as they are present in the C Standard Library.

One would expect that, given we are in 2023, macOS (or at least some included Framework or library) would expose a “prepend” mode or primitive that would put the new contents on a new file, append the original contents, remove the original file and then rename the new file to the old in the directory structure. This would probably mean breaking POSIX compatibility. But in the end if you are going to perform this from within Shortcuts, POSIX be damend.