Trying to cut a Photoshop image in 5 equal parts horizontally

I want to create a script to crop the image into 5 different images of the same width.

suppose the image is 1000 x 400, I want the script to crop it in 5 parts of 200 x 400.

Said that, I have create this script:


tell application "Finder"
	
	set exportFolder to (path to desktop as text) & "cropCinco:"
	if (not (exists folder exportFolder)) then
		make new folder at desktop with properties {name:"cropCinco"}
	end if
	
end tell


set listFiles to {imagem1, imagem2, imagem3, imagem4, imagem5}

tell application "Adobe Photoshop CC 2014"

	activate
	
	set theDOC to the current document
	
	tell theDOC
		
		set docWidth to the width
		set docHeight to the height
		
		set fifth to docWidth / 5
		
		set xStart to 0
		set xFinal to docWidth - fifth
		set index to 1

		repeat 5 times
			
			set fileName to exportFolder & "image" & (index as string) & ".png"

			my cropDocAndExport(theDOC, xStart, 0, xFinal, 0, fileName)
			set xStart to xStart + fifth
			set xFinal to xFinal - fifth
			set index to index + 1
			
		end repeat
		
		
	end tell
	
end tell


on cropDocAndExport(doc, cropLeft, cropTop, cropRight, cropDown, nomeFicheiro)
	
	tell application "Adobe Photoshop CC 2014"
		activate
		
		tell doc
			set currentHistoryState to current history state
			
			crop current document bounds {cropLeft, cropTop, cropRight, cropDown}
			
			export it in nomeFicheiro as save for web with options {class:save for web export options, web format:PNG, transparency:true, png eight:false, interlaced:false, dither:none, quality:100}
			
			set current history state to currentHistoryState
			
		end tell
		
	end tell
	
end cropDocAndExport

The first crop parameter passed to the method is {0, 0, 800, 0}

It crashes on the crop line with this message: Adobe Photoshop CC 2014 got an error: File/Folder expected

if I change the crop line to

crop it bounds {cropLeft, cropTop, cropRight, cropDown}

then I have this error: Adobe Photoshop CC 2014 got an error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop. - Could not complete the command because the affected area is empty or does not overlap the canvas.

Any ideas?

Fifth is a reserved word!

Index
Specifies an object by describing its position with respect to the beginning or end of a container.
For related information, see Relative (page 224).
Syntax
class [ index ] integer
integer (st | nd | rd | th ) class
( first | second | third | fourth | fifth | sixth | seventh | eighth | ninth | tenth ) class

Hi. The first error (file/folder) relates to double-referencing the document. Either tell the doc or specify crop current document”not both. The second error relates to {0, 0, 800, 0}, which is an impossible coordinate; the end width can’t be zero.

Edit: Crop is also destructive to your canvas; if you want to perform consecutive commands, your target should be a duplicate.

On further inspection, it appears you’ve used the history state to serve as an undo, which may actually be more efficient than the duplicate method, so never mind my last bit of advice, however, I discovered a few other errors. I’ve removed the handler, renamed variables, and made both math and stylistic changes, for clarity.


tell application "Finder"
	set exportFolder to (path to desktop as text) & "cropCinco:"
	if not (exists folder exportFolder) then
		make new folder at desktop with properties {name:"cropCinco"}
	end if
end tell

tell application "Adobe Photoshop CS3"
	activate
	tell document 1 --assumes exists
		set {leftmost, uppermost, docWidth, docHeight} to {0, 0, width, height}
		set decrement to docWidth / 5
		set docWidth to decrement
		set counter to 1
		set HistoryState to current history state --no need to repeat
		repeat 5 times
			export (crop it bounds {leftmost, uppermost, docWidth, docHeight}) in (exportFolder & "image" & counter & ".png") as save for web with options {class:save for web export options, web format:PNG, transparency:true, png eight:false, interlaced:false, dither:none, quality:100}
			set current history state to HistoryState
			set counter to counter + 1
			set leftmost to leftmost + decrement 
			set docWidth to docWidth + decrement --addition, not subtraction
		end repeat
	end tell
end tell