There might be cleaner and easier solutions, but here are my thoughts!
The following code depends on
PDFtk Server, a command-line tool licensed under GNU General Public License that you can use for free as long as you don't sell commercial software with it. If you want to use the code below, you need to install it. Maybe you can also get the task done with the Applescript library of Preview.app or with another PDF tool like
Xpdf, but I have chosen PDFtk Server. We use it for two things:
1. Make a dump of the file to get the title for the PDF (sometimes the title is missing in the test files I downloaded from JSTOR. More on this later)
2. Move the first page to the end of the document and write the file
Then there is another command-line tool involved that you should already have installed on your Mac. It's called: awk. With awk I did the string manipulations to extract the title from the file dump. Alternatives would be to use sed or grep for this.
I guess it would be better to write this as a shell script, but here is a one liner you can copy and paste to your Terminal bash after modifying the input and output path to your needs:
Code:
inputfiles=$"/path/to/inputfiles/*pdf" ; outputfiles=$"/path/to/outputfiles/" fileext=".pdf" ; for i in $inputfiles ; do newfilename=$(pdftk "$i" dump_data_utf8 | awk 'c&&!--c; /InfoKey: Title/{c=1}' | awk '{ sub("InfoValue: ", ""); print}') ; if [ -z "$newfilename" ] ; then newfilename="$(basename "$i" $fileext)" ; fi ; fileiterator=0 ; while [ -f "$outputfiles$newfilename$fileext" ] ; do let fileiterator++ ; newfilename="${newfilename%%_*}"_"$fileiterator"; done ; pdftk A="$i" cat A2-end A1 output "$outputfiles$newfilename$fileext" ; done
The files without a title value keep their filename (as a number). You could use Xpdf to read out the PDF content to plain text and extract the first line if you want. The test files I have are unfortunately not so homogeneous that the first line is always the expected title. So this could be a little tricky.
If there are documents with equal titles I just added a number to the document. You could extract other values to get a better name.
Finally you could call the code within an Automator task or Applescript, but that's on you. Hope this helps to accomplish the task.