Sequential file names

Hi

I know, I should spend more time looking into this, unfortunately time isn’t something I have. My boss has given me limited time to work this out and I have never done anything like this before. Basically, I need to create a drop folder that will create .PNG files of each page of an Adobe Indesign file and name them sequentially as a 000.PNG file name.

I modified the default OSX ‘Image - Duplicate as PNG’ script to create a Folder Action which, when I export the Indesign pages to as JPEG, then creates the PNG file with the capital letter suffix.

Where I really need help is how to get the file names to change? Can any one help me please?

(what I have so far…)

property done_foldername : "PNG Images"
property originals_foldername : "Original Images"
property newimage_extension : "PNG"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"TIFF", "JPEG", "GIFf", "PICT", "PDF", "EPS"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"tif", "tiff", "jpg", "jpeg", "gif", "pict", "pct", "pdf", "eps"}


on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder done_foldername of this_folder) then
			make new folder at this_folder with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of this_folder) as alias
		if not (exists folder originals_foldername of this_folder) then
			make new folder at this_folder with properties {name:originals_foldername}
			set current view of container window of this_folder to list view
		end if
		set the originals_folder to folder originals_foldername of this_folder
	end tell
	try
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			set the item_info to the info for this_item
			if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
				tell application "Finder"
					my resolve_conflicts(this_item, originals_folder, "")
					set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
					set the source_file to (move this_item to the originals_folder with replacing) as alias
				end tell
				process_item(source_file, new_name, results_folder)
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			tell application "Finder"
				activate
				display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
			end tell
		end if
	end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
	tell application "Finder"
		set the file_name to the name of this_item
		set file_extension to the name extension of this_item
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		if the new_extension is "" then
			set target_name to file_name
			set target_extension to file_extension
		else
			set target_extension to new_extension
			set target_name to (the trimmed_name & "." & target_extension) as string
		end if
		if (exists document file target_name of target_folder) then
			set the name_increment to 1
			repeat
				set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & target_extension) as string
				if not (exists document file new_name of the target_folder) then
					-- rename to conflicting file
					set the name of document file target_name of the target_folder to the new_name
					exit repeat
				else
					set the name_increment to the name_increment + 1
				end if
			end repeat
		end if
	end tell
	return the target_name
end resolve_conflicts

-- this sub-routine processes files 
on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		-- the target path is the destination folder and the new file name
		set the target_path to ((results_folder as string) & new_name) as string
		with timeout of 900 seconds
			tell application "Image Events"
				launch -- always use with Folder Actions
				set this_image to open file (source_file as string)
				save this_image as PNG in file target_path with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

In your image processing section, you have “save this_image as PNG in file target_path with icon”. That statement returns a reference to the saved file, e.g. “set savedImage to save this_image as PNG in file target_path with icon” locates the saved image for you.

Then you tell the Finder to set the name of savedImage to the name you obtained in resolve_conflicts.

Hi Adam,

Thanks for the prompt reply, I have tried for a few hours now to put your advice into practice, yet I still have no success! I really am too limited in my knowledge.

Do I have to put the line “set savedImage to save this_image as PNG in file target_path with icon” in somewhere, or was this just to show me how to use the reference?

Also could you show me how to tell the Finder to look at the resolve_conflicts part of the script?

Am I right in thinking I could use that resolve_conflicts part of the script to overwrite the file name and give me the sequence I need in the filenames ( 001.PNG, 002.PNG, 003.PNG ) and will it give me zero padding so that I get 010.PNG instead of 0010.PNG?

I really appreciate your or anyone elses time spent helping me out.

Thanks again!

The script you are using as a template does a lot more than your description is asking for, and leaves out several elements you want. Review these points:

  1. You want a folder action script to accept JPEG files dropped on the folder, convert them to PNG files (in caps), and rename them no matter what their name was before. What do you want to do if the dropped item is not a JPEG?

  2. The naming process should number the files sequentially if they are dropped one at a time (but will not preserve their order if dropped many at a time – you cannot predict the order in which the script will process them.)

  3. The numbering should be in padded three-digit form (presuming there will not be more than 999 of the files).

  4. Why do you need to check for name conflicts if the files are numbered sequentially? There shouldn’t be any conflict unless the numbering is supposed start over again. Is the target folder (for the PNGs) to be changed for different sets? Will more than one folder action be feeding the target? In the latter case, should the script check for the highest numbered PNG already in the target?

  5. The conversion process is not instantaneous and the folder action will use the Finder (which responds to the action) to do the renaming as well. If files are dropped before an earlier conversion is completed, they will be missed – they’ll just land in the folder. One way to deal with this is to change the folder label color to red, say, when files are dropped, and revert it to green, say, when the folder action terminates.

  6. What do you want to do with the JPEGs dropped on the folder after they are converted – they will still be there in the folder because they cannot be renamed in the folder (the action would see that as a new file dropped), they must be renamed in the target folder.

  7. I’m not sure what happens if you drop two files with the same name on the folder. Is that a possibility?

