Sequentially numbering a file

I am just starting on this, but would like some input.

I have a number of JPG files, let’s call them 24F4.jpg, 24F5.jpg, 24F6.jpg, etc.
I would like to select one of them and prompt for number of duplicates needed, then
make copies of that file with a -1, -2, -3, etc. added into the file name.

So, I would start with 24F4.jpg and need 10 copies.
I would end up with 10 files called:
24F4-1.jpg
24F4-2.jpg
24F4-3.jpg
24F4-4.jpg
24F4-5.jpg
24F4-6.jpg
24F4-7.jpg
24F4-8.jpg
24F4-9.jpg
24F4-10.jpg
placed in the same directory.

Is there a very straight forward way to do this?

david

Hello.

I tested this on a Finder selection, and it worked for me, hopefully it will work for you to. :slight_smile:

property scriptTitle : "Duplicate A File"

to uniqueFilename(pthToFol, fileName)
	-- http://macscripter.net/edit.php?id=154515
	
	local fnend, mfile, msuf, i, numext, newname
	set fnend to (length of fileName) - (offset of "." in (reverse of characters of fileName as text))
	
	set mfile to text 1 thru fnend of fileName
	
	set msuf to text (fnend + 1) thru -1 of fileName
	
	set i to 0
	set numext to ""
	try
		repeat
			set tf to (pthToFol & mfile & numext & msuf)
			set asa to tf as alias
			set i to i + 1
			set numext to "-" & i
		end repeat
		
	on error
		set newname to (mfile & numext & msuf)
	end try
	return newname
end uniqueFilename

tell application "Finder"
	set theFile to the selection
end tell
if theFile is {} then
	tell application (path to frontmost application as text)
		set theFile to choose file with prompt scriptTitle & ":
Please select a file to duplicate:"
	end tell
else if class of theFile is list then
	set theFile to item 1 of theFile
end if
tell application (path to frontmost application as text)
	repeat
		try
			set numItems to text returned of (display dialog "Please select number of copies you want of the file" default answer "3" with title scriptTitle with icon 1)
			set numItems to numItems as integer
			exit repeat
		on error e number n
			if n ≠ -128 then
				beep
			else
				error number -128
			end if
		end try
	end repeat
end tell

tell application "Finder"
	
	
	set theFileName to name of theFile
	set pxTargPath to POSIX path of (theFile as alias)
	set pxFolPath to POSIX path of (container of theFile as alias)
	set theContainer to (container of theFile as text)
end tell

try
	repeat numItems times
		set newname to my uniqueFilename(theContainer, theFileName)
		do shell script "cp " & quoted form of pxTargPath & " " & quoted form of (pxFolPath & newname)
		
	end repeat
on error e number n
	tell application (path to frontmost application as text)
		display alert scriptTitle & ":
Error: " & e & " : " & n
	end tell
end try


Edit
Added the dash “-” that the OP wanted into the uniqueFilename() handler, so that the OP, gets the filenames as he wants them.

ok, that’s a start, which I would have never figured out.

What if I want to prompt for the file using something like this:

set theFile to choose file with prompt "Select image file" of type "JPG" as text

how would I parse out the file name vs. the path to the location?

david

Hello.

This is another way.

set theFile to choose file with prompt "Select image file" of type "JPG" as text
tell application id "sevs"
	tell disk item (theFile as text)
		set pxCont to POSIX path of container of it
		set fname to name of it
	end tell
end tell

log pxCont & space & fname
--> ~/Desktop/ScreenShots Figure 2015-03-05 kl. 12.33.20.JPG

Wow… that works very well. It seems way more complex than I would have thought it to be.

What would I have to do to make it an app that I drop the graphic file on top of and it would prompt me for the number of copies and then duplicate the file in the folder where I was dragging it from?

I don’t need to do that, but just curious if it’s tons more complex, or just a simple line of code.

david

thanks again!

Hello.

This worked for me, when I tested it, as you can see, I used the droplet template from the AppleScript Editor.

I just want to state that any faults are mine, (and that I like the copyright notice).

You’ll have to save this as a droplet with your applescript editor to make it work for you.

How To make the droplet:

  1. Open the script below in AppleScript Editor.

  2. Make a new file with the “Make new droplet with properties” in AppleScript Editor.

  3. Select the contents of this script, and paste it over the contents of the droplet made by the new template.

  4. Save it with a name.

(* 
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software ( the "Apple Software" ), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and / or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON - INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND / OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT ( INCLUDING NEGLIGENCE ), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copyright ( C ) 2011 Apple Inc. All Rights Reserved.
*)

(* INSTRUCTIONS
This template is an example of how a droplet can also have user-accessible properties.
This droplet is designed to process one or more files, whose icons are dragged onto the droplet icon. The droplet also provides access to user-settable properties through a dialog displayed when the applet is run as an application.
To use, place your file processing code in the process_item sub-routine. Save as an application.
*)
(* This template has been edited by me McUsr, and all faults are mine, no warranties, about anything, what so ever! *)
property type_list : {"JPEG"} -- e.g.: {"PICT", "JPEG", "TIFF", "GIFf"} 
property extension_list : {"jpeg", "jpg"} -- e.g.: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property typeIDs_list : {"public.jpeg"} -- e.g.: {"public.jpeg", "public.tiff", "public.png"}

