Skip navigation

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

1056 Views 23 Replies Latest reply: Feb 19, 2013 11:31 AM by twtwtw RSS
1 2 Previous Next
E pluribus unum Calculating status...
Currently Being Moderated
Feb 8, 2013 5:25 AM

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)
  • 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?

  • twtwtw Level 5 Level 5 (4,580 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.

  • twtwtw Level 5 Level 5 (4,580 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).

  • twtwtw Level 5 Level 5 (4,580 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?

  • twtwtw Level 5 Level 5 (4,580 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

  • twtwtw Level 5 Level 5 (4,580 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?

  • twtwtw Level 5 Level 5 (4,580 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

  • twtwtw Level 5 Level 5 (4,580 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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.