AppleScript: how to use a csv list to duplicate & rename an image?

Posted:
in Genius Bar edited January 2014
I have a csv list that contains a series of image names (1AMS2612.jpg, 1AMS2645.jpg, 1AMS7374.jpg, 1AMS7375.jpg, etc) and a single 'placeholder' graphic. I need to use the csv list (well contents of the file...this can be made into another format) to make a copy of the image for each entry in the list, and rename that file to the name of the entry.



Here's what I'm thinking about:

1) Get the first entry from the list (e.g. 1AMS2612.jpg).

2) Make a copy of the image file

3) Rename image file to match the entry's name (e.g. 1AMS2612.jpg)

4) Get second entry, and repeat the process





Any help would be greatly appreciated!

Comments

  • Reply 1 of 13
    lundylundy Posts: 4,466member
    Quote:
    Originally Posted by Keda View Post


    I have a csv list that contains a series of image names (1AMS2612.jpg, 1AMS2645.jpg, 1AMS7374.jpg, 1AMS7375.jpg, etc) and a single 'placeholder' graphic. I need to use the csv list (well contents of the file...this can be made into another format) to make a copy of the image for each entry in the list, and rename that file to the name of the entry.



    Here's what I'm thinking about:

    1) Get the first entry from the list (e.g. 1AMS2612.jpg).

    2) Make a copy of the image file

    3) Rename image file to match the entry's name (e.g. 1AMS2612.jpg)

    4) Get second entry, and repeat the process





    Any help would be greatly appreciated!



    I can help. First thing you need to specify is how to access the image given only the file name. IOW the path to the image file. Since the image files aren't named the same as the strings in the CSV list, there has to be some other way to locate them.
  • Reply 2 of 13
    kedakeda Posts: 722member
    Quote:
    Originally Posted by lundy View Post


    I can help. First thing you need to specify is how to access the image given only the file name. IOW the path to the image file. Since the image files aren't named the same as the strings in the CSV list, there has to be some other way to locate them.



    Sorry, I'm an AppleScript newbie. What does IOW mean?



    Just to reiterate my situation...I have a 'template' image that I need to duplicate 1098 times. Then I need to rename the files to match the listing in the csv. (1AMS2612.jpg, 1AMS2645.jpg, 1AMS7374.jpg, 1AMS7375.jpg, etc). I'm not sure if I was clear in my initial description.



    Thanks!
  • Reply 3 of 13
    lundylundy Posts: 4,466member
    Quote:
    Originally Posted by Keda View Post


    Sorry, I'm an AppleScript newbie. What does IOW mean?



    Just to reiterate my situation...I have a 'template' image that I need to duplicate 1098 times. Then I need to rename the files to match the listing in the csv. (1AMS2612.jpg, 1AMS2645.jpg, 1AMS7374.jpg, 1AMS7375.jpg, etc). I'm not sure if I was clear in my initial description.



    Thanks!



    In Other Words.



    This seems pretty straightforward. But I need some more details.



    1. Where is this template image? Is it a file on the disk somewhere? If so, we need the path to it.

    2. Where is the CSV list? Is it a file on the disk somewhere?



    Basically, you are going to read the CSV file and then loop duplicating the image file to some directory and renaming it as you indicated.



    Applescript can do this easily. The only slightly tricky part is reading the CSV file, but I know how to do that.



    Just give me the locations of the data and I will get started on the code.
  • Reply 4 of 13
    lundylundy Posts: 4,466member
    Here's the script.

    First, you need to do the following:



    1. Substitute the path to your image for my placeholder.

    2. Substitute the path to your CSV file for my placeholder.

    3. Substitute the path to your Desktop for my placeholder in the path to the "Images" folder.

    3. Make a new empty folder on the Desktop and name it Images.



    Then run the script. If you turn the volume up you should hear 1098 "dings" as each file is created and moved into the Images folder.





    Code:




    set fileRef to open for access alias "YourDisk:Users:yourHomeFolderesktop:CSVFile.txt "

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    set theImageFile to alias "YourDisk:Users:yourHomeFolderesktop:Image.j pg"

    set theFolder to alias "YourDisk:Users:yourHomeFolderesktop:Images: "

    repeat with theItem in theList

    set theImageFileCopy to duplicate theImageFile to theFolder

    set name of theImageFileCopy to (theItem as Unicode text)

    end repeat

    end tell









    NOTE: The forum software put spaces in the filename and at the end of the path in a couple of cases. You'll need to remove them.
  • Reply 5 of 13
    kedakeda Posts: 722member
    Wow, thanks!



    I'll give it a shot.





    Quote:
    Originally Posted by lundy View Post


    Here's the script.

    First, you need to do the following:



    1. Substitute the path to your image for my placeholder.

    2. Substitute the path to your CSV file for my placeholder.

    3. Substitute the path to your Desktop for my placeholder in the path to the "Images" folder.

    3. Make a new empty folder on the Desktop and name it Images.



    Then run the script. If you turn the volume up you should hear 1098 "dings" as each file is created and moved into the Images folder.





    Code:




    set fileRef to open for access alias "YourDisk:Users:yourHomeFolderesktop:CSVFile.txt "

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    set theImageFile to alias "YourDisk:Users:yourHomeFolderesktop:Image.j pg"

    set theFolder to alias "YourDisk:Users:yourHomeFolderesktop:Images: "

    repeat with theItem in theList

    set theImageFileCopy to duplicate theImageFile to theFolder

    set name of theImageFileCopy to (theItem as Unicode text)

    end repeat

    end tell









    NOTE: The forum software put spaces in the filename and at the end of the path in a couple of cases. You'll need to remove them.



  • Reply 6 of 13
    kedakeda Posts: 722member
    Thank you! That worked wonderfully.
  • Reply 7 of 13
    lundylundy Posts: 4,466member
    Glad to hear it! Was there anything in the code you want explained?
  • Reply 8 of 13
    kedakeda Posts: 722member
    Quote:
    Originally Posted by lundy View Post


    Glad to hear it! Was there anything in the code you want explained?



    The code is very straightforward. This made me realize that I have absolutely no idea what AppleScript is capable of.
  • Reply 9 of 13
    I used the basis of the above script from Lundy to perform a similar operation where I have a folder full of images and a csv file of only some of these images.. I want to copy from Folder-A to Folder-B ONLY the files from the list. When I made my changes to path and ran script it is copying ALL the files from Folder-A to Folder-B Event Log does show it properly reading the csv file.. What have I done incorrect.



    -regards and much thanks for any help



    below is my current script-

    ------------------------------------------------------------------------

    set fileRef to open for access alias "MacDaddy HD:Users:kevinvesktop:GMA10Kelly:PARTSGMA10.txt"

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    \tset theImageFile to alias "StudioArtScratch:Hi-Res"

    \tset theFolder to alias "MacDaddy HD:Users:kevinvesktop:GMA10Kelly:HARVEST"

    \trepeat with theItem in theList

    \t\tset theImageFileCopy to duplicate theImageFile to theFolder

    \t\tset name of theImageFileCopy to (theItem as Unicode text)

    \tend repeat

    end tell
  • Reply 10 of 13
    imikehimikeh Posts: 1member
    I have a .csv file that I open in numbers then save as .csv western- The csv contains 3 columns of names. I have 15 rows for the 3 column the rows contain the first name, last name and id #

    I need to copy all that info to a .jpeg place holder I have and duplicate it. I can get it to generate copies of the jpeg but the fist and last names wont match.

    I am using the script listed here then automator to convert to jpeg.

    Help Help help.

    Sorry if I did not explain it very well.
  • Reply 11 of 13
    mdriftmeyermdriftmeyer Posts: 7,501member
    Use the tags to keep your colons in tact.



    Code:




    below is my current script-

    ------------------------------------------------------------------------

    set fileRef to open for access alias "MacDaddy HD:Users:kevinvesktop:GMA10Kelly:PARTSGMA10.txt"

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    set theImageFile to alias "StudioArtScratch:Hi-Res"

    set theFolder to alias "MacDaddy HD:Users:kevinvesktop:GMA10Kelly:HARVEST"

    repeat with theItem in theList

    set theImageFileCopy to duplicate theImageFile to theFolder

    set name of theImageFileCopy to (theItem as Unicode text)

    end repeat

    end tell











    Quoting Code: set it up as
    Code:


    Quote:

    ...







    to keep the colons in tact.



    Code:


    Quote:



    set fileRef to open for access alias "YourDisk:Users:yourHomeFolderesktop:CSVFile.t xt "

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    set theImageFile to alias "YourDisk:Users:yourHomeFolderesktop:Image.j pg"

    set theFolder to alias "YourDisk:Users:yourHomeFolderesktop:Images: "

    repeat with theItem in theList

    set theImageFileCopy to duplicate theImageFile to theFolder

    set name of theImageFileCopy to (theItem as Unicode text)

    end repeat

    end tell








  • Reply 12 of 13
    I'm hoping someone could help here, please!



    I'm trying to do something very similar - I need to duplicate a file "file.xml" 400 times and rename each resulting file as a value stored in a CSV list "list.csv"



    I've tried to work with the code above, but I really have no idea of what I'm doing. Could someone help? Here's the code I've used so far:



    set fileRef to open for access alias "Skynet:list.csv"

    set theList to read fileRef using delimiter ","

    close access fileRef



    tell application "Finder"

    \tset theImageFile to alias "Skynet:file.xml"

    \tset theFolder to alias "Skynet:result"

    \trepeat with theItem in theList

    \t\tset theImageFileCopy to duplicate theImageFile to theFolder

    \t\tset name of theImageFileCopy to (theItem as Unicode text)

    \tend repeat

    end tell





    When I run it, I copies the file "file.xml" one time, gives one ding, and has copied the file into the folder "result". Can anyone suggest how to change the code so it copies the filename from the file "list.csv" and repeats until it runs out of filenames from "list.csv"?



    Any help on this would be GREATLY appreciated -



    Thanks



    Dan
  • Reply 13 of 13
    I have a similar issue, but with multiple files. Hoping someone can help. I have some experience with AppleScript, but not much.

    Basically, I have a list of files named Image_001.tif, Image_002.tiff, etc... and a list of names - can be in CSV or just about any format.

    The list of names coordinates with the order of the file list.

    I want to rename the photo files with the list of names. Any help is greatly appreciated. Thanks!
Sign In or Register to comment.