Folder Action to Rename Files

I’m working on a folder action to change filenames with some variables. Either “filename_189.pdf.pdf”, “filename_(Page 189).pdf” or “filename_ (Page 1).pdf”.

I would like the end result to be “filename_189.pdf” or “filename_001.pdf”, basically the file name with a 3 digit number.

I’m able to do the first part…


on adding folder items to this_folder after receiving added_items
	tell application "Finder" to repeat with thisFile in added_items
		set thisFilesName to thisFile's name
		repeat with currEnd in {".pdf.pdf"}
			if thisFilesName ends with currEnd then
				set newName to (text 1 thru -((count currEnd) + 1) of thisFilesName) & ".pdf"
				if file newName of this_folder exists then delete file newName of this_folder
				set name of thisFile to newName
				exit repeat
			end if
		end repeat
	end repeat
end adding folder items to

Any suggestions for the rest?

Thanks Tim

Here’s how you can manipulate the names…

set added_items to {"filename_189.pdf.pdf", "filename_(Page 189).pdf", "filename_ (Page 1).pdf"}

set newNames to {}
repeat with anItem in added_items
	if anItem ends with ".pdf.pdf" then
		set fileName to text 1 thru -5 of anItem
	else if anItem contains "(Page " then
		set AppleScript's text item delimiters to "(Page "
		set tempList to text items of anItem
		set AppleScript's text item delimiters to ")"
		set pageNumber to text item 1 of (item 2 of tempList) -- this gets the number after "Page"
		set AppleScript's text item delimiters to "_"
		set baseName to item 1 of (text items of anItem) -- get the original name
		set AppleScript's text item delimiters to ""
		set theNumber to text -3 thru -1 of ("00" & pageNumber)
		set fileName to baseName & "_" & theNumber & ".pdf"
	end if
	set end of newNames to fileName
end repeat
return newNames

Thank You regulus6633,

But I can’t seem to get this to work

What isn’t working? Please show me your code.

Basically I’m using your script modified to run when files are added to folder.

Thanks Tim


on adding folder items to this_folder after receiving added_items
	set newNames to {}
	repeat with anItem in added_items
		if anItem contains "(Page " then
			set AppleScript's text item delimiters to "(Page "
			set tempList to text items of anItem
			set AppleScript's text item delimiters to ")"
			set pageNumber to text item 1 of (item 2 of tempList)
			set AppleScript's text item delimiters to "_"
			set baseName to item 1 of (text items of anItem)
			set AppleScript's text item delimiters to ""
			set theNumber to text -3 thru -1 of ("00" & pageNumber)
			set fileName to baseName & "_" & theNumber & ".pdf"
		end if
		set end of newNames to fileName
	end repeat
	return newNames
end adding folder items to

First, my code only calculates the name. It doesn’t actually rename any files so you have to add code for that. Second, the following line will error when a file name doesn’t contain "(Page " because the variable fileName won’t exist at that point. As such you should put that line inside of the if statement or make some other case such that fileName always exists.

set end of newNames to fileName