Renaming PDFs

I have the following AppleScript (written by Kai) which renames files:


set sourcefolder to choose folder with prompt "Select the source folder:"
set destinationFolder to choose folder with prompt "Choose where files are to be saved:"
tell application "Finder" to repeat with afolder in (get sourcefolder's folders)
	set foldername to afolder's name
	set filelist to afolder's files
	repeat with i from 1 to (count filelist)
		set name of (duplicate filelist's item i to destinationFolder) to foldername & i & ".pdf"
	end repeat
end repeat

If I have the following name “FIRST_001002_1f+2+_001.pdf”, and would like to rename it to “LAST_001.pdf”

How would I change the Script to have a user-defined amount of characters to delete off the front (in this case 18 characters, i.e. “FIRST_001002_1f+2+”, leaving “001.pdf” ), and then to add the folder name at the beginning (in this case “LAST” ) ?

Thank you for any help you can provide.

–Herbert Ripka
Greendale, WI

It’s a little unclear just what you want to do. The script you provided presumes that all the files are in folders inside the selected sourcefolder. Am I correct that you want the duplicates in the destinationfolder to take the name of the internal folder and not the sourcefolder? The destination folder will not have internal folders. Second, Kai’s script numbers the files consecutively. Now you want to use the number that is already attached to the file name - correct?

The method for doing what you asked for goes like this (but integrating it in Kai’s script requires an answer to the above):

set AppleScript's text item delimiters to ""
set fName to "FIRST_001002_1f+2+_001.pdf"
set fldr to "LAST"
set newName to fixName(fldr, fName)

to fixName(Folder_Name, File_Name)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	set fixedName to Folder_Name & "_" & (text item -1 of File_Name)
	set AppleScript's text item delimiters to astid
	return fixedName
end fixName

It’s a little unclear just what you want to do. The script you provided presumes that all the files are in folders inside the selected sourcefolder. Am I correct that you want the duplicates in the destinationfolder to take the name of the internal folder and not the sourcefolder?

Yes, if they could be the name of the sourcefolder, that would be great.

The destination folder will not have internal folders. Second, Kai’s script numbers the files consecutively. Now you want to use the number that is already attached to the file name - correct?

If that is possible, yes.

Thank you for your efforts on this.

So there are no sub-folders in the sourceFolder?

Try this:

(* This script assumes that the chosen source folder has no internal folders, i.e. is simply a collection of files. 
It further assumes that, as in your example, every filename ends in _xyz, where xyz are numbers.*)

set sourcefolder to choose folder with prompt "Select the source folder:"
set destinationFolder to choose folder with prompt "Choose where files are to be saved:"
tell application "Finder"
	set foldername to destinationFolder's name -- change to sourcefolder if that's what you meant
	set filelist to sourcefolder's files
	repeat with afile in filelist
		set fileName to my fixName(foldername, afile's name)
		set name of (duplicate afile to destinationFolder) to fileName
	end repeat
end tell

to fixName(Folder_Name, File_Name)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	set fixedName to Folder_Name & "_" & (text item -1 of File_Name)
	set AppleScript's text item delimiters to astid
	return fixedName
end fixName

Exactly!!

:slight_smile: THANK YOU VERY MUCH!! :slight_smile:

–Herbert Ripka
Greendale, WI