Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript File Rename Request

Hello Folks,


I have thousands of files that I need to rename and I am sure this can be done with a script, but have no idea how. Would appreciate any help, or a script to achieve the folowing -


In a finder window, I have a bunch of files selected. Each file is an author name (firstname lastname). I would like to rename the selected files to (lastname, firstname). So swapping the two names and inserting a comma. Needs to work on multiple selected files that may no be a contiguous selection if possible.


Grateful for any help offered 🙂

OS X Mavericks (10.9)

Posted on Nov 16, 2013 10:43 AM

Reply
Question marked as Best reply

Posted on Nov 16, 2013 11:09 AM

This should do what you want, within limits:


tell application "Finder"

set fileList to selection as alias list

end tell


tell application "System Events"

repeat with thisFile in fileList

set fName to name of thisFile

set {oldTID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, {" ", "."}}

set nameBits to text items of fName

set AppleScript'stext item delimiters to oldTID

set newName to item 2 of nameBits & ", " & item 1 of nameBits & "." & item 3 of nameBits

set name of thisFile to newName

end repeat

end tell


Limitations:

  • Filenames must have a file extension or you'll get an error
  • Filenames must have only a first name, a last name, and a single space between them. If any files have a middle name or a qualifier like Jr. or III, the final result may be in the wrong order.

These are fixable with a somewhat more complicated script.

7 replies
Question marked as Best reply

Nov 16, 2013 11:09 AM in response to emmpeez

This should do what you want, within limits:


tell application "Finder"

set fileList to selection as alias list

end tell


tell application "System Events"

repeat with thisFile in fileList

set fName to name of thisFile

set {oldTID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, {" ", "."}}

set nameBits to text items of fName

set AppleScript'stext item delimiters to oldTID

set newName to item 2 of nameBits & ", " & item 1 of nameBits & "." & item 3 of nameBits

set name of thisFile to newName

end repeat

end tell


Limitations:

  • Filenames must have a file extension or you'll get an error
  • Filenames must have only a first name, a last name, and a single space between them. If any files have a middle name or a qualifier like Jr. or III, the final result may be in the wrong order.

These are fixable with a somewhat more complicated script.

Nov 17, 2013 1:29 PM in response to emmpeez

as I said, your files must have file extensions for this script to work. In other words, this file should be called something like 'Alan Furst.txt', whereas it is obviously only called 'Alan Furst'. This script works by using file name regularities, and if your files are named irregularly the script needs to be modified.


If you need the script modified you'll have to provide a sample of the file names you're working with. Make sure the sample includes all the file name variations you're dealing with (e.g. one, two, three or etc words in the name, with and without extensions, other details that might trip up the script).

Nov 17, 2013 1:49 PM in response to twtwtw

My apologies...


Used the wrong term in the original question 😟


When I said files, I should have said folders!


So the many folders are named as described 'Charles Dickens' and I would like to rename them 'Dickens, Charles'


User uploaded file


So, Gregson, Julia is correct and Grffin, Kate is also correct. But Gretchen Morgenson is incorrect.


Sorry for the confusion, but wondering if it can still be achieved with Folders?


Thanks

Nov 17, 2013 2:04 PM in response to emmpeez

Ah, that makes life easier. I've added a couple of if statements to make sure it only works on folders and skips names that have been done (i.e. names that already have commas in them).


tell application "Finder"

set fileList to selection as alias list

end tell


tell application "System Events"

repeat with thisFile in fileList

if kind of thisFile is "Folder" then

set fName to name of thisFile

if fName does not contain "," and (count of words of fName) > 1 then

set {oldTID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, {" "}}

set nameBits to text items of fName

set newName to last item of nameBits & ", " & items 1 thru -2 of nameBits

set AppleScript'stext item delimiters to oldTID

set name of thisFile to newName

end if

end if

end repeat

end tell

AppleScript File Rename Request

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.