1 2 Previous Next 23 Replies Latest reply: Feb 19, 2013 11:31 AM by twtwtw
E pluribus unum Level 1 Level 1 (0 points)

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)
  • 1. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    Pierre L. Level 4 Level 4 (3,860 points)

    Ave, E pluribus unum,

     

    Could you give a clear example of what you are trying to achieve?

  • 2. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

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

  • 3. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    not too difficult, but could you post a couple of typical lines from the csv/txt file so that we can see the structure? We need to know how to parse out the old and new file names.

  • 4. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

    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
  • 5. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    what's the bridge between the old file name and the new?  in other words, you have files called 'untitled.mp4' or 'untitled1.mp4'.  How do you know which line of your csv file to use for each? (incidentally, you've shown a tab-delimited file, not a csv file; csv files use commas as delimiters).

  • 6. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

    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.

  • 7. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    I think you misunderstood me.  let me be more precise.  the script sees a file called 'untitled'.  should it choose the Ch.01_01 line, the Ch.01_02 line, the first line, the last line?  or does it not matter what order they're named as long as they are all named?

  • 8. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

    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.

  • 9. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    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 (item i 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 (item i 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

  • 10. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

    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

  • 11. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    the two lines at the top of the script are there for you to alter the delimiters used.  For instance, if you use a comma as a delimiter, use this line:

     

    set splitDelimiter to ","


    and if you want the filename to have an underscore rather than space-dash-space, use this:

     

    set recombineDelimiter to "_"


    see how it works?

  • 12. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    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 (item i 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 (item i 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

  • 13. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    E pluribus unum Level 1 Level 1 (0 points)

    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:    

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

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

                        &

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

     

     

    REPLACE WITH:     

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

                          &

                          set fileList to (POSIX path of (files of folder "/pah/to/folder" whose visible is true andname begins 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 (item i 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 (item i 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

  • 14. Re: Workflow and/ or script to rename finder items using a .txt or a .csv file as a reference?
    twtwtw Level 5 Level 5 (4,690 points)

    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.

1 2 Previous Next