It’s hard to help you when it’s clear that you haven’t described (or possibly even thought out) all of the rules. The sample you give is a good for extracting sample techniques, but it is not the framework you want to use necessarily.

Thanks for you points Adam,

Sorry if my post was not clear, I am now thinking I should have started learning Applescript from scratch. I thought I would be able to adapt an existing script that seemed to almost do what I needed.

What I wanted to achieve is to simplify the process of converting a multi page Indesign document to seperate pages as PNG files named in sequence in padded 3 digit form with suffix in capitals letters.

I am obviously out of my depth and have the utmost respect for guys like yourself who take time to help people like me.

Maybe I can find another way of doing this?

Hi,
does this help at all?

set source_folder to choose folder

tell application "Finder" to set indd_list to every item of source_folder whose name ends with ".indd"

repeat with this_indd in indd_list
	tell application "Adobe InDesign CS2"
		open (this_indd as alias)
		set indd_doc_name to name of document 1
		set short_doc_name to characters 1 thru -5 of indd_doc_name as string
		export document 1 format JPG to ((source_folder as string) & short_doc_name & "jpg") without showing options
		close document 1 saving no
	end tell
end repeat

tell application "Finder" to set jpg_list to every item of source_folder whose name ends with ".jpg"

repeat with this_jpg in jpg_list
	tell application "GraphicConverter"
		open (this_jpg as alias)
		set doc_name to name of window 1
		set short_name to characters 1 thru -4 of doc_name as string
		save window 1 in ((source_folder as string) & short_name & "PNG") as PNG
		close window 1
	end tell
	tell application "Finder" to delete this_jpg
end repeat

Nik

Thanks for you help Nik,

The first half of the script is wonderful, automating the export as JPEG. However we I not have a copy of Graphic Converter here and it is possible to do the conversion in Image Events.

So I have tried to use the image processing part of my original script as the second half, however it gives me a ‘The variable this_image is not defined’ error. I must be missing a line somewhere, any ideas?

thanks again.

Nate

Hi Nate,
Please give this a try:

set source_folder to choose folder

tell application "Finder" to set indd_list to every item of source_folder whose name ends with ".indd"

repeat with this_indd in indd_list
	tell application "Adobe InDesign CS2"
		open (this_indd as alias)
		set indd_doc_name to name of document 1
		set short_doc_name to characters 1 thru -5 of indd_doc_name as string
		export document 1 format JPG to ((source_folder as string) & short_doc_name & "jpg") without showing options
		close document 1 saving no
	end tell
end repeat

tell application "Finder" to set jpg_list to every item of source_folder whose name ends with ".jpg"

repeat with this_jpg in jpg_list
	tell application "Finder" to set filename to name of this_jpg
	set new_filename to characters 1 thru -4 of filename & "png"
	set savepath to (source_folder as string) & new_filename
	tell application "Image Events"
		launch
		set openfile to open (this_jpg as alias)
		save openfile as PNG in file savepath with icon
	end tell
	tell application "Finder" to delete this_jpg
end repeat

Thanks,
Nik

Hi Nik,

Thank you very much, that is marvelous! Really quick too.

Now if only I can sort out that zero padding in the saved filename?

cheers,

Nate

Hi Nate,
does this help with the numbering problem?

set source_folder to choose folder

tell application "Finder" to set indd_list to every item of source_folder whose name ends with ".indd"

repeat with this_indd in indd_list
	tell application "Adobe InDesign CS2"
		open (this_indd as alias)
		set indd_doc_name to name of document 1
		set short_doc_name to characters 1 thru -6 of indd_doc_name as string
		export document 1 format JPG to ((source_folder as string) & short_doc_name & ".jpg") without showing options
		close document 1 saving no
	end tell
end repeat

tell application "Finder" to set jpg_list to every item of source_folder whose name ends with ".jpg"

repeat with this_jpg in jpg_list
	tell application "Finder" to set filename to name of this_jpg
	set new_filename to characters 1 thru -4 of filename & "png"
	set savepath to (source_folder as string) & new_filename
	tell application "Image Events"
		launch
		set openfile to open (this_jpg as alias)
		save openfile as PNG in file savepath with icon
	end tell
	tell application "Finder" to delete this_jpg
end repeat

tell application "Finder" to set png_list to every item of source_folder whose name ends with ".png"

set flag_no to 1
repeat with this_png in png_list
	if flag_no < 10 then
		set no_of_zeros to "00"
	else if flag_no < 100 then
		set no_of_zeros to "0"
	else if flag_no > 99 then
		set no_of_zeros to ""
	end if
	set png_name to the name of this_png
	if png_name contains short_doc_name then --> just incase any PNG files that don't belong to the page just processed
		tell application "Finder" to set the name of this_png to no_of_zeros & (flag_no as string) & ".png"
		set flag_no to flag_no + 1
	end if
end repeat

Thanks,
Nik