Renaming and Filing Images

I have a script that processes images in Photoshop, renames them and then files them in various folders on a server according to their filename. I’m getting an error -4960 SOME of the time when I drop images onto this applescript droplet. Sometimes, the script actually works perfectly. I’ve noticed a pattern: the images process when _s_4c is already in the filename. If _s_4c is not in the filename, the script will process an image in photoshop, rename the image by adding _s_4c to the filename and then throw up the error and stop processing images.

The way I have it set up, the text item delimiters will take off the _s_4c if it exists and then add it back on. This way, I can drop a whole bunch of images onto the droplet and have them all named consistently even when what I get is inconsistent. I’m sure there is something I need to change in the renaming and filing part of the code. If someone could help me figure it out, I’d really appreciate it.

As I don’t own/use Photoshop I can’t test if my understanding is right.

When your script rename a file inserting " _s_4c" in the name, the variable fileToMove contains the path to the renamed file but the variable aFile is not changed.
So, when the script enter the next block of instructions, when it define fileToMove to aFile it seems logical to get an error because there is no longer a file with the old path.

Try to add one instruction :

–RESETTING APPLESCRIPT’S TEXT ITEM DELIMITERS AND NAME
set AppleScript’s text item delimiters to oldDelims
set name of fileToMove to fileToMove_Name
set fileName to fileToMove # ADDED

–TELLING FINDER TO RESET APPLESCRIPT’S TEXT ITEM DELIMITERS IF THERE’S AN ERROR

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) vendredi 23 octobre 2015 11:07:08

I tried adding that line and got the same error, but I think you are on the right track as the script does work if I comment out the renaming part.

I’m just an ass.

The added line was supposed to be :

set afile to fileToMove # ADDED

I don’t know why I wrote :
set fileName to fileToMove # ADDED

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) vendredi 23 octobre 2015 17:25:30

This is the current code. It’s still popping up the -4960 error. Interestingly enough, after I get the error, if I try to drop the renamed file on the droplet a second time, it will process successfully.

