Shortcuts: counting files and Quick Actions

Traditionally, many automation scripts in macOS have iterated through folders performing actions on some or all of the files within them: resizing images, converting document formats, or changing file names, for instance. In this basic tutorial for Shortcuts in macOS, I’m going to introduce a simple way to iterate through folders, recursively if wanted, and show how that can be used in a standalone shortcut and as a Quick Action.

Open the Shortcuts app, and click on the + tool to add a new shortcut. Give it a suitable name such as CountFiles by typing that into the space in the top bar of the window. It’s important to document your work as you go along, and Shortcuts provides Comment actions to do this. Unfortunately, it isn’t easy to find actions in its sidebar on the right; throughout this tutorial I’ll use the search bar there to location them quickly.

In the search bar, type comment to find the Comment action. Drag and drop that into the main view and add an explanatory comment for this shortcut.

shortcuts10

Next, we want a starting action which will prompt the user to select the folder they want to count. Type select in the search bar, and locate the Select File action in the Documents section below. Add that as the next action, then select the blue word Files inside it and change that to Folders. Click on Show More to see the option to Select Multiple, but on this occasion we’ll keep the shortcut simple and leave it unticked here.

shortcuts11

Having selected the folder in the dialog, the next step is to get the contents of that folder. Type get contents in the search bar and add the Get Contents of Folder action. This connects automatically and changes the folder name to File, which is the output of the first action. Click on Show More and here tick the Recursive box so that the contents of the folder will be examined recursively, and include all folders within the selected folder.

shortcuts12

For this basic tutorial, all we’ll do with these files is to count them. Type count in the search bar and add the Count action, which automatically connects and sets itself to count the Items in the output of the previous action.

shortcuts13

The last action is to show the user the result. Type show in the search bar and add the Show Result action, which then connects and changes to read Show count.

shortcuts14

With your shortcut complete and ready to test, click on the Run tool at the top and test it out.

shortcuts15

To implement this as a Quick Action, we’ll use the same actions, but this time we’ll set it to receive its input not from a dialog, but as a Quick Action instead.

Add another shortcut with the + tool, but this time give the shortcut the name CountFilesQA. Add your comment to begin with, then at the top of the sidebar select the Shortcut Details tool instead of the Action Library. In the Details tab there, select Use as Quick Action for both Finder and Services Menu.

shortcuts16

We now need to specify what type of items to receive from the Quick Action. Click on the blue Any item in the action to bring up its editing pane, and there click the Clear button at the foot, which leaves the receive input set as No. Click on the blue No to show that pane again and tick the Folders item within that list.

shortcuts17

As a fallback, we can also set the If there’s no input item to Ask For Folder.

shortcuts18

That action should then provide our subsequent actions with the selected folder from the Quick Action. To that, we need to add the Get contents of folder action. To wire that up to the Quick Action input, Control-click the Folder blue item and set it to Shortcut Input, and make that Recursive too. Complete the rest of the shortcut as you did in the previous example.

shortcuts19

Because we’ve made this a Quick Action, Shortcuts has already added it to the Quick Actions folder, and made it available in the Finder for you to test.

shortcuts20

In the Finder, select a suitable folder and bring up the contextual menu. You should see the new CountFilesQA Quick Action in the list of available Quick Actions. Select it, and see whether it works.

shortcuts21

The code used to implement this can run quite slowly when given a large and deep folder, but eventually you should see the result.

shortcuts22

Hopefully that has given you a start in developing your own shortcuts, and shown how simple it is to turn them into Quick Actions.