Adobe Illustrator step and repeat script

Hi

I am not expert when it comes to apple scripting but I wrote this script a while ago and it has been such a great tool that I just had to share it. Also I wanted to give something back to this great community that has helped me become a better scripter.

It is aimed at people who use Adobe Illustrator and tested on CC2017 but should work on others too.

WHAT THIS SCRIPT DOES

This script takes a masked artwork (recommend a rectangle to the size of your artwork excluding any bleed) in Adobe Illustrator and steps and repeats it across and down the document a set number of times defined by the user.

It then drags out the mask (by 15mm) around outside of the new grid of artworks so that the outside edge of the stepped artwork is exposed.

This is particularly handy if you have a label or artwork that need stepping to fit a multi-up cutter drawing for example.

Please feel free to use this tool with my compliments

Anyway…Hope to works for you.

Enjoy

Tim

P.S. I am aware that the script is a bit clunky and a few redundancies but it works just the same.

P.P.S. Let me know if you found this useful.

---WHAT THIS SCRIPT DOES---

--  This script takes a masked artwork (recommend a rectangle to the size of your artwork excluding any bleed) in Adobe Illustrator and steps and repeats it across and down the document a set number of times defined by the user.

--  It then drags out the mask (by 15mm) around outside of the new grid of artworks  so that the outside edge of the stepped artwork is exposed.

--  This is particularly handy if you have a label or artwork that need stepping to fit a cutter drawing for example.

-- Written by Tim Baldwin

--  Please feel free to use this tool with my compliments

property offsetA : 15
set offsetA to 15

tell application id "com.adobe.illustrator"
	activate
	if (count of every document) is not 0 then
		tell document 1
			set selectedmask to selection
			
			
			--check that slection is a masked item
			if my selectioncheck(selectedmask) is not true then return --End Script
			
			
			--Get Configuration to step
			set StepConfiguration to my getStepConfiguration()
			set StepAcross to item 1 of StepConfiguration
			set StepDown to item 2 of StepConfiguration
			set RotateAcross to item 3 of StepConfiguration
			set RotateDown to item 4 of StepConfiguration
			-- RotateAcross, RotateDown
			
			if StepAcross = 1 and StepDown = 1 then
				display dialog "1x1 means no stepping neccessery." buttons "OK" default button 1
				return --End Script
			end if
			
			set selectedmask to item 1 of selectedmask
			
			set offsetA to my convert_from_mm_to_pt(offsetA)
			set pathlisttoAjust to {}
			
			set pathlisttoAjust to my StepThisItem(selectedmask, StepAcross, StepDown, RotateAcross, RotateDown)
			
			my IllustratorMaskPathAdjust(pathlisttoAjust)
			
		end tell
	end if
end tell


on StepThisItem(selectedmask, StepAcross, StepDown, RotateAcross, RotateDown)
	tell application id "com.adobe.illustrator"
		if (count of every document) is not 0 then
			tell document 1
				set x to selection
				log x
				set x to item 1 of x
				set name of x to "Middle_Middle"
				repeat with ii from 1 to (StepAcross * StepDown) - 1
					duplicate group item "Middle_Middle"
				end repeat
				
				set MaskedItemstoMove to every group item whose name is "Middle_Middle"
				set StepAcrossTab to 0
				set StepDownTab to 0
				set pathlisttoAjust to {}
				set rotateThisItem to false
				repeat with ii from 1 to count of MaskedItemstoMove
					tell item ii of MaskedItemstoMove
						set name to ""
						set WidthofPath to width of path item 1
						set HeightofPath to height of path item 1
						set XPositionofPath to item 1 of position
						set YPositionofPath to item 2 of position
						set position to {XPositionofPath + (WidthofPath * StepAcrossTab), YPositionofPath - (HeightofPath * StepDownTab)}
						
						
						if StepAcrossTab is 0 then
							set name of path item 1 to "Middle_Left"
							if StepDownTab is 0 then set name of path item 1 to "Top_Left"
							if StepDownTab is (StepDown - 1) then set name of path item 1 to "Bottom_Left"
						else
							if StepAcrossTab is (StepAcross - 1) then
								set name of path item 1 to "Middle_Right"
								if StepDownTab is 0 then set name of path item 1 to "Top_Right"
								if StepDownTab is (StepDown - 1) then set name of path item 1 to "Bottom_Right"
								
							else
								if StepAcrossTab > 0 then
									set name of path item 1 to "Middle_Middle"
									if StepDownTab is 0 then set name of path item 1 to "Middle_Top"
									if StepDownTab is (StepDown - 1) then set name of path item 1 to "Middle_Bottom"
								end if
							end if
						end if
						if StepAcross is 1 then
							set name of path item 1 to "Middle_Left_Right"
							if StepDownTab is 0 then set name of path item 1 to "Top_Top"
							if StepDownTab is (StepDown - 1) then set name of path item 1 to "Bottom_Bottom"
						end if
						if StepDown is 1 then
							set name of path item 1 to "Middle_Top_Bottom"
							if StepAcrossTab is 0 then set name of path item 1 to "Left_Left"
							if StepAcrossTab is (StepAcross - 1) then set name of path item 1 to "Right_Right"
						end if
						set xx to name of path item 1
						
						log xx
						
						copy path item 1 to the end of pathlisttoAjust
						
						--Rotate Across
						if RotateAcross is true then
							if StepAcrossTab = 0 and StepAcross > 1 then set rotateThisItem to false
							if rotateThisItem is true then my RotateThisMask(item ii of MaskedItemstoMove)
							if rotateThisItem is true then
								set rotateThisItem to false
							else
								set rotateThisItem to true
							end if
							set StepAcrossTab to StepAcrossTab + 1
							if StepAcrossTab = StepAcross then
								set StepAcrossTab to 0
								set StepDownTab to StepDownTab + 1
							end if
						else
							if RotateDown is true then
								if StepDownTab = 0 and StepDown > 1 then set rotateThisItem to false
								if rotateThisItem is true then my RotateThisMask(item ii of MaskedItemstoMove)
								if rotateThisItem is true then
									set rotateThisItem to false
								else
									set rotateThisItem to true
								end if
								set StepDownTab to StepDownTab + 1
								if StepDownTab = StepDown then
									set StepDownTab to 0
									set StepAcrossTab to StepAcrossTab + 1
								end if
							else
								set StepAcrossTab to StepAcrossTab + 1
								if StepAcrossTab = StepAcross then
									set StepAcrossTab to 0
									set StepDownTab to StepDownTab + 1
								end if
							end if
						end if
						
						--Rotate Down
						
						
						
						
					end tell
					
				end repeat
				--set ii to name of every group item
			end tell
		end if
	end tell
	return pathlisttoAjust
