Apple Event: May 7th at 7 am PT

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

Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?

Can someone assist me, I am looking for a workflow and/ or script that would allow me to rename finder items using a .txt or a .csv file as a reference for the new naming convention?

MacBook Pro (15-inch Late 2011), OS X Mountain Lion (10.8.2)

Posted on Feb 8, 2013 5:25 AM

Reply
23 replies

Feb 17, 2013 3:01 PM in response to E pluribus unum

This is just logic; think it through. The script needs to know two things to get off the ground: where to find the folder of items to be renamed, and where to find the csv file with the name-change data. those are the variables workingFolder and csvFile. You know you want to use the choose folder command to get the location of the folder (workingFolder); once you have that you still need to get the location of the csv file. In the first script you ask the user for that location, but now you want the script to automatically choose the first csv file of the given folder. So how do you get the script to set the variable csvFile to that first csv file?

Feb 17, 2013 6:15 PM in response to twtwtw

Thank you for provoking me to resolve the issue on my own. I had to google some things about applescript in order to realize that the order of sytanx in literal to the working order of the script. I now understand what the script does in detail... THANK YOU!


I moved the following line of code:


-- csv contents as list

set csvList to paragraphs of (read csvFile)


The ruslting script is this:

-- choose the folder

set workingFolder to (choose folder "Select the folder to be processed") as text


-- change this to specify the correct csv delimiter

set splitDelimiter to ","

-- change this to specify the file name join character

set recombineDelimiter to "_"


tell application "System Events"


-- choose the csv file automatically: need to specify name extension

set csvFile to (POSIX path of (first file of folder workingFolder whose name extension is "csv"))


-- csv contents as list

set csvList to paragraphs of (read csvFile)


-- list of files in alphabetical order

set fileList to (POSIX path of (files of folder workingFolder whose visible is true and name begins with "untitled"))


repeat with i from 1 to count of fileList


-- get a line from the csv list, split it at the delimiter, and recombine it

set {oldTID, my text item delimiters} to {my text item delimiters, splitDelimiter}

set newFileNamePieces to text items of (itemi of csvList)

set my text item delimiters to recombineDelimiter

set newFileName to newFileNamePieces as text

set my text item delimiters to oldTID



-- get a file from the file list and note its extension

set currentFile to file (itemi of fileList)

set fileExtension to name extension of currentFile



-- write the new name back out to the file

set name of currentFile to (newFileName & "." & fileExtension)

end repeat

end tell

Feb 18, 2013 7:35 AM in response to E pluribus unum

what files did it skip renaming? post a couple of file names that it skipped and a couple of the original file names of files that were renamed.


As far as adding a sort-by-date feature, that's doable, but making it a command means adding another dialog (unless you always want to sort it by date). to get a date-sorted list, you have to use the Finder (or else add a sorting handler or pull something from the command line; System Events doesn't have a sorting option). that looks like this (I've used the modification date, but you may want to use the creation date):


tell application "Finder"

set sortedFileList to sort (files of workingFolder) bymodification date

end tell


you'll use the sortedFileList variable in place of the fileList variable that's currently in the script.

Feb 19, 2013 10:28 AM in response to twtwtw

After your last response i worked through the issue of having the script select the .csv file automatically.


Next, I ran into the issue of the script skipping files, I looked into the .csv file and found invalid characters, which i replaced.


Now, I have the issue of the script naming the files in non-sequential order.

How do I know theyre not in order? Well, I made a copy of the main-folder to be worked on and called it "TEST" and Inside of the test folder I have files to be named and the .csv containing the new names. The files are named "Untitled 01" through "Unitiled 82" and the Names file has 1 entries each file in the "TEST" folder.


When I run the script it names all the files (provided that there are no invalid characters) and when it is done none of the files are named according to the order they were listed in the folder. I assume the script needs to be told to sort the files and name them strictly based on the order in which they appear.


Small sample from within .csv file:


Ch.01_04Command structure
Ch.01_05Kernel and shells
Ch.01_06Unix manual pages
Ch.02_01The working directory
Ch.02_02Listing files and directories
Ch.02_03Moving around the filesystem
Ch.02_04Filesystem organization
Ch.03_01Naming files
Ch.03_02Creating files
Ch.03_03Unix text editors
Ch.03_04Reading files
Ch.03_05Reading portions of files
Ch.03_06Creating directories

Feb 19, 2013 10:41 AM in response to E pluribus unum

The issue here is in the phrase 'the order in which they appear'. The visual order of files in the window is separate from the internal order. System Events always returns files in alphabetical order, while the Finder can be told to sort them by any given order. It's possible (well, it should be, Finder scripting is somewhat buggy) to retrieve the visual order from the displayed window order, but I wouldn't recomend that - it leads too easily to mistakes.


what order do you want them to be processed in?

Feb 19, 2013 11:31 AM in response to E pluribus unum

That image isn't too helpful since I can't actually see what order the originals are in (you say it's by date created, but don't show the dates involved), but if you want it done by the date created then use the following sort instead of the one I gave above:


tell application "Finder"

set sortedFileList to sort (files of workingFolder) bycreation date

end tell


i.e. use creation date raather than modification date.

Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?

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