Hello.
I am glad it works for you. I have a new version for you if you are ready for step 2:
I have played with the script in the mean time, I have added logic, for the situations, where there exists a file named with an identical number already.
Should it happen, then you are asked to replace it (Yes), or all (Yes to All), (so that you won’t be asked again), and the script merly removes any files that already exists with a conflicting name in the destination folder.
If you answer “No” to the dialog, then you are asked if you want to skip all conflicts, the first time around.
If you answer Yes to that, then all the files that can’t be renamed, because it already exists a file with that name in the desitination folder will be skipped. If you answer “No”, then you will be asked the same question on a per file basis.
When renameing of a file in the destination folder is to be skipped, then the default behaviour is to delete the file that has been copied into the folder to be renamed there.
Setting the property keepConflicts to true at the top of the script lets the source of the name conflicts be in the destination folder with their original names.
I hope you’ll try it out.
use AppleScript version "2.3"
use scripting additions
property keepConflicts : false
property scriptTitle : "Duplicate and Rename in DestFolder"
property lastLoc : missing value
if lastLoc is missing value then set lastLoc to path to desktop folder
tell application (path to frontmost application as text)
set sourceFolder to choose folder with prompt "Original Images" default location lastLoc
set lastLoc to sourceFolder
set destFolder to choose folder with prompt "Rename Destination" default location sourceFolder
set NewNames to choose file with prompt ".TXT file of new names" default location (path to desktop folder)
end tell
duplicateAndRenumber(sourceFolder, destFolder, NewNames)
on duplicateAndRenumber(sourceFolder, destFolder, NewNames)
set {overWriteAll, skipAll, haveAsked, movedCount} to {false, false, false, 0}
try
set thetext to read NewNames using delimiter ","
set theList to cleanseList for (flattenTable for (explode from thetext by ","))
set thePrefix to "im"
set theSuffix to "_main"
tell application "Finder"
set theItems to (duplicate files of folder sourceFolder to folder destFolder with replacing) as alias list
script o
property l : theItems
property m : theList
end script
set itemCount to (count theItems)
if (count theList) ≥ itemCount then
repeat with i from 1 to itemCount
set newName to thePrefix & (word 1 of item i of o's m) & theSuffix
try
if exists file newName of folder destFolder then
if not overWriteAll and not skipAll then
tell me to set btn to button returned of (display dialog ¬
"You have renamed files in this directory already" & return & "The file: " & newName & " exists" & return & "Proceed anyway?" with title my scriptTitle buttons {"No", "Yes to All", "Yes"} with icon 2 default button 3)
if btn = "Yes" or btn = "Yes to All" then
try
move file newName of folder destFolder to trash
set the name of item i of o's l to newName
set movedCount to movedCount + 1
on error
error "An error occured during overwrite of file " & newName
end try
if btn = "Yes to All" then set overWriteAll to true
else if btn = "No" and not haveAsked then -- button = No
tell me to set skipbtn to button returned of (display dialog ¬
"Do you want to skip all files that already exists in dest folder with a new file name?" with title my scriptTitle buttons {"No", "Yes"} with icon 2 default button 2)
if skipbtn = "Yes" then set skipAll to true
set haveAsked to true
if not my keepConflicts then
move item i of o's l to trash
end if
end if
else if overWriteAll then
try
move file newName of folder destFolder to trash
set the name of item i of o's l to newName
set movedCount to movedCount + 1
on error
error "An error occured during overwrite of file " & newName
end try
else if skipAll then
if not my keepConflicts then
move item i of o's l to trash
end if
end if
else
set the name of item i of o's l to newName
set movedCount to movedCount + 1
end if
end try
end repeat
else
error "There were to few numbers for the items"
end if
end tell
tell me to display notification "Duplicated and Renamed " & movedCount & " out of " & itemCount & " Files." subtitle "Move and Rename"
on error e
tell me to display dialog "There was an error:" & e with title "Renaming Files" buttons {"OK"} with icon 2
end try
end duplicateAndRenumber
on compactList for aList
-- removes any empty list items
set b to aList
script o
property l : aList
end script
repeat with i from 1 to (count aList)
if item i of o's l = "" then set item i of o's l to missing value
end repeat
return o's l's text
end compactList
on flattenTable for aTable
tell (a reference to text item delimiters)
set {tids, contents} to {contents, return}
set tmpTxt to aTable as text
set aList to text items of tmpTxt
set contents to tids
end tell
return aList
end flattenTable
on explode from theData by aDelim
--takes an initial chunck of text, turns it into paragraphs, and then turns
-- the paragraphs into lists with items. Any leading/trailing single
-- whitespace around the list separator is removed with it.
script o
property theContents : theData
end script
if class of theData is list then
-- we have been broken into paragraphs and must create
-- a list of list for each paragraph.
repeat with i from 1 to (length of o's theContents)
set item i of o's theContents to explode from item i of o's theContents by aDelim
end repeat
else if (offset of linefeed in theData) > 0 ¬
or (offset of return in theData) > 0 then
-- We got an initial chunk of text we'll turn into paragraphs
tell (a reference to text item delimiters)
local tids
set {tids, contents} to {contents, {linefeed, return}}
set o's theContents to text items of o's theContents
set contents to tids
end tell
-- And pass it onto ourself for processing each paragraph.
set o's theContents to explode from o's theContents by aDelim
else
-- We must turn a paragraph into a list of ists.
tell (a reference to text item delimiters)
local tids
set {tids, contents} to {contents, (space & aDelim)}
-- removes any leading spaces from list separator.
set o's theContents to text items of theData
set contents to aDelim
set o's theContents to o's theContents as text
set contents to (aDelim & space)
-- removes any trailing spaces from the list separator.
set o's theContents to text items of theData
set contents to aDelim
set o's theContents to o's theContents as text
-- breaks the string into a list by list separator.
set o's theContents to text items of o's theContents
set contents to tids
end tell
end if
return o's theContents
end explode