end StepThisItem

on RotateThisMask(ThisItem)
	tell application id "com.adobe.illustrator"
		if (count of every document) is not 0 then
			tell document 1
				set PositionofPathItem to position of path item 1 of ThisItem
				set RV_MatrixA to ¬
					{class:matrix, mvalue_a:-1.0, mvalue_b:0.0, mvalue_c:0.0, mvalue_d:1.0, mvalue_tx:0.0, mvalue_ty:0.0}
				set RV_MatrixB to ¬
					{class:matrix, mvalue_a:1.0, mvalue_b:0.0, mvalue_c:0.0, mvalue_d:-1.0, mvalue_tx:0.0, mvalue_ty:0.0}
				
				transform ThisItem using RV_MatrixA
				
				transform ThisItem using RV_MatrixB
				set NewPositionofPathItem to position of path item 1 of ThisItem
				set PositionofGroupItem to position of ThisItem
				set position of ThisItem to {(item 1 of PositionofGroupItem) - ((item 1 of NewPositionofPathItem) - (item 1 of PositionofPathItem)), (item 2 of PositionofGroupItem) - ((item 2 of NewPositionofPathItem) - (item 2 of PositionofPathItem))}
			end tell
		end if
	end tell
	
end RotateThisMask

on getStepConfiguration()
	tell application id "com.adobe.illustrator"
		activate
		repeat
			set RotateAcross to false
			set RotateDown to false
			set ii to display dialog "Please enter stepping configuration required
	e.g. 4x2 is 4 across by 2 down = 8up" default answer "1x1" buttons {"Rotate Down", "Rotate Across", "Straight stepping"} default button 3
			if button returned of ii is "Rotate Across" then set RotateAcross to true
			if button returned of ii is "Rotate Down" then set RotateDown to true
			if button returned of ii is not "Cancel" then
				set xx to my Tab_Setup(text returned of ii, "x")
				try
					return {item 1 of xx as number, item 2 of xx as number, RotateAcross, RotateDown}
				on error
					display dialog "Didn't recognised configuration. Please try again." buttons "OK" default button 1
				end try
				
			end if
			--display dialog "Wrong format.  Please use whole numbers e.g. 4x2" buttons "Try again" default button 1
		end repeat
	end tell
end getStepConfiguration



-- This subroutine returns a list is strings taken from one long string divided by the given delimiter
on Tab_Setup(target_string, delimiter)
	set savedTextItemDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delimiter
	set New_String to every text item of target_string as list
	set AppleScript's text item delimiters to savedTextItemDelimiters
	return New_String
end Tab_Setup

