🛍️

Making a dynamic auto-sorting list in Shortcuts to easily deploy memes

So you like memes. (Me too!) And you like Markdown. (Right there with ya, buddy.) And you’re the sort of person who cares about web accessibility, and you do your best to add alt text (image descriptions) to all your images. (Geez, you and me are like peas and carrots!)

Jenny and Forrest from ‘Forrest Gump’ sit on a school bus; the boy looks at the girl, who stares ahead. Text overlay: “Jenny and me was like peas and carrots.”

You’ve got your favorite memes that you return to, time after time. But you also discover new ones that you want to add to your arsenal. If you blog or post to social media using Markdown (👋 like you can over at Micro.blog), you’ll know there’s no good way to keep track of your go-to GIFs. You might be uploading the same image again and again — a waste of time and server storage — and you’ve got to rewrite the alt text each time… you are still adding those, right?

Say you’re Super Into It™ and keep a note of all your memes and their Markdown. (Wow! You’re in good company.) But now you’re hunting and pecking through that list, trying to find the right one, doing the copy/paste dance, and are frustrated that the one meme you use all the time and need to deploy at a moment’s notice is buried in the list. (Not that I would know anything about that…) Why can’t your most-used memes just float up to the top of the list? (That’s what we call a foreshadow in the business. 😏)

Or maybe you don’t suffer any of these problems because you’re not as maniacal about crafting the perfect post. (Honestly, good for you. That’s probably healthier than what I’m going through over here.) But if you do run into any of these friction points, definitely click the button below. You’ll probably like the shortcut that I’m about to share.

An animated image of Taylor Swift opening the door to see herself on the doorstep, with the back-and-forth conversation of “It’s me.” “Hi.” “I’m the problem, it’s me!”

And if you’re interested in how this thing works, read on past the button. I’m low-key quite proud of how it came together. It’s got text files, it’s got counting, it’s got splitting text, rearranging it, and something I like to call the Dynamic Auto-Sorting List…Thingy’. (Okay, you got me. I just made that bit up.) But it sure was fun to make, and (🤞) has been working great for me for a few months.

Let’s get into it.

Before we get into it though, it might be helpful for you to be able to follow along with the shortcut in full. I won’t go step-by-step through every part of this thing, but here’s a massive full-res screenshot of the whole thing in all its glory. At least, the way it was at the time of publication.

TL;DR

Here are the broad strokes of how this shortcut works. It stores each meme as a line of data in a text file. That data is structured with three key bits of information: (1) The meme’s use count, (2) The meme’s name, and (3) The meme’s Markdown including its alt text and image URL.

The shortcut parses that text file by splitting up each line, checking the use count to sort the highest used ones to the top, and then it displays a menu to choose a meme from that sorted list. When a meme is chosen, that line is plucked from the text file, the Markdown is copied to the clipboard, and the use count is increased by 1. The text file gets rewritten with that new use count, and the user simply pastes the Markdown without having to re-upload the image or re-write the alt text.

When a new meme needs to be added to the list, the shortcut guides the user through selecting the image if it’s not already passed in, uploads it to Micro.blog (or other online storage bucket), and (optionally) generates alt text using OpenAI’s Vision API. The meme’s Markdown is assembled, copied to the clipboard for immediate use, and added to the text file with a use count of 1.

Does that make sense? Alright, let’s get to the nitty-gritty.

Setting up the storage text file

Nobody wants to edit a shortcut every time they add a new meme — believe me, I tried. For a while, I had a Dictionary action that I’d paste the meme’s title and Markdown into every time I uploaded a new one. It sucked. It was worse than just using an Apple Note. So instead of storing all this data in Shortcuts itself, I needed somewhere that could be written and overwritten. I could have used an app like Data Jar, but that would be one more step for every user to download and maintain. Yuck. Instead, I chose to build the foundation of this shortcut with the quickest and slickest file format known to mankind: the .txt file.

