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 9, 2013 6:55 AM in response to Pierre L.

Well, my issue is that I have a ton of media files that have the name "untitled" proceeded by a sequential number. I would like to change the naming convention of these files to a specific title based on the contents of a file (.csv or .txt) that I have assembled with all of the media titles.


Example:


Folder (1) contains media files with the name of untitled.mp4, untitled1.mp4, etc.

I would like to change the names, not the extension, of these files using the contents of a preassembled (.csv or .txt) file that contains the new media names (Jeff, James, homer, etc).

Feb 11, 2013 9:58 AM in response to twtwtw

I am looking to automate the process of naming my media files with the help of either a .csv or .txt file as a referrence. I have included a sample of the .csv below (the .txt file would look similar and be tab delimited). The steps in the process are as follows:


Create naming file (.csv or .txt)

Sift through media files

Name files by Chapter

Add description to the end of Chapter number

Repeat...


I would like to automate this process, making it alot quicker. I am aware that I could use an automator workflow, naming the files with text before and according to a certain numbering convention. This doesnt seem to suit me as I would still have additional steps of adding the descriptions manually. I am unfamiliar with constructinbg a more complex workflow inside automator.


Ch.01_01Welcome to InDesign FX
Ch.01_02Blurring Objects with Drop Shadows
Ch.01_03Creating Interlocking Objects
Ch.01_04Exploring the Effects Panel
Ch.01_05Creating Long Text Shadows with Type on a Path
Ch.01_06Making Shiny Effects
Ch.01_07Producing Slime
Ch.01_08Exploring Bevel and Emboss Settings
Ch.01_09Exploring Inner Glow Settings
Ch.01_10Building Better Bevels

Feb 11, 2013 9:35 PM in response to twtwtw

The files are in fact labeled 'untitled, untitled 1, untitled 1 [1], untitled 1 [2], and untitled 1 [3]' and so on. The selection above is a copy and paste from a .csv file. I mention tab-delimited because that's how I would import them into the .csv (if it were required to do so) otherwise I would use commas or delimiters.


Essential the task is to take the naming convention with the .csv or .txt file and use it to replace the name of the media files named 'untitled' and so-on.

Feb 12, 2013 7:46 AM in response to twtwtw

I did misunderstand you and I apologize for that. I usually get the files to follow ths naming convention (untitled or untitled 1 or untitled 1 [1] and then untitled 1 [2]); however, I can always change that to (untitled 1, 2, 3, etc.). Ideally, all files will be named untitled 1, 2, 3, etc. and the script should match the first file to the first line in the document being referrenced.

Feb 12, 2013 8:19 AM in response to E pluribus unum

in applescript I could probabl putter this out in automator, but it's more straightforward to do it in applescript:


-- delimiters: first to split CSV line and second to recombine the split text into a file name

set splitDelimiter to tab

set recombineDelimiter to " - "


-- csv contents as list

set csvList to paragraphs of (read "/path/to/csvfile.csv")


tell application "System Events"


-- list of files in alphabetical order

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


repeat with i from 1 to count of fileList


-- 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



-- 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



-- write the new name back out to the file

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

end repeat

end tell

Feb 13, 2013 9:31 PM in response to twtwtw

Thank you very much for your help and for developing this script, it worked; however, there was on glitch. The glitch in the script is that the delimiter remains a comma "," and has not changed to the dash "-". Aside from that it works. In addition I have a request which is to add in a prompt for choosing the source file for the ".csv" as well as a prompt for the source media files. I am hoping to run this script from my desktop as a .app

Feb 13, 2013 10:40 PM in response to E pluribus unum

missed the other part of your question, sorry. this script is revised to ask for the csv and folder. If you want to play with it you could automate the task a bit more by always putting the csv file in the same place (for instance, inside the folder with the files it needs to work on). that way you could choose the folder and get applescript to find the csv file for you.


also, you might want to think about using the script menu for this. You can turn that on in the AppleScript Editor preferences, and you'll get a menu on the top bar that will let you run scripts directly. saves you needing to make an app or travel to the desktop to find it.


-- change this to specify the correct csv delimiter

set splitDelimiter to ","

-- change this to specify the file name join character

