Moving and Renaming a Group of Files

I have the following hacked together script that will move and rename one specific file. I’m sure it can be improved.


set sourcefilename to "ahq.pdf"
set sourcefolderpath to "Macintosh HD:Users:chartley:Desktop:Academic League:WA Print:work:"
set sourcefilepath to (sourcefolderpath & sourcefilename) as text

set destfolderpath to "Macintosh HD:Users:chartley:Desktop:Academic League:WA Print:round 1:"

tell application "Finder"
	move (sourcefilepath as alias) to (destfolderpath as alias) with replacing
	set theFile to ("Macintosh HD:Users:chartley:Desktop:Academic League:WA Print:round 1:" as text) & "ahq.pdf" as alias
	set name of theFile to "ahq1.pdf"
end tell

I need to be able to do this with all the files in the work folder named above, but I’m not sure how to do this. Thanks for any help you can offer.

Charlie

Hi,

the shell command /bin/mv can move and rename files at the same time


set WAPrintPath to ((path to desktop as text) & "Academic League:WA Print:")

set sourcefilename to "ahq.pdf"
set sourcefolderpath to WAPrintPath & "work:"
set sourcefilepath to (sourcefolderpath & sourcefilename)
set destfilepath to WAPrintPath & "round 1:ahq1.pdf"
do shell script "/bin/mv " & quoted form of POSIX path of sourcefilepath & space & quoted form of POSIX path of destfilepath

Thanks Stefan.

Now, I will use FileMaker Pro scripts to create 10 different pdf files to the work folder. So that I can repeat this process, I need to move the files to a different folder (ie. round 1) and rename them by adding a digit to the filename. The next round will go to folder named round 2 and the added digit will be a 2, etc.

How can I set up a loop to do this? Thanks.
Charlie

you could do it like this


set WAPrintPath to ((path to desktop as text) & "Academic League:WA Print:")
set sourcefolderpath to WAPrintPath & "work:"

tell application "Finder" to set theFiles to files of folder sourcefolderpath
repeat with i from 1 to 10
	set sourcefilepath to item i of theFiles as text
	set destfilepath to WAPrintPath & "round " & (i as text) & ":ahq" & (i as text) & ".pdf"
	do shell script "/bin/mv " & quoted form of POSIX path of sourcefilepath & space & quoted form of POSIX path of destfilepath
end repeat

Okay, I’ve not been clear. The first time through the work folder will contain ten files variously named ahq.pdf, aha.pdf, laq.pdf, laa.pdf, scq.pdf, sca.pdf, ssq.pdf, ssa.pdf, maq.pdf, and maa.pdf. They would be moved to the round 1 folder and renamed by adding “1” to the file names (ie. ahq1.pdf). The next time around, the same named files in the work folder (a new set) would be moved to the folder “round 2” and renamed b adding “2” to the name. This would be repeated through six rounds.

While I’m on the subject, is there a way to script combining two pdf files into one by adding the second to the end of the first? I know this can be done manually with Preview, but would like to script it.

Thanks,
Charlie

How could the script know, when a new set of files has been added?

You can concatenate PDF files with Acrobat Pro (not Reader) which is quite good scriptable

I would be happy with a script that handled one round. I could modify it into six separate scripts for the six rounds.

Regarding merging pdf files, I don’t have access to Acrobat Pro. Wish I did.

My script above actually handles one round.
I changed it slightly to move all files into one folder


set WAPrintPath to ((path to desktop as text) & "Academic League:WA Print:")
set sourcefolderpath to WAPrintPath & "work:"

tell application "Finder" to set theFiles to files of folder sourcefolderpath
repeat with i from 1 to (count theFiles)
	set sourcefilepath to item i of theFiles as text
	set destfilepath to WAPrintPath & "round 1:ahq" & (i as text) & ".pdf"
	do shell script "/bin/mv " & quoted form of POSIX path of sourcefilepath & space & quoted form of POSIX path of destfilepath
end repeat

This line won’t work…

set destfilepath to WAPrintPath & “round 1:ahq” & (i as text) & “.pdf”

because all of the files are not named ahq.pdf. For example, there is one named aha.pdf, another named ssq.pdf, etc. I think the script would need to identify the name of the file, and then append the number to it.

try this


set WAPrintPath to ((path to desktop as text) & "Academic League:WA Print:")
set sourcefolderpath to WAPrintPath & "work:"

tell application "Finder" to set theFiles to files of folder sourcefolderpath
repeat with i from 1 to (count theFiles)
	tell item i of theFiles to set {sourcefilepath, theName} to {it as text, its name}
	set destfilepath to WAPrintPath & "round 1:" & text 1 thru -5 of theName & (i as text) & ".pdf"
	do shell script "/bin/mv " & quoted form of POSIX path of sourcefilepath & space & quoted form of POSIX path of destfilepath
end repeat

Almost perfect! I did have to modify one line.


set WAPrintPath to ((path to desktop as text) & "Academic League:WA Print:")
set sourcefolderpath to WAPrintPath & "work:"

tell application "Finder" to set theFiles to files of folder sourcefolderpath
repeat with i from 1 to (count theFiles)
	tell item i of theFiles to set {sourcefilepath, theName} to {it as text, its name}
	set destfilepath to WAPrintPath & "round 1:" & text 1 thru -5 of theName & "1.pdf"
	do shell script "/bin/mv " & quoted form of POSIX path of sourcefilepath & space & quoted form of POSIX path of destfilepath
end repeat

Your version was adding the value of i to the filename, but I needed it to be “1” for all the files put in round 1 folder. I’ll need to use this to create five additional scripts that handle each round, but this will do the trick.

Thanks for sticking with me on this. I appreciate your help.
Charlie

Some time ago I wrote a small command line utility to merge several PDFs contained in a folder into a new one. Before merging them, the script sorts the PDF files alphabetically by their file name. Maybe you can make good use of it, it will run on Mac OS X 10.4+.

If you are interested, here is the source code, as well as the ready for use tool.

To call it from within AppleScript, you can use code as follows:


-- path to the mergepdf tool
set toolpath to "Macintosh HD:Users:martin:Desktop:mergepdf"
set qtdtoolpath to quoted form of POSIX path of toolpath

-- path to the merged PDF file
set outputpath to "Macintosh HD:Users:martin:Desktop:merged.pdf"
set qtdoutputpath to quoted form of POSIX path of outputpath

-- path to the folder containing the PDF files
set pdffolderpath to "Macintosh HD:Users:martin:Desktop:PDFs:"
set qtdpdffolderpath to quoted form of POSIX path of pdffolderpath

-- executing the command
set command to qtdtoolpath & " -output " & qtdoutputpath & " -folder " & qtdpdffolderpath
do shell script command

Hope that helps!

Best regards from Berlin,

Martin

Thanks Martin! I think I can make this work if I do a bit of tinkering with where I save the original pdf files.