How to use variables with Automator's 'find item' action?

Hello everybody here,

I would like to use Automator as to organise a huge amount of files—mostly PDFs and EPUBs—by sorting them into subfolders named after the files’ authors.
These authors’ names are part of files’ names.

Other options would be either using tags or metadata but I need to keep all this as simple for now.

Now, since neither ‘Find Finder Items’ nor ‘Filter Finder Items’ accept variables into the ‘Criteria’ fields, I tried to set up a Quick Action with a ‘Spotlight search’ action instead.
My—too much simple—workflow asks for an AUTHOR’s NAME (—> variable #1) and a SEARCH FOLDER (—> variable #2) to start from and uses those two variables to find all the items matching the value given to AUTHOR’s NAME by the user within that specific folder.

The next part of the flow should consist into passing the so far processed files to another action aiming to sort them within a SUBFOLDER named after the given AUTHOR’s NAME variable.
This part of the job is not developed yet.

Unfortunately the workflow as I conceived it pulls out a too large number of items, since Spotlight looks for elements matching the rules throughout the whole document, not only its own name.
So it is no use for me.

Much more unfortunately I am not any good at scripting and don’t have any clue as how to sort out a solution to my task.

Has anybody any suggestion?
Many many thanks in advance.

Have you tried doing it with Hazel? I feel like this sort of thing might be easier than trying to accomplish it with Automator.

Hi Brooks, I have Hazel and make it perform other tasks.

In my case I don’t think it can be much use because what I need to do cannot be fully automatized.

I found some scripts around performing the extraction of a filename or some digits of it but this is not what I want.

In my case I have long filenames containing an author name without any regular pattern so it is impossible to set up a fully automatic action to achieve what I have on my mind.

So basically my problem is how use some words arbitrarily chosen like variables to find and sort items

Without a filename example it’s hard to be specific but you could try doing this with shell commands in the Terminal or using the “Run Shell Script” action in Automator

Make a new folder with the authors name if needed
mkdir ~/Documents/AuthorsName
(Just an example, change the path name to your requirements)

(As a test and using the Terminal, find the files and print them to the terminal to see if its works properly. The ‘dot’ after ‘find’ means the current directory, change it to any other folder if you want)
find . -iname ‘*authors name*’ -print

Find and move the files.
(The following command does a case insensitive search in the current dir and finds all the filenames containing the authors name and moves them to the new folder)
find . -iname ‘*authors name*’ -exec mv '{}' ~/Documents/AuthorsName \;

1 Like

Thank you so much alby916, I do appreciate either your job and explanations, they are gold for me.
Your commands work perfectly.

Please let me ask you just another question.
Within this command

how can I have Terminal make a folder with a space between the two ‘Authors’ and ‘Name’ words?

As to be more specific about my request, the filename patterns I have are usually of this sort:
‘xyz Name Surname xyz’
‘xyz Surname, Name xyz’
‘xyz Name xyz’
where ‘xyz’ before and after ‘Name Surname’—or in whatever order the words are given, even with or without punctuation—is any string of alphanumeric text not following a regular pattern, usually comprised of the title of that paper.

So, since I have plenty of this kind of files and I know their authors’ names, what I have on my mind is a process of this kind performed through Automator:

  1. select a folder
  2. ctrl click
  3. either Quick Action or Service –> Find and sort Authors Files
  4. popup window: ‘What is Author’s Name you are looking for?’
  5. action performed within parent folder and subfolders

To do such a thing I thought of using variables to be filled up with those ‘Name’ and ‘Surname’ I am talking of.

Thank you again.

No problem Miagolazzi

To make a space between words in a folder name in Terminal, just use backslash ‘\ ’ where you want a space
ie. mkdir ~/Documents/Authors\ Name

Having said that, if you are using Automator, it’s easier to use the New Folder action where you can put a variable into the Name: box. You won’t need backslashes there.

1 Like