I am currently trying to finish off a script that will change the file names of some pdf’s, the script needs to run automaitcally and I need it to run through any errors it finds.
The part I am stuck on is as follows
repeat with eachfile in folder (alias “location”)
set x to the name of eachfile as text
set y to x’s text 1 thru 6
set name of eachfile to y & “.pdf”
What I need to add is an error handler that will change the name of the file if it encounters a file named the same in the directory it is working on. e.g. add “-1” to the file name if its a duplicate.
I have looked to put a time stamp on the end of each file its renaming to ensure it is unique and would be equally useful, but sadly my skills are still developing.
Not sure if this is what you’re looking for but here 'tis.
set newName to ""
tell application "Finder"
set theChoice to choose folder with prompt "Choose folder with files for renaming."
set fileList to every file of folder theChoice
set theDate to current date
if month of the (current date) is January then
set mnthNum to 1
else if month of the (current date) is February then
set mnthNum to 2
else if month of the (current date) is March then
set mnthNum to 3
else if month of the (current date) is April then
set mnthNum to 4
else if month of the (current date) is May then
set mnthNum to 5
else if month of the (current date) is June then
set mnthNum to 6
else if month of the (current date) is July then
set mnthNum to 7
else if month of the (current date) is August then
set mnthNum to 8
else if month of the (current date) is September then
set mnthNum to 9
else if month of the (current date) is October then
set mnthNum to 10
else if month of the (current date) is November then
set mnthNum to 11
else if month of the (current date) is December then
set mnthNum to 12
end if
set timeStamp to mnthNum & "_" & day of theDate & "_" & year of theDate as text
repeat with i from 1 to (count fileList)
set nameList to name of every file of folder theChoice as list
set fileName to name of item i of fileList
if (length of fileName) is not less than 6 then
set newName to text 1 thru 6 of fileName
else
set newName to fileName
end if
display dialog item 1 of nameList as string
if newName is not in nameList then
set properties of item i of fileList to {name:newName}
else
set otherName to newName & timeStamp
set nameList to name of every file of folder theChoice as list
if otherName is not in nameList then
set properties of item i of fileList to {name:newName & timeStamp}
end if
end if
end repeat
end tell
There might be an easier way to get the months to numbers but I don’t know what it is.