on open droppedfiles
	
	repeat with afile in droppedfiles
		
		--MAKING A BACKUP COPY OF THE IMAGE TO A FOLDER BEFORE PROCESSING
		tell application "Finder"
			activate
			try
				set backupFolder to alias "Users:mbornbach:Desktop:Pre-Silo Backups:"
				--"Volumes:nas:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Melissa Processing:Scripts:Pre-Silo Backups:"
				set fileToMove to afile
				duplicate fileToMove to backupFolder with replacing
				
			end try
		end tell
		
		--ON TO THE PHOTOSHOP PROCESSING
		tell application "Adobe Photoshop CC 2014"
			activate
			set display dialogs to never
			open afile showing dialogs never
			
			--TELLING PHOTOSHOP TO USE PIXEL MEASUREMENTS AND SETTING THE TARGET IMAGE SIZE SO IT CAN RESIZE THE IMAGE LATER ON
			set ruler units of settings to pixel units
			set target_size to 1550
			
			--DOING ALL THE PHOTOSHOP WORK
			tell current document
				try
					
					--CHANGING TO CMYK
					change mode to CMYK
					
					--FLATTENING IMAGE
					flatten
					
					--SETTING NAME OF PATH AND TURNING IT INTO A CLIPPING PATH
					select first path item
					if name of first path item is not "Path 1" then set name of first path item to "Path 1"
					make clipping path of first path item flatness 2
					
					--CROPPING THE IMAGE
					create selection first path item feather amount 0 with antialiasing
					expand selection by 25
					set theCropBounds to bounds of selection
					crop bounds theCropBounds
					
					--RESIZING THE IMAGE IF IT IS TOO BIG
					set long_side to height
					set long_side_identity to 1
					if width > long_side then set long_side_identity to 2
					if width > long_side then set long_side to width
					if long_side > target_size as integer and long_side_identity = 1 then
						resize image height target_size resolution 300 resample method bicubic
					end if
					if long_side > target_size as integer and long_side_identity = 2 then
						resize image width target_size resolution 300 resample method bicubic
					end if
					
					close saving yes
					
					--TELLING PHOTOSHOP WHAT TO DO IF THERE'S AN ERROR
				on error
					close afile
					
					tell application "Finder"
						activate
						set fileToMove to afile
						set oldDelims to AppleScript's text item delimiters
						set fileExt to name extension of fileToMove
						
						--ADDING _ERROR TO FILENAME FOR PROBLEM FILES
						set AppleScript's text item delimiters to {"."}
						set fileName to name of fileToMove --> ex. Disneyland.tif
						set nameWithoutExtension to first text item of fileName --> Disneyland
						set newName to nameWithoutExtension & "_ERROR" & "." & fileExt --> Disneyland_s_4c.tif
						set name of fileToMove to newName
						
						--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
						set AppleScript's text item delimiters to oldDelims
						set name of fileToMove to fileToMove_Name
						
					end tell
				end try
			end tell
			
			
			--DONE WITH PHOTOSHOP, NOW USING THE FINDER TO RENAME THE FILE, ADDING  _s_4c to THE IMAGE NAME
			tell application "Finder"
				activate
				set fileToMove to afile
				set fileToMove_Name to name of fileToMove
				set fileExt to name extension of fileToMove
				
				set oldDelims to AppleScript's text item delimiters
				
				try
					--ELIMINATING _4c on IMAGES THAT ALREADY HAD _4c IN FILE NAME BEFORE PROCESSING
					if name of fileToMove contains "_4c" and name of fileToMove does not contain "_s_4c" then
						set AppleScript's text item delimiters to {"_4c"}
						
						--ELIMINATING DOUBLE _s_4c on IMAGES THAT ALREADY HAD _s_4c IN FILE NAME BEFORE PROCESSING
					else if name of fileToMove contains "_s_4c" then
						set AppleScript's text item delimiters to {"_s_4c"}
						
						--FOR IMAGES THAT DIDN'T HAVE _s_4c IN FILE NAME BEFORE PROCESSING    
					else if name of fileToMove does not contain "_s_4c" or "_4c" then
						set AppleScript's text item delimiters to {"."}
					end if
					
					set fileName to name of fileToMove --> ex. Disneyland.tif
					set nameWithoutExtension to first text item of fileName --> Disneyland
					set newName to nameWithoutExtension & "_s_4c" & "." & fileExt --> Disneyland_s_4c.tif
					set name of fileToMove to newName
					
					--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS AND NAME
					set AppleScript's text item delimiters to oldDelims
					set name of fileToMove to fileToMove_Name
					set afile to fileToMove # ADDED
					
					--TELLING FINDER TO RESET APPLESCRIPT'S TEXT ITEM DELIMITERS IF THERE'S AN ERROR
				on error
					set AppleScript's text item delimiters to oldDelims
					
				end try
			end tell
			
			--FILING THE IMAGES
			tell application "Finder"
				activate
				
				set fileToMove to afile
				set fileToMove_Name to name of fileToMove as string
				set fileExt to name extension of fileToMove as string
				
				--DUPLICATE FILE TO DEB'S CONVERT FOLDER ON R DRIVE
				try
					if fileExt is "tif" then
						duplicate fileToMove to alias "Volumes:sdrive:IMAGES_FROM PREPRESS:z_DEBORAH_CONVERT and FILE:"
					else
						move fileToMove to alias "Volumes:server1:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Processing:Scripts:Convert:"
					end if
					
					--Moving file to the "File Manually in RDRIVE & 1_4c IMAGES folder" inside the Processing Folder if there is an error
				on error
					move fileToMove to alias "Volumes:server1:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Processing:Scripts:File Manually in RDRIVE & 1_4c IMAGES:" with replacing
				end try
				
				
				
				--ONTO FILING INTO THE 1_4C IMAGES FOLDERS
				try
					
					--DECLARING LISTS OF STRINGS OF CHARACTERS FOR THE COMPUTER TO LOOK FOR
					
					--AG Files
					set AG00string to {"AG00", "AG01", "AG02", "AG03"}
					set AG04string to {"AG04", "AG05", "AG06", "AG07", "AG08", "AG09"}
					set AGAstring to {"AGA", "AGB", "AGC", "AGD", "AGE", "AGF", "AGG", "AGH", "AGI", "AGJ", "AGK", "AGL", "AGM", "AGN", "AGO", "AGP", "AGQ", "AGR", "AGS", "AGT", "AGU", "AGV", "AGW", "AGX", "AGY", "AGZ"}
					set AG_Astring to {"AG_A", "AG_B", "AG_C", "AG_D", "AG_E", "AG_F", "AG_G", "AG_H", "AG_I", "AG_J", "AG_K", "AG_L", "AG_M", "AG_N", "AG_O", "AG_P", "AG_Q", "AG_R", "AG_S", "AG_T", "AG_U", "AG_V", "AG_W", "AG_X", "AG_Y", "AG_Z"}
					
					--BI Files
					set BI0string to {"BI0", "BI1", "BI2", "BI3", "BI4", "BI5"}
					set BI6string to {"BI6"}
					set BI7string to {"BI7"}
					set BI8string to {"BI8"}
					set BI9string to {"BI9"}
					set BIAstring to {"BIA", "BIB", "BIC", "BID", "BIE", "BIF", "BIG", "BIH", "BII", "BIJ", "BIK", "BIL", "BIM", "BIN", "BIO", "BIP", "BIQ", "BIR", "BIS", "BIT", "BIU", "BIV", "BIW", "BIX", "BIY", "BIZ"}
					set BI_Astring to {"BI_A", "BI_B", "BI_C", "BI_D", "BI_E", "BI_F", "BI_G", "BI_H", "BI_I", "BI_J", "BI_K", "BI_L", "BI_M", "BI_N", "BI_O", "BI_P", "BI_Q", "BI_R", "BI_S", "BI_T", "BI_U", "BI_V", "BI_W", "BI_X", "BI_Y", "BI_Z"}
					
										
					--MOVING THE ACTUAL FILES TO THE 1_4C IMAGES FOLDERS
					
					--Moving AG files to 1_4c IMAGES Folders	
					if AG00string contains (text 1 thru 4 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG00 - AG03 - HI RES:"
					else if AG04string contains (text 1 thru 4 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG04 - AG09 - HI RES:"
					else if AGAstring contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AGA - AGZ - HI RES:"
					else if AG_Astring contains (text 1 thru 4 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AGA - AGZ - HI RES:"
						
						--Moving BI files to 1_4c IMAGES Folders
					else if BI0string contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BI0 - BI5 - HI RES:"
					else if BI6string contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BI6 - HI RES:"
					else if BI7string contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BI7 - HI RES:"
					else if BI8string contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BI8 - HI RES:"
					else if BI9string contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BI9 - HI RES:"
					else if BIAstring contains (text 1 thru 3 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BIA - BIZ - HI RES:"
					else if BI_Astring contains (text 1 thru 4 of fileToMove_Name) then
						move fileToMove to alias "Volumes:server1:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:BI-high res images:BIA - BIZ - HI RES:"
						
						--Moving All Other Files to the "File Manually in 1_4c IMAGES" Folder inside the Processing Folder
					else
						move fileToMove to alias "Volumes:server1:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Processing:Scripts:File Manually in 1_4c IMAGES:"
					end if
					
					--Moving Files to the "File Manually in 1_4c IMAGES" Folder inside the Processing Folder if there is an error
				on error
					move fileToMove to alias "Volumes:server1:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Processing:Scripts:File Manually in 1_4c IMAGES:"
				end try
				
			end tell
		end tell
	end repeat
end open




If I read well, there are two blocks in wich you rename a file.
The given insertion must be done in both.

 --RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
                   set AppleScript's text item delimiters to oldDelims
                   set name of fileToMove to fileToMove_Name

set aFile to fileToMove # ADDED
end tell
end try
end tell

   --DONE WITH PHOTOSHOP, NOW USING THE FINDER TO RENAME THE FILE, ADDING _s_4c to THE IMAGE NAME

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) samedi 24 octobre 2015 09:22:47

I was still having trouble with getting that -4960 error message right after the code renames the file but before it moves it, so I tried simplifying the code down to the part that is giving me trouble.

The simplified code also brings up the -4960 error for all images that did not have _s_4c at the end of the filename when I dropped them onto the applescript droplet. If the image does have _s_4c at the end of the filename when I drop it onto the droplet, the script works and will move the image any folder(s) I want.

Also, the script does work if I remove the following line but then the images that didn’t have the _s_4c at the end of the filename are not named correctly.

set name of afile to newName

Here’s the full simplified code:

on open droppedfiles
    
    repeat with afile in droppedfiles
        
        
        --USING THE FINDER TO RENAME THE FILE, ADDING  _s_4c to THE IMAGE NAME
        tell application "Finder"
            activate
            set fileExt to name extension of afile
            set oldDelims to AppleScript's text item delimiters
            
            try
                --ELIMINATING _4c on IMAGES THAT ALREADY HAD _4c IN FILE NAME BEFORE PROCESSING
                if name of afile contains "_4c" and name of afile does not contain "_s_4c" then
                    set AppleScript's text item delimiters to {"_4c"}
                    
                    --ELIMINATING DOUBLE _s_4c on IMAGES THAT ALREADY HAD _s_4c IN FILE NAME BEFORE PROCESSING
                else if name of afile contains "_s_4c" then
                    set AppleScript's text item delimiters to {"_s_4c"}
                    
                    --FOR IMAGES THAT DIDN'T HAVE _s_4c IN FILE NAME BEFORE PROCESSING	
                else if name of afile does not contain "_s_4c" or "_4c" then
                    set AppleScript's text item delimiters to {"."}
                end if
                
                set fileName to name of afile --> ex. Disneyland.tif
                set nameWithoutExtension to first text item of fileName --> Disneyland
                set newName to nameWithoutExtension & "_s_4c" & "." & fileExt --> Disneyland_s_4c.tif
                set name of afile to newName
                
                
                --RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS AND NAME	
                set AppleScript's text item delimiters to oldDelims
                
                
                --FILING THE IMAGES			
                --DUPLICATE FILE TO WEB CONVERT FOLDER ON R DRIVE
                duplicate afile to alias "Volumes:sblain$:IMAGES_FROM PREPRESS:z_DEBORAH_CONVERT and FILE:"
                
                --Moving file to the "File Manually in RDRIVE & 1_4c IMAGES folder" inside the Processing Folder if there is an error
            on error
                set AppleScript's text item delimiters to oldDelims
                move afile to alias "Volumes:nas:Advertising Department:5_ADVERTISING DEPT - PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Melissa Processing:Scripts:File Manually in RDRIVE & 1_4c IMAGES:" with replacing
            end try
            
            
        end tell
    end repeat
end open

Browser: Safari 537.78.2
Operating System: Mac OS X (10.7)

Hi mbornbach,

The code works ok for me.
Have you tried checking what’s happening with the text item delimiters?


display dialog "]" & AppleScript's text item delimiters & "["

What did you name the file when it worked for you? When I have a file named AG012345(10_15).tif, the script does not work and the text item delimiters display as ][ in the dialog box and then I get the -4960 error has occurred message. When I have a file named AG012345(10_15)_s_4c.tif, the script works and the text item delimiters display as ][ in the dialog box.

I originally named the file in a number of ways to test different parts of your simplified script.
I’ve just tested the script with a file named ‘AG012345(10_15).tif’ and the script worked ok for me. The file was renamed and then moved to a folder I’ve set up.

Hmmmm. Thanks. I think I’ll try moving it to folders on the desktop instead of the server to see if that works. If not, I’m curious to try it on a different computer with a different operating system.

Yeah, I didn’t mention that. I’ve been testing the script on my desktop.

EDIT: I’ve just tried it over the network, to a folder on a network drive, and it still works ok. The file is renamed and a duplicate created etc.