Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Dublicate and rename in Applescript

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 openthis_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-OTHER, Applescript

Posted on Apr 16, 2012 3:36 AM

Reply
8 replies

Apr 16, 2012 8:09 PM in response to Apple_Student

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

Apr 20, 2012 7:20 AM in response to red_menace

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)

Apr 20, 2012 8:41 AM in response to Apple_Student

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?

Apr 21, 2012 7:15 AM in response to red_menace

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


Apr 21, 2012 12:13 PM in response to Apple_Student

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_

Oct 22, 2015 1:17 PM in response to red_menace

Sorry to highjack this old post but I was hoping you could help out on this. I have already posted this question here: Applescript to rename files with creation date, then move files to a duplicated folder structure.


I've managed to tinker around with your code and a bit from elsewhere. I just have a few questions on how to amend the script.


1. Can I edit the script so I can drop a folder onto it and it runs? (like an application built in automator)

2. Is it possible to capture the original creation date of the oldest file in the folder and get it to populate the the date text box - with the creation date in the format YYYYMMDD

3.And then get the resultant duplicated folder to save in the same directory as the original folder?


Hope that all makes sense. Any help would be much appreciated.

set desktopPath to choose folder  
-- Write a message into the event log.    
log "  --- Starting on " & ((current date) as string) & " --- "  
--  debug lines    
--set desktopPath to (path to desktop) as string  
log "desktopPath = " & desktopPath  
  
set unixDesktopPath to POSIX path of desktopPath  
log "unixDesktopPath = " & unixDesktopPath  
  
set quotedUnixDesktopPath to quoted form of unixDesktopPath  
log "quoted form is " & quotedUnixDesktopPath  
  
try  
  set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath  
  display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix  
  
end try  
  
  
  
tell application "Finder"  
  
  
  set JobDate to text returned of (display dialog "Please enter Identifier:" default answer "Date")  
  
  set catType to {"Cat 01", "Cat 02", "Cat 03"}  
  
  
  set JobCat to {choose from list catType}  
  set JobTitle to text returned of (display dialog "Please enter Identifier:" default answer "Name")  
  
  set JobName to JobDate & "_" & JobCat & "_" & JobTitle  
  
  set templateFolder to (container of (path to me) as string) & "_aa" as alias  
  
  --set folder_Contents to (get files of entire contents of folder templateFolder)  
  
  set fldName to name of templateFolder  
  
  set newName to my replace_chars(fldName, "_aa", JobName)  
  
  set destinationFolder to choose folder with prompt "Please Choose Destination Folder"  
  
  
  
  set newFolder to duplicate templateFolder to destinationFolder  
  set pro_folder to newFolder  
  set all_pro_folders to every folder of pro_folder  
  log all_pro_folders  
  repeat with parent_folder in all_pro_folders  
  log parent_folder  
  set child_folders to every folder of parent_folder  
  
  repeat with current_folder in child_folders  
  set grandchildItems to every item of current_folder  
  repeat with grandchildItem in grandchildItems  
  set greatgrandchildItems to every item of grandchildItem  
  repeat with greatgrandchildItem in greatgrandchildItems  
  set fold_name to the name of greatgrandchildItem  
  log (fold_name)  
  set new_name to my replace_chars(fold_name, "_aa", JobName)  
  set the name of greatgrandchildItem to new_name  
  log (new_name)  
  end repeat  
  set fold_name to the name of grandchildItem  
  log (fold_name)  
  set new_name to my replace_chars(fold_name, "_aa", JobName)  
  set the name of grandchildItem to new_name  
  log (new_name)  
  end repeat  
  
  set fold_name to the name of current_folder  
  log (fold_name)  
  set new_name to my replace_chars(fold_name, "_aa", JobName)  
  log (new_name)  
  set the name of current_folder to new_name  
  
  end repeat  
  set fold_name to the name of parent_folder  
  log (fold_name)  
  set new_name to my replace_chars(fold_name, "_aa", JobName)  
  log (new_name)  
  set the name of parent_folder to new_name  
  end repeat  
  
  set pro_folder's name to newName  
end tell  
  
  
  
on replace_chars(this_text, search_string, replacement_string)  
  set AppleScript's text item delimiters to the search_string  
  set the item_list to every text item of this_text  
  set AppleScript's text item delimiters to the replacement_string  
  set this_text to the item_list as string  
  set AppleScript's text item delimiters to ""  
  return this_text  
end replace_chars 

Dublicate and rename in Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.