Replace a file

Discussion in 'Mac Programming' started by Applescripter, Apr 27, 2012.

  1. Applescripter, Apr 27, 2012
    Last edited: Apr 28, 2012

    Applescripter macrumors newbie

    Apr 27, 2012
    :apple: Hi, I wannt to make an Apllescriptsapp( with Applescript Editor) in which you can make an image to dublicate it with 3 different names ( In the end you have 4 times the same picture with 4 different names). Well, the app should also be able to replace the pics if there were any with the same name before there, but it didn't worked so well right now. Can somebody give me the right code to do that?

    property destination : "Dance" as alias -- the folder to copy to
    property nameList : {"blabla", "dudada", "gsasy"}
    on run -- application double-clicked or script run from the Editor
    	open (choose file with multiple selections allowed)
    end run
    on open theseItems -- items dropped onto the application
    	repeat with anItem in theseItems -- go through all the dropped items
    		repeat with aName in nameList -- go through all the names
    			tell application "Finder" -- make a copy and rename
    				set name of (((duplicate anItem to destination) to aName) with replacing)
    			end tell
                   end repeat
    	end repeat
    end open
  2. kryten2, Apr 27, 2012
    Last edited: Apr 27, 2012

    kryten2 macrumors 6502a

    Mar 17, 2012

    Is a bit of an understatement but at least you tried.

    property destination : "Dance" as alias -- the folder to copy to 
    needs to point to a location in the form of :

    "Macintosh HD:Users:yourusername:Documents:Dance"
    Your on run handler opens a choose file dialog and does nothing else. If you want to work with theseItems change to :

    set theseItems to (choose file with multiple selections allowed) 
    The on open handler contains errors as well. It will be much more rewarding if you find the solution yourself. You'll gain valuable knowledge by doing so. Work on it a bit more and if you get stuck again post the code.
  3. Red Menace, Apr 27, 2012
    Last edited: Apr 27, 2012

    Red Menace macrumors 6502

    Red Menace

    May 29, 2011
    Littleton, Colorado, USA
    If you also want to duplicate the original file without renaming it, just add another Finder duplicate command after the first repeat statement. Take a look at the Finder's scripting dictionary for information about the duplicate command - "replacing:true" isn't going to do anything.

    Edit: forgot that the script copies first and then renames, so the additional duplicate command for the original item would go after duplicating and renaming for the other names (between the end repeat statements).

Share This Page