on IllustratorMaskPathAdjust(PathstoAdjust)
	tell application id "com.adobe.illustrator"
		tell document 1
			repeat with ii from 1 to count of PathstoAdjust
				set ThisPathtoAdjust to item ii of PathstoAdjust
				
				tell ThisPathtoAdjust
					
					set PathName to name
					
					set WidthofPath to width
					set HeightofPath to height
					set XPositionofPath to item 1 of position
					set YPositionofPath to item 2 of position
					
					if PathName is "Top_Left" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath - offsetA, YPositionofPath + offsetA}
					end if
					
					if PathName is "Bottom_Left" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath - offsetA, YPositionofPath}
					end if
					
					if PathName is "Top_Right" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath, YPositionofPath + offsetA}
					end if
					
					if PathName is "Bottom_Right" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath, YPositionofPath}
					end if
					
					if PathName is "Top_Top" then
						set width to WidthofPath + offsetA + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath - offsetA, YPositionofPath + offsetA}
					end if
					
					if PathName is "Bottom_Bottom" then
						set width to WidthofPath + offsetA + offsetA
						set height to HeightofPath + offsetA
						set position to {XPositionofPath - offsetA, YPositionofPath}
					end if
					
					if PathName is "Middle_Top" then
						set width to WidthofPath
						set height to HeightofPath + offsetA
						set position to {XPositionofPath, YPositionofPath + offsetA}
					end if
					
					if PathName is "Middle_Bottom" then
						set width to WidthofPath
						set height to HeightofPath + offsetA
						set position to {XPositionofPath, YPositionofPath}
					end if
					
					if PathName is "Middle_Left" then
						set width to WidthofPath + offsetA
						set height to HeightofPath
						set position to {XPositionofPath - offsetA, YPositionofPath}
					end if
					
					if PathName is "Middle_Right" then
						set width to WidthofPath + offsetA
						set height to HeightofPath
						set position to {XPositionofPath, YPositionofPath}
					end if
					
					if PathName is "Middle_Middle" then
						set width to WidthofPath
						set height to HeightofPath
						set position to {XPositionofPath, YPositionofPath}
					end if
					
					if PathName is "Middle_Left_Right" then
						set width to WidthofPath + offsetA + offsetA
						set height to HeightofPath
						set position to {XPositionofPath - offsetA, YPositionofPath}
					end if
					
					if PathName is "Middle_Top_Bottom" then
						set width to WidthofPath
						set height to HeightofPath + offsetA + offsetA
						set position to {XPositionofPath, YPositionofPath + offsetA}
					end if
					
					if PathName is "Left_Left" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA + offsetA
						set position to {XPositionofPath - offsetA, YPositionofPath + offsetA}
					end if
					
					if PathName is "Right_Right" then
						set width to WidthofPath + offsetA
						set height to HeightofPath + offsetA + offsetA
						set position to {XPositionofPath, YPositionofPath + offsetA}
					end if
					
					set name to ""
					log PathName
				end tell
			end repeat
		end tell
	end tell
end IllustratorMaskPathAdjust


-- This subroutine check that the selected group is a masked item
on selectioncheck(selectedmask)
	tell application id "com.adobe.illustrator"
		tell document 1
			activate
			log selectedmask
			if (count of selectedmask) = 0 then
				display dialog "No items in selection - Please select the masked group you want to step." buttons "OK" default button 1
				return false
			end if
			if (count of selectedmask) > 1 then
				display dialog "Too many in selection - Please select the masked group you want to step." buttons "OK" default button 1
				return false
			end if
			
			set selectedmask to item 1 of selectedmask
			log selectedmask
			
			if class of selectedmask is not group item then
				display dialog "Not a group item - Please select the masked group you want to step." buttons "OK" default button 1
				return false
			end if
			
			set thismaskitems to every page item in selectedmask
			
			set maskpath to item 1 of thismaskitems
			
			if class of item 1 of thismaskitems is not path item then
				display dialog "Not a masked group - Please select the masked group you want to step." buttons "OK" default button 1
				return false
			end if
			
			set maskproperties to properties of maskpath
			
			if clipping of item 1 of maskpath is not true then
				display dialog "Wrong selection - just select the masked item you want to step." buttons "OK" default button 1
				return false
			end if
		end tell
	end tell
	return true
	
end selectioncheck


-- This subroutine converts millimetres to points
to convert_from_mm_to_pt(mm)
	set pt to mm * 2.834645
	return pt
end convert_from_mm_to_pt

-- This subroutine converts points to millimetres
to convert_from_pt_to_mm(pt)
	set mm to pt / 2.834645
	return mm
end convert_from_pt_to_mm


to DuplcateMask()
	tell application id "com.adobe.illustrator"
		
		tell document 1
			set selectedmask to selection
			
			set xx to duplicate selectedmask
			set ii to selection
		end tell
		
	end tell
end DuplcateMask