Help with file overwrite renaming

Hi, I have a script which exports a selection in Illustrator and saves it as a PNG. I liked the way that it would overwrite the exact file name if I chose to save the PNG with a name that already exsists.

For some unknown reason, it recently started exporting files which have the same name and appending “_1” after the filename. It wasn’t doing this before 10.6.5. Can you help me get it back to the way it worked before?

For example, if I already had a document saved as mydocument.png and I run the script choosing mydocument as my filename this is what gets saved: mydocument.png_1

But before (and the way I prefer it) it was overwriting the exact file name: mydocument.png

Help!


set PathToFile to (choose file name with prompt "Export Selection as PNG:") as string

tell application "Adobe Illustrator"
	-- If Illustrator is not the frontmost application, activate it. 
	if not frontmost then activate
	-- Make sure there is a document to copy from 
	if (count documents) > 0 then
		set selectedItems to selection of current document
		if selectedItems is not {} then
			copy
			set colorSpace to color space of current document
			make new document with properties {color space:colorSpace}
			paste
		end if
	end if
	
	-- Turn off user interaction so dialogs don't cause the script to get stuck
	set user interaction level to never interact
	
	
	
	
	--	Perform the save
	¬
		export document 1 to file (PathToFile) as PNG24	-- Close new document without asking to save
	close document 1 saving (no)
end tell

hi,

the export-command has no option to overwrite an existing file. so just use the finder to delete the file you’ve chosen … before AI exports the document


set PathToFile to (choose file name with prompt "Export Selection as PNG:") as string

try
	tell application "Finder" to delete alias PathToFile
end try

tell application "Adobe Illustrator"
	-- If Illustrator is not the frontmost application, activate it. 
	if not frontmost then activate
	-- Make sure there is a document to copy from 
	if (count documents) > 0 then
		set selectedItems to selection of current document
		if selectedItems is not {} then
			copy
			set colorSpace to color space of current document
			make new document with properties {color space:colorSpace}
			paste
		end if
	end if
	
	-- Turn off user interaction so dialogs don't cause the script to get stuck
	set user interaction level to never interact
	
	
	
	
	--	Perform the save
	¬
		export document 1 to file (PathToFile) as PNG24 -- Close new document without asking to save
	close document 1 saving (no)
end tell

Hi Hans,

I tried your solution but it still adds the “_1” suffix to the end of the file on save (ie myPNGFilename.png_1). Not sure why it’s doing this since your script has it deleting the old file first.

Weird, huh?