set recombineDelimiter to "-"


-- choose the csv file

set csvFile to choose file with prompt "Select the CSV file." of type "public.text"

-- choose the folder

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


-- csv contents as list

set csvList to paragraphs of (read csvFile)


tell application "System Events"


-- 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 file from the file list and note its extension

set currentFile to file (itemi of fileList)

set fileExtension to name extension of currentFile



-- 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



-- write the new name back out to the file

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

end repeat

end tell

Feb 14, 2013 9:28 AM in response to twtwtw

THANK YOU!!!!


I have taken your recommendation and enabled the script menubar item. I also tweeked the script as you recommended, I will play with both options to see which I prefer. I thank you again for the quick turn around on this.


If I am to understand your comment correctly about placing the .csv file in the same folder as the files to be renamed I should modify the script as such.



REMOVE:

setcsvFiletochoose filewith prompt "Select the CSV file." of type "public.text"

setworkingFolderto (choose folder "Select the folder to be processed") astext

&

setfileListto (POSIX pathof (filesoffolderworkingFolderwhosevisibleistrueandnamebegins with "untitled"))



REPLACE WITH:

setcsvListtoparagraphsof (read "/path/to/csvfile.csv")

&

setfileListto (POSIX pathof (filesoffolder "/pah/to/folder" whosevisibleistrueandnamebegins with "untitled"))


After reading this and attempting to modify it I am missing one piece of markup or the arrangement of the markup.



Please find the modified script with options:


-- change this to specify the correct csv delimiter

set splitDelimiter to ","

-- change this to specify the file name join character

set recombineDelimiter to "_"


-- choose the csv file

set csvFile to choose file with prompt "Select the CSV file." of type "public.text"

-- choose the folder

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


-- csv contents as list

set csvList to paragraphs of (read csvFile)

--set csvList to paragraphs of (read "/path/to/csvfile.csv")


tell application "System Events"

-- 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"))


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


repeat with i from 1 to count of fileList

-- 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


-- 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


-- write the new name back out to the file

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

end repeat

end tell

Feb 14, 2013 10:16 AM in response to E pluribus unum

You have two options for auto-selection here:


1. Choose the csv file with a dialog and have the script find its parent folder:


-- choose the csv file

set csvFile to choose file with prompt "Select the CSV file." of type "public.text"

-- choose the folder automatically

tell application "System Events"

set workingFolder to POSIX path of container of csvFile

end tell


2. Choose the folder with a dialog and have the script find the csv file:


This has two variants. You can either specify a particular file name that the csv file will always have:


-- choose the folder

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

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

tell application "System Events"

set csvFile to POSIX path of file "file name.csv" of folder workingFolder

end tell

or if you need more flexibility you can search for the first csv file in the folder


-- choose the folder

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

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

tell application "System Events"

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

end tell

As you can see the only difference is that the second approach requires you to know something about the csv file (its full name or extension). I'm not sure what your naming conventions are, so use whichever works better for you.

Feb 17, 2013 12:57 PM in response to twtwtw

I am going through this script to get a better understanding of the nature of the script so that I could learn to make scripts myself. I saved the script you posted prior to my last response, in that script you layed out the method for selecting the file containing the new names for the "untitiled" media files and for selecting the folder containing the "untitled" meia files.


Here are the two methods you created to choose the file and folder:

-- choose the csv file

set csvFile to choose file with prompt "Select the CSV file." of type "public.text"

-- choose the folder

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


tell application "System Events"


-- list of files in alphabetical order

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

Here are the methods I would like to implement:

-- choose the folder

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

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

tell application "System Events"

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

end tell


Based on my request you provided options which are outlined in your last reply; I am unsure what needs to be removed to implement the method I like, which is to choose a folder and have the script locate the csv file amongst the media files in the folder.


-- choose the csv file

--set csvFile to choose file with prompt "Select the CSV file." of type "public.text"

-- choose the folder

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


-- csv contents as list

set csvList to paragraphs of (read csvFile)


-- 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"

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


-- 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


I had to comment out the set csvFile because I was unsure whether it stays or goes. When I run the script as it is here i get a applescript error saying "The variable csvFile is not defined."

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.