7 Replies Latest reply: Apr 22, 2012 9:09 AM by Apple_Student
Apple_Student Level 1 Level 1 (0 points)

Hi, I wannt to make an Apllescriptsapp( with Applescript Editor)  in which you can drag an image in and the then it dublicate with 3 different names ( In the end you have 4 pics with 4 different names). Well but it didn't worked so well right now. Can somebody write my down the right code for doing that?

 

That's what i've typed:

 

on open this_image

          tell application "Finder" to set name of (this_image as alias) to "dance1.png"

          duplicate this_image to "Users:myname:Dektop"

tell application "Finder" to set name of (this_image as alias) to "dance2.png"

          duplicate this_image to "Users:myname:Dektop"

tell application "Finder" to set name of (this_image as alias) to "dancing.png"

          duplicate this_image to "Users:myname:Dektop"

end open


Applescript-Editor, Applescript
  • 1. Re: Dublicate and rename in Applescript
    red_menace Level 6 Level 6 (14,605 points)

    The Finder will add a number suffix for each copy if you specify the same location as the original, so you don't have to do that much.   Note that the open handler is passed a list of items, even if there is only one, so you should repeat through the list, for example:

     

    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
          tell application "Finder"
             set targetFolder to container of anItem -- copy to the same folder
             repeat 3 times
                duplicate anItem to targetFolder
             end repeat
          end tell
       end repeat
    end open
  • 2. Re: Dublicate and rename in Applescript
    Apple_Student Level 1 Level 1 (0 points)

    Thanks, this helped my some. Unfortunately, I want that the pictures aren't saved in the same folder than the original pic  and the dublications should wear other names (example dance.png->(dublication 1) dancepad.png->

    (dublication 2 )hip.png->(dublication 3) moonwalk.png / and not dance.png->(dublication 1) dance1.png->(dublication2) dance2.png->(dublication3) dance3.png)

  • 3. Re: Dublicate and rename in Applescript
    red_menace Level 6 Level 6 (14,605 points)

    Your renaming schemes are different in your posts [ ? > dance1 > dance2 > dancing] vs [dance > dancepad > hip > moowalk] - how are you coming up with these names?

     

    You are going to have to describe - exactly, and in detail - the changes in the name, what happens when the name already exists, etc.  What happens if the file is named "X"?  Add a prefix/suffix from a list?  Rename from a list?

  • 4. Re: Dublicate and rename in Applescript
    Apple_Student Level 1 Level 1 (0 points)

    if the name of the picture is called "x.png"(or any other name)  it sould set the the names of the dubilcations to:

    x.png -->(dublication 1) dancepad.png->(dublication 2 )hip.png->(dublication 3) moonwalk.png.

     

    the dublications sould be saved in:

    /Users/myname/desktop/dancingfolder/ ( I know that the last time I only wrote "/Users/myname/Dektop" but now I want to save them in the "dancingfolder") (the original picture is located in the "Document" folder, not in the "dancingfolder")

     



  • 5. Re: Dublicate and rename in Applescript
    red_menace Level 6 Level 6 (14,605 points)

    The following script will make a copy to the destination folder and then rename it for each of the names in the nameList property (the items are renamed after they are copied).  If saved as an application, files can also be dropped onto it.  I added a handler to make unique names in the event multiple files are selected or dropped:

     

    property destination : (((path to desktop) as text) & "dancingfolder") as alias -- the folder to copy to
    property nameList : {"dancepad", "hip", "moonwalk"} -- names for the copied files


    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" to try -- make a copy and rename
                set name of (duplicate anItem to destination) to my makeUniqueName_withExtension_inFolder_(aName, get name extension of anItem, destination)
             on error errmess -- oops
                log errmess -- just log the error
             end try
          end repeat
         
       end repeat
    end open


    on makeUniqueName_withExtension_inFolder_(someName, someExtension, someFolder)
       set {counter, divider, places} to {0, "_", 2} -- number of places 1-6
      
       if someExtension is not "" then set someExtension to "." & someExtension
       set newName to someName & someExtension
       tell application "System Events" to tell (get name of items of folder (someFolder as text))
          repeat while it contains newName -- add a number suffix as needed to make a unique name
             set counter to text -places thru -1 of ("000000" & (counter + 1)) -- leading zero(s)
             set newName to someName & divider & counter & someExtension
          end repeat
       end tell
      
       return newName
    end makeUniqueName_withExtension_inFolder_
  • 6. Re: Dublicate and rename in Applescript
    Apple_Student Level 1 Level 1 (0 points)

    Thanks very much, this was the thing I searched for.

  • 7. Re: Dublicate and rename in Applescript
    Apple_Student Level 1 Level 1 (0 points)

    Well my last question is how to save the dublications in

    /System/Library/CoreServices/Dance.app/Contents/

    Resources/ instead of desktop/dancingfolder ( I know that usually there's no Dance.app in this folder but I placed mine there)