A text file, stored in the Shortcuts folder of iCloud Drive, houses all the Markdown and supplies this dynamic list that we’ll choose from later on. But since the user won’t already have this set up to start, we need to create the base file.

A screenshot of a digital workflow with various commands for file handling and automation, notably involving text files and shortcuts, in a mobile operating system interface.
Accessing the quintessential text file, and creating it if it doesn’t already exist.

Each time the shortcut runs, it first checks to see if the necessary text file exists. If it does, great, it’ll move on. If not, it creates and saves a text file to the correct location with a singular line of text 0 | ➕ Add new meme. You’re getting a peek at the sorting system. That item has a use count of 0 and will be the only choice in the menu until we, well, add some new memes.

After saving the foundational text file, the shortcut runs itself again so it can actually do something useful. Just don’t forget to add a Stop This Shortcut action when you do these restarts, lest you get into a weird never-ending loop.

Creating and sorting the list

Since we have a structure that each line of data added to that text file follows, we can separate each bit and use them in different ways. First by splitting each line, and then by sorting it highest-to-lowest since each line starts with its use count. The Filter Files action is key here, even though they’re not really files that we’re sorting.

A screenshot of a digital workflow with various blocks is shown, lacking context or surrounding environment descriptions.
Breaking apart the data, sorting it, and presenting it in a new order — all from a text file.

Okay, now that we have all the lines in order, we want to isolate the meme’s name since all use counts and Markdown just make the list hard to read. With each of those lines, we split them further by the pipe character | which separates each bit of the meme’s data. The second one, after the use count, is the Meme’s title. We can set the name to a new variable meme-name and add it to a new list meme-list. They’re already in the right order, so now a simple Choose from List action deployed on that meme-list variable gives us a menu to choose from.

Voilà!

Copying the meme and increasing its use count

The important bit, choosing a meme to copy, is already done, but all the behind-the-scenes stuff still has to take place. Namely, copying it to the clipboard and increasing its use count.

Since we want the ➕ Add new meme’ item to remain in a predictable spot, namely the bottom of the list, we need to check if that was the item chosen, and if so, exclude it from increasing its use count above 0. An If action takes care of that.

We return to the Filter Files action to filter those original individual lines of data down to only the one containing the name of the chosen meme. We split it up again using another Split Text action and assign variables to each of the three parts.

A screenshot shows a flowchart of programming logic blocks labeled with actions such as “Comment,” “Filter,” and “Set variable,” indicating steps in a software automation process.
Getting the data from the chosen meme, and categorizing it for later use.

We can finally copy the Markdown to the clipboard, and then use a Calculate action to add 1 to the chosen-use-count variable. The meme’s line of data is rewritten with the new use count, and that line is swapped in the original text with a Replace Text action. We immediately save the new text file over the old in iCloud Drive. Woohoo!

A user interface displays buttons and comments for managing image and text files within a software application.
Some crafty N + 1 math and swapping out the text file.

Adding a new meme

The other half of the If action we used earlier to check if ➕ Add new meme’ was chosen comes back into play. In the Otherwise’ section of that action, we start the process of uploading a new image with another If action.

This time, we check if anything was passed into the shortcut as input — an image perhaps! — because if it was, we can just upload it straight away with the Simple MB Image Uploader’ shortcut.

A screenshot of a digital workflow or script with comments and action blocks, focusing on image uploading and Markdown text generation, within a software interface.
Sending new memes to the cloud with an external shortcut.

I should note here that while I built in my shortcuts for uploading images to Micro.blog, you don’t have to be on Micro.blog to get use out of this shortcut. You can swap in any shortcut here that uploads an image and returns an image URL or, better yet, Markdown. Maybe you use a different hosting platform, ImageKit, Amazon S3, or even Dropbox to house your photos. Awesome! Swap in your shortcut to upload the image here. And give me a shout if you need a hand getting it right.

Otherwise, when nothing has been passed in, we can present a menu with a Choose from Menu action. Most of the time you’re going to upload a new meme image, but sometimes you might already have one uploaded with the Markdown good-to-go, so I’ve included an option to simply paste in that Markdown. But when you need to upload an image, choose that option and it runs the same uploader shortcut as earlier, which is configured to present source options like the Photos and Files apps when nothing is passed into it.

We’re in the home stretch! Once the image is uploaded by the external shortcut and its Markdown is returned, after allowing for edits to the Markdown (in case you need to revise the auto-generated alt text), it’ll walk you through providing a name for the new meme (Ask for Input action). Just like when we updated a chosen meme’s data line with its new use count, in this case, we assemble the line of data with a fresh use count of 1, its title, and its Markdown, and then prepend it to the original text file and save it.

A screenshot depicts a user interface with functions for editing text, setting variables, commenting, and saving in a software application.
Saving the new meme and copying it to the clipboard. We’re done!

Finally, the new meme’s Markdown is copied to the clipboard and, as a sanity check, an alert pops up so that you can visually confirm that everything copied correctly before pasting the meme elsewhere. I’m a big fan of those final confirmation steps as it means I don’t need to stare at my phone waiting for the shortcut to finish, since the alert will, well, alert me. And it’s nice to double-check that no pipes got twisted up in the shortcut somewhere.

Takeaways

Ron Burgandy meme captioned, “Boy, that escalated quick. I mean, that really got out of hand fast.”

Dynamic List. I learned a lot while building this shortcut. The idea to use a text file to store all the data came to me after I employed a similar technique for my file-sharing shortcut, UpShare MB. In that one, file URLs are saved to a text file for long-term storage and retrieval — again so that you’re not uploading the same file over and over, you can just retrieve it from your history — but it doesn’t have any sorting function. Being able to dynamically add new bits of data and have it adjust to your usage was a breakthrough technique that I’m sure could be used elsewhere.

For instance, you could build a list of loved ones that you want to keep in touch with. Every time you choose one of them to call, their entry could get timestamped and sorted to the bottom of the list so that you know the last time you chatted and you can easily rotate through everyone on your list.

Manual Edits. Even though adding and copying memes all happens through the shortcut, you can very easily revise your list of memes just by opening the text file and manually editing it. You can remove old Memes, artificially bump the use counts, or change a meme’s name and Markdown. As long as you save it when you’re done, the next time you run Markdown Memes’ your list of options will reflect any changes you made to the text file.

A screenshot of a computer’s text editor containing a structured list of meme descriptions with associated URLs. The cursor blinks at the end of the document, suggesting active editing.
See? It really is all just a text file!

Embrace the If Action. I used to lean toward using a Choose from Menu action every time I needed to use branching paths of actions in a shortcut. Lately, I’ve been finding a ton of utility in combining Choose from List with If to make more complex menu trees with conditional statements, or if just one or two options need their own set of actions. In this case, the ➕ Add new meme’ option had its own set of actions, while everything else chosen from the list could follow the same path.

Design Shortcuts to be Multi-Functional. I’m a big fan of shortcuts that work just as well when they’re run on their own as when they’re run with something passed into them from the share sheet. If you can build your shortcuts to work with or without input, you’ll catch way more use cases, especially if you’re sharing them with other people. Everyone’s workflows are different and they’ll probably use your shortcut in ways you hadn’t imagined, so the more versatile the better. For instance, running Markdown Memes’ from the Shortcuts app or Home Screen will display your list of memes to choose from. But if you share a GIF to it from the Photos app ➕ Add new meme’, it knows to just upload the shared image rather than making you pick it out again.


I hope you picked up a few new tricks here, and if nothing else have a new meme-deployment tool to play with. Let me know if you have any questions or comments on this shortcut or a wild idea of how I can improve it.

A meme of Jack Sparrow removing his captain’s hat from his head and placing it over his heart in an expression of acknowledgment.

Shortcuts Tips


❮ Previous post

Humane pinned their launch date, if not their pin April 12, 2024

Next post ❯

7 Things This Week [#140] April 14, 2024