…I search a macro to rename better the duplicates of files under Finder.
Currently, Finder renames a file (example: “File-original.txt”) so:
“File-original - copie.txt”
My question: how to duplicate a file or a folder of Finder by naming it with the addition of a number or by incrementing its number?
Example 1
Selection of Finder: the file is named "Test.txt"
Copying of the file: the duplicate is named “Test_01.txt”
Example 2
Selection of Finder: a file named "Test_10.txt"
Copying of the file: duplicate is named “Test_11.txt”
And ideal would also be this alternative
Selection of Finder: the file is named "Test.txt"
New name of the file: the original file is named "Test_01.txt"
Copying of the file: the duplicate is named “Test_02.txt”
You can use the Get File Attribute action to break the path into parts (like the parent path, the base filename and the extension).
You can test for the existence of a number in the base filename using the If Then Else action and the Variable “matches” regular expression condition.
You can use the Search Variable action to break the base filename apart into before and after the numbers. Search for (.?)(\d+)(.).
To increment the number while keeping the same number of digits, you can use the perl trick described at Incremental letters & numbers w/ reset, or if you only ever need exactly two digits, you can use the Set Variable to Calculation action with a format of “00”.
And then finally the Copy a File action to copy the file to the new location.
…and thank you once again for your assistant.
But, once again I realise my inability to manage this too complex programming for my small brain.
It’s all right, KM is always for me a tremendous software.
Thank you still for your attention.
Sylvain
Bonjour Peter,
…et merci une nouvelle fois pour votre aide.
Mais, une nouvelle fois je me rends compte de mon incapacité à gérer cette programmation trop complexe pour ma petite cervelle.
Ce n’est pas grave, KM est toujours pour moi un formidable logiciel.
This is simplistic but will work with up to 99 duplicates.
Run with an Execute an AppleScript action.
tell application "Finder"
set finderInsertionPathHFS to insertion location as text
set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set theItem to item 1 of finderSelectionList
set itemNameExt to ("." & name extension of theItem)
set AppleScript's text item delimiters to itemNameExt
set itemBaseName to first text item of (get name of theItem)
set AppleScript's text item delimiters to "_"
try
set itemNum to ((((last text item of itemBaseName) / 100) + 0.01) as text)
set newName to (text items 1 thru -2 of itemBaseName) as text
set AppleScript's text item delimiters to "."
set itemNum to last text item of itemNum
set newName to newName & "_" & itemNum & itemNameExt
on error
set newName to first text item of itemBaseName & "_01" & itemNameExt
end try
set theFilePath to finderInsertionPathHFS & newName
if file theFilePath exists then
beep
error ("A file already exists at that path!" & return & return & theFilePath)
else
set newItem to duplicate theItem to (alias finderInsertionPathHFS)
select newItem
set name of newItem to newName
end if
end tell
I would suggest you do the “Hour of Code”. Its a good first start for learning basic programming. The same sort of principles and concepts apply regardless of whether you are programming JavaScript or AppleScript Keyboard Maestro actions - conditions, loops, variables, and basic a sequence of actions.
I want to thank you for passed time (and expertise) to create this script.
Thousand thank you!..
After reading of script, I confess not to understand everything: I will have been able never to accomplish this code.
I hope that other users than I will be able to use this script: the option offered by Apple seems to me in no way practical…
NB: just a point, the copying of a file called “Test_01.txt” produces a duplicate named “Test_0,02.txt”, while ideal would have been “Test_02.txt” (my system : MacPro 5.1 El Capitan 10.11.1 french).
The copying of a named folder “DossierTest” produces a named duplicate “DossierTest_01.” with dot at end.
NB2: absolutely perfect, protection against the already existent duplicates (script does not operate)…
[quote=“tempo, post:6, topic:2362”]NB: just a point, the copying of a file called “Test_01.txt” produces a duplicate named “Test_0,02.txt”, while ideal would have been “Test_02.txt” (my system : MacPro 5.1 El Capitan 10.11.1 french).
The copying of a named folder “DossierTest” produces a named duplicate “DossierTest_01.” with dot at end.[/quote]
Hey Sylvain,
You didn’t mention using it on folders in your first request.
These kinds of problems you’re having are precisely why I dislike parsing text with vanilla AppleScript.
On my system I would use the Satimage.osax AppleScript Extension’s regular expression support and make this task trivial.
I’ve got to spend the time to learn more JXA, so I can take advantage of JavaScript’s regular expression engine.
In the meantime this script does what you want with a bit more intelligence.
------------------------------------------------------------
tell application "Finder"
set finderInsertionLocation to insertion location as text
set finderSelectionList to selection as alias list
if length of finderSelectionList ≠ 1 then
beep
error "Too many or too few items were selected in the Finder!"
end if
set theItem to item 1 of finderSelectionList
set itemName to name of theItem
end tell
set perlCmd to text 2 thru -1 of "
my $nameStr = '" & itemName & "';
if ( $nameStr =~ m!(.+)_(\\d+)(.*)! ) {
print \"$1_\".sprintf(\"%02d\", ($2 + 1)).\"$3\";
} elsif ($nameStr =~ m!(.+)(\\..*)$!) {
print \"$1\\_01$2\";
} else {
print \"$nameStr\\_01\";
}
"
set perlCmd to "/usr/bin/env perl -fw <<< " & quoted form of perlCmd
set newItemName to do shell script perlCmd
set newItemPath to finderInsertionLocation & newItemName
try
# Make sure the new file name doesn't already exist.
alias newItemPath
beep
on error
tell application "Finder"
set newItem to (duplicate theItem) as alias
set name of newItem to newItemName
select newItem
update (alias finderInsertionLocation)
end tell
end try
------------------------------------------------------------
Now, the numbering of duplicates increments logically.
I offer you to subject him in Apple, even if I doubt that their blindness is more and more strong for some years and that it is less and less close to users…
Still bravo for your expertise… Merci beaucoup !..
Thanks from Paris… where this evening, we cry !.. ;-(