-- open a finder window to allow the user to select the target folder
-- a logfile will be created on the desktop
-- set PROCESS_RENAME to true to disable demo mode !!!!
property PROCESS_RENAME : false
property DEFAULT_SUFFIX : "XXXX"
property DIGITS : {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
property LOGFILE : "~/Desktop/renaming.log"
tell application "Finder"
set currentSuffix to DEFAULT_SUFFIX
-- let the user select the target folder
set targetFolder to (choose folder with prompt "Select the target folder:")
display dialog "The following folder files will be renamed: " & linefeed & POSIX path of targetFolder
my logfunc(linefeed & "New folder renaming on folder: " & POSIX path of targetFolder & linefeed)
-- get the target folder files
set folderFiles to name of every file of entire contents of targetFolder
-- iterate on each file found
repeat with targetFile in folderFiles
set fileExtension to ""
set fileBaseName to targetFile
-- extract file base name and file extension
-- expect file extension to be something like ".XXX"
if (targetFile contains ".") then
-- point found so extract base name and extension
set fileExtension to (characters -4 thru -1 of (targetFile as text)) as text
set fileBaseName to (characters 1 thru -5 of (targetFile as text)) as text
end if
-- extract suffix from base file or filename if no extension found
set newSuffix to extract_year_suffix(fileBaseName) of me
-- suffix found on the new file ?
if (length of newSuffix is greater than 0) then
set currentSuffix to newSuffix
set msg to ("FileName: " & targetFile & " >> No name change!")
my logfunc(msg)
else
set newFileName to (fileBaseName & " - " & currentSuffix & fileExtension)
if (PROCESS_RENAME) then
-- rename file via shell script
set sourcePath to (quoted form of (POSIX path of targetFolder & targetFile))
set destPath to (quoted form of (POSIX path of targetFolder & newFileName))
my rename_file(sourcePath, destPath)
else
set msg to ("DEMO: Old Name: " & targetFile & " >> New Name: " & newFileName)
my logfunc(msg)
end if
end if
end repeat
display dialog "End of renaming folder: " & linefeed & POSIX path of targetFolder
end tell
to extract_year_suffix(fileBaseName)
set found_suffix to ""
-- check presence of year suffix
if fileBaseName contains "-" then
-- extract the last 4 chars
set theLast4Chars to (characters -4 thru -1 of fileBaseName)
-- check all the last 4 Chars are digits
set allAreDigits to true
repeat with aChar in theLast4Chars
if aChar is not in DIGITS then
set allAreDigits to false
exit repeat
end if
end repeat
if allAreDigits then
set found_suffix to (theLast4Chars as text)
return found_suffix
end if
end if
-- no year suffix found
return ""
end extract_year_suffix
on logfunc(msg)
do shell script "echo '" & msg & "' >> " & LOGFILE
end logfunc
on rename_file(sourcePath, destPath)
try
set shellCommand to "mv -f " & sourcePath & " " & destPath
my logfunc(shellCommand)
do shell script shellCommand
on error line number num
display dialog "rename_file: Error on line number " & num
end try
end rename_file
Tal vez no sea la mejor manera de hacerlo, pero no soy un experto en applescript.