Repeat Loop Operation

Repeat loops in a shortcut do not always operate as one might expect, and this arises from the fact that repeat loops create a list from the output of the last action in the loop. This is documented by Apple as follows:

For each iteration of a repeat loop, the last action’s output is gathered into a list, and that list becomes the output of the Repeat action as a whole.

To demonstrate, the following shortcut creates a list that contains 500 instances of the word Hello, and an item in this list is accessed by way of the Repeat Results magic variable. The timing result for this shortcut was 0.92 second.

The following uses the Add to Variable action to create the desired list, but it actually creates two lists. The first is a list that contains 500 items and each item in this list is the word Hello. The second list is the concatenated result of the Add to Variable action, and it contains 125,520 items, each of which is the word Hello. The timing result for this shortcut was 58.2 seconds.

The following is a variation of the above where the repeat loop’s result is the Nothing action. The timing result for this shortcut was 1.07 second.

Anyways, repeat loops are used a lot and it’s good to know how they work.

2 Likes

I recently needed to write a Shortcut Quick Action that would allow a user to select individual files or folders and filter just PDFs from the file selection, or the contents of any folder. I used a nested repeat block to add each PDF to a variable. After the repeat blocks, then I passed that list variable to a run AppleScript to process each PDF. The original request was to capture the first two pages of each PDF and write that to the same location with a “_p1-2” suffix.

Click on the image to enlarge.

2 Likes

I recently had a situation where I was struggling to have a repeat loop work correctly in the Shortcuts app. Here was the scenario: a friend of mine asked me what kind of fish I have in my 150 gallon saltwater aquarium, because he is looking to get into the hobby as well. I told him I would send him some images of the fish that I have. So what I did was download an image of each fish that I have in my aquarium and I named each file with the name of the fish and how many of those fish I actually have. For example, “6 Green Chromis Fish.png”, was the name of one of my files and I did that for all 60 fish that I have.

Rather than send him a whole bunch of different images, I thought it would be better to include all of the images into one PDF file with the name of each file, without the name extension, overlaid in the top left corner of each image. So in this multi page PDF, on each image he would see something like “6 Green Chromis Fish” or “2 Oscelaris Clownfish”, etc.

If I was using just AppleScript & Shell script code, I could easily create an automation for this in less than 30 minutes. However, this time I decided to try using the Shortcuts app with its “Repeat With Each” command. It was a nightmare. Every PDF result that I got had all of the images in there but each image had the exact same name overlaid on each. After two hours, I decided I don’t care how long it takes I’m gonna figure this out. Finally after 4 hours and 15 minutes, I finally got the “Repeat With Each” command to output the correct results. This is what I came up with.

Here is a snapshot of the final result:

Here Is The File:

Overlay Text On (Choose Images) To PDF.shortcut

1 Like

wch1zpink. Thanks for the shortcut, which works great and is very useful. I agree that the Shortcuts app can be frustrating to use initially, although its operation quickly becomes apparent, and shortcuts like yours can be written very quickly.