property post_alert : "Yes"
property scriptTitle : "MakeNDuplicates"
on run
	repeat
		display dialog "This droplet will process files dragged onto its icon." & linefeed & linefeed & "There is a user-settable preference for displaying an alert dialog when the droplet encounters a dragged-on item that is not a file of the type processed by the droplet." & return & return & "Post User Alert: " & (post_alert as text) buttons {"Set Prefs", "Done"} default button 2 with title "My File Processing Droplet"
		if the button returned of the result is "Set Prefs" then
			display dialog "Should this droplet post an alert dialog when items that are not files are dragged onto it?" & return & return & "Current Status: " & (post_alert as text) buttons {"No", "Yes"} default button post_alert
			if the button returned of the result is "Yes" then
				set post_alert to "Yes"
			else
				set post_alert to "No"
			end if
		else
			return "done"
		end if
	end repeat
end run

-- This droplet processes files dropped onto the applet 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		set this_name to the name of the item_info
		try
			set this_extension to the name extension of item_info
		on error
			set this_extension to ""
		end try
		try
			set this_filetype to the file type of item_info
		on error
			set this_filetype to ""
		end try
		try
			set this_typeID to the type identifier of item_info
		on error
			set this_typeID to ""
		end try
		if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
			-- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
			process_item(this_item)
		else if post_alert is "Yes" then
			display alert "PROCESSING ALERT" message "The item "" & this_name & "" is not a file that this droplet can process." buttons {"Cancel", "Continue"} default button 2 cancel button "Cancel" as warning
		end if
	end repeat
end open

-- this sub-routine processes files 
on process_item(this_item)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- PLACE YOUR FILE PROCESSING STATEMENTS HERE 
	repeat
		try
			set numItems to text returned of (display dialog "Please select number of copies you want of the file" default answer "3" with title scriptTitle with icon 1)
			set numItems to numItems as integer
			exit repeat
		on error e number n
			if n ≠ -128 then
				beep
			else
				error number -128
			end if
		end try
	end repeat
	tell application id "MACS"
		tell file (this_item as text)
			set theFileName to name of it
			set pxFolPath to POSIX path of ((container of it) as alias)
			set theContainer to (container of it) as text
		end tell
		set pxTargPath to POSIX path of this_item
	end tell
	
	try
		repeat numItems times
			set newname to my uniqueFilename(theContainer, theFileName, "-")
			do shell script "cp " & quoted form of pxTargPath & " " & quoted form of (pxFolPath & newname)
		end repeat
		display notification "" & numItems & "Copies made of " & theFileName with title my scriptTitle
		delay 1
	on error e number n
		tell application (path to frontmost application as text)
			display alert scriptTitle & ":
Error: " & e & " : " & n
		end tell
	end try
	
end process_item




to uniqueFilename(pthToFol, fileName, numSep)
	-- http://macscripter.net/edit.php?id=154515
	
	local fnend, mfile, msuf, i, numext, newname
	set fnend to (length of fileName) - (offset of "." in (reverse of characters of fileName as text))
	
	set mfile to text 1 thru fnend of fileName
	
	set msuf to text (fnend + 1) thru -1 of fileName
	
	set i to 0
	set numext to ""
	try
		repeat
			set tf to (pthToFol & mfile & numext & msuf)
			set asa to tf as alias
			set i to i + 1
			set numext to numSep & i
		end repeat
		
	on error
		set newname to (mfile & numext & msuf)
	end try
	return newname
end uniqueFilename

on quit
	
	continue quit
end quit

Edit
I had to go back and fix some issues, like the dash in the file name, and assert that it indeed would quit, when hitting cancel.

Added a “howto” on how to save it on OP’s machine.

Hey David,

Most programming is more complicated than you think it will be. :cool:

I hate droplets, so I use utilities like FastScripts and Keyboard Maestro to run my scripts (usually with keyboard shortcuts).

This script will operate on the selected file in the front Finder window.

It has a default number of 10 copies and restricts you to 50 or less (easily changed).

I’ve gone for simple, so while there is error-checking it’s pretty basic.

try
	
	tell application "Finder"
		set fSel to selection as alias list
		if length of fSel = 1 then
			set fSel to first item of fSel
			if kind of fSel ≠ "Image" then error "Selected file is not an image!"
		else
			error "Problem with number of files selected!"
		end if
	end tell
	set dupeNum to text returned of (display dialog "How many duplicates do you want?" default answer "10" giving up after 30)
	if dupeNum > 50 then error "Too many files!"
	set fSel to quoted form of (POSIX path of fSel)
	
	set shCMD to "
	filePath=" & fSel & ";
	cd $(dirname \"$filePath\");
	fileName=$(basename \"$filePath\");
	fileExt=\"${fileName##*.}\";
	fileName=\"${fileName%.*}\";
	
	for i in {0.." & dupeNum & "}
	do
		cp \"$filePath\" \"$fileName-$(printf \"%03d\" \"$i\").$fileExt\"
	done
	rm \"$filePath\"
	"
	do shell script shCMD
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try

Hi,

Date your files by year, month, and day in that order so that they will be easily sorted. There’s more tricks to sorting your files I think, but that’s the main thing if you want to write a simple squick script to sort.

gl,
kel