Manipulating Lists in a Shortcut

I’ve included below shortcuts that manipulate lists. A number of these shortcuts rely on the Filter Files action, and the sort behavior of this action has apparently changed recently. These shortcuts were tested on macOS 15.6 only

A list of text or numbers can be sorted with the Filter Files action.

A list of dates can be sorted with the Filter Files action but the dates must first be converted to a sortable format (the ISO 8601 standard is used here). This shortcut requires further testing.

Sort Date List.shortcut (23.1 KB)

A list of text or numbers can be shuffled with the Filter Files action.

Two lists can be concatenated with the Add to Variable action. The variable is created if it doesn’t exist.

A list can be filtered using the Filter Files action.

Duplicate items in a list can be removed using a repeat loop.

Duplicate items in a list can be removed with a regex, which is fast with very-large lists. The regex pattern is from Nigel.

Remove Duplicates from List.shortcut (22.6 KB)

There are a lot more, but these probably give a good idea of how things work.

One can use Zsh shell features to sort ascending (o) and unique (u) in combination with a passed list to remove duplicates from that list. No n-tuple run shell scripts, no loops, and no regex.

List w/o Duplicates

1 Like

VikingOSX Thanks for the suggestion, which works great. While testing, I added a Split Text action to your shortcut to return a list.

FWIW, the following takes a similar approach in that it sorts the list then removes duplicates. It’s not as compact as your suggestion, but it’s faster.

List without duplicates Peavine.shortcut (22.1 KB)

In an AppleScript, you can set the value of a particular item of an existing list to a new value, This doesn’t appear to be natively possible in a shortcut. This can be done with a repeat loop, although it can be slow if the list is large:

Set Value by Index with Loop.shortcut (22.5 KB)

If the list contains text only, the following approach is faster and simpler:

Set Value by Index with Regex.shortcut (22.5 KB)

There are several approaches that can be used to add a value to the beginning or end of an existing list. The Add to Variable action is the fastest approach in my testing:

It’s unlikely anyone would want to add a value at some other point in an existing list, so I haven’t attempted this.

With a small tweak to the Run Shell Script syntax, I can sort ascending a unique list and output it with each item separated by a newline.

If you select the rows of the result and copy to the clipboard, you can then open the Terminal application and enter this command sequence to see that each line ends with a newline (0a):

pbpaste | hexdump -C