Debugging Mode erroing on Script

This script compiles and runs fine, but when I turn on Debugging mode in Script Debugger, I get

Applescript Compile Error
Expected “end” but found “on”.

I understand that Script Debugger is injecting additional invisible lines into the script to enable debugging features, so I assume something about my code it incompatible with that injection, but I have no idea how to figure out what.

--*SaveFiletypes*app*SaveFiletypes*

--Automatic ArtJobLevel calculator. No longer has any effect on ArtBot's queue system.


repeat
	set myStatus to "getting started"
	try
		tell script "ROT Additions" to if get_user_is_bot() is false then
			if the button returned of (display dialog "This script (" & my name & ") is meant for use by ArtBot." & linefeed & "Use by an employee will be logged and misuse will be taken seriously." & linefeed & "Continue?" buttons {"Quit", "Continue"} default button 1) is "Quit" then quit
			set userName to do shell script "id -F"
			tell script "ROT Additions" to slack_bot_post("artbotline", "@channel `User " & sanitize_for_http(userName) & " attempting to run ArtBot Prime script " & name of me & "`")
		end if
		
		-->> Options
		set overwriteLevels to false -- If true, allows non-level-0 orders to have their level changed.
		set filterJobs to false -- If true, runs only on Level 0 orders.
		
		
		--Get queue:
		set myStatus to "Fetching Art Needed Queue"
		set artNeeded to my get_art_needed() -- Update Date Files to check if Dropbox is syncing
		
		set myStatus to "Updating Branch Date Files"
		set theDate to (current date) - (time to GMT)
		set timeFileName to "TimeCheck.txt"
		tell script "ROT Additions"
			set scriptsPath to POSIX path of get_scripts_folder()
		end tell
		set {delimitHolder, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
		set sharedScriptsFolder to (text items 1 through -3 of scriptsPath as text)
		set AppleScript's text item delimiters to delimitHolder
		set branchFolders to paragraphs of (do shell script "find " & quoted form of sharedScriptsFolder & " -name '*Scripts' -maxdepth 2 -mindepth 1")
		repeat with aBranch in branchFolders
			set branchFiles to do shell script "ls " & quoted form of aBranch
			set timeFilePath to (aBranch & "/" & timeFileName)
			if branchFiles contains "DEPRECATED BRANCH.txt" then
				try
					do shell script "rm " & quoted form of timeFilePath
				end try
			else
				if branchFiles does not contain timeFileName then do shell script "echo \"\" > " & quoted form of timeFilePath
				set timeFileAlias to (POSIX file timeFilePath) as alias
				try
					close access timeFileAlias
				end try
				open for access timeFileAlias with write permission
				set eof of timeFileAlias to 0
				write theDate to timeFileAlias starting at eof as date
				close access timeFileAlias
			end if
		end repeat
		
		
		--Get scan date range:
		set myStatus to "Getting Scan Date Range"
		tell script "ROT Additions" to set scanDateRange to load_artBot_scan_daterange()
		
		set myStatus to "Building Progress Dialog"
		set progress description to "Scanning 0/" & (count of artNeeded) & " orders..."
		set scanIndex to 0
		set progress total steps to (count of artNeeded)
		set checkOrders to {}
		
		--FOR TESTING:
		--set artNeeded to {}
		--set checkOrders to {738833}
		--END TESTING
		
		set myStatus to "Preliminary Scan"
		set artNeededCount to (count of artNeeded)
		repeat with eachOrder in artNeeded -- Eliminate orders from the full check by their scheduled print date.
			ignoring case
				set scanIndex to scanIndex + 1
				set progress completed steps to scanIndex
				set progress description to "Performing cursory scan of " & scanIndex & "/" & artNeededCount & " orders..."
				if scheduledPrintDate of eachOrder is in {"0000-00-00", "9999-99-99"} then
					--skip unscheduled orders
				else if filterJobs is true and artJobLevel as text is not 0 then
					--Filter leveled orders
				else
					set parsedPrintDate to parse_print_date(scheduledPrintDate of eachOrder)
					if (startDate of scanDateRange is missing value or parsedPrintDate ≥ date (startDate of scanDateRange)) ¬
						and (endDate of scanDateRange is missing value or parsedPrintDate ≤ date (endDate of scanDateRange)) then
						set checkOrders to checkOrders & |id| of eachOrder
					end if
				end if
			end ignoring
		end repeat --artNeeded summary analysis
		
		set myStatus to "Begining Full Order Analysis"
		set progress total steps to (count of checkOrders)
		set doOrders to {}
		set checkIndex to 0
		set levelCounter to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
		repeat with eachOrder in checkOrders -- Full analysis using orderInfo
			try
				set myStatus to "Updating Progress"
				--Update tally:
				set checkIndex to checkIndex + 1
				set my progress description to "Determining Job Level of " & checkIndex & "/" & (count of checkOrders) & " orders..."
				set my progress completed steps to checkIndex
				set my progress additional description to ("Evaluating order #" & eachOrder & "

Level 0: " & item 1 of levelCounter & "
Level 1: " & item 2 of levelCounter & "
Level 2: " & item 3 of levelCounter & "
Level 3: " & item 4 of levelCounter & "
Level 4: " & item 5 of levelCounter & "
Level 5: " & item 6 of levelCounter)
				
				set myStatus to "Determinging Level"
				set jobLevel to 0
				
				tell script "ROT Additions" to set orderInfo to get_order_info(eachOrder)
				set myStatus to "Getting Features"
				set features of orderInfo to item 1 of |data| of features of orderInfo
				set myStatus to "Getting Report"
				set report of orderInfo to item 1 of |data| of report of orderInfo
				
				set myStatus to "Setting Per Order Variables"
				set continueChecking to true --Set this to false to lock a level in place, not to be overwritten by higher levels.
				set allowLevelUpdate to true --Set this to false to stop any level from actually being updated.
				
				-->> Begin detail checks
				
				
				-->>Non-level-0 orders:
				set myStatus to "non-level-0 Orders"
				if continueChecking and ¬
					(artJobLevel of orderInfo as text is not "0") then
					set jobLevel to artJobLevel of orderInfo
					set allowLevelUpdate to false
					set continueChecking to false
					
					set myStatus to "Level 1 Orders"
					--Level 1 orders with art changes should be recalculated:
					if (artJobLevel of orderInfo as text is "1") and ¬
						((count of |data| of artworkChanges of orderInfo) > 0) then
						set jobLevel to 2
						set allowLevelUpdate to true
						set continueChecking to true
					end if
					
					--Blocked orders:
					set myStatus to "Blocked Orders"
					if ((artworkBlocked of features of orderInfo) as text = "1") then
						set jobLevel to 0
						set allowLevelUpdate to true
						set continueChecking to false
					end if
					
				end if
				
				-- S&S Activewear Orders
				set myStatus to "S&S Activewear Orders"
				if continueChecking and ¬
					(branding of orderInfo is "ssactivewear.com") then
					set jobLevel to 4
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				
				--South By Sea orders
				set myStatus to "South By Sea orders"
				if continueChecking and ¬
					(brand of report of orderInfo is "southbysea.com") then
					set jobLevel to 6
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				set myStatus to "Design Tree Orders"
				--Design tree orders
				if continueChecking and ¬
					(customerName of orderInfo is "DSGN TREE") then
					set jobLevel to 0
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				--Blocked orders:
				set myStatus to "Blocked Orders"
				if continueChecking and ¬
					((artworkBlocked of features of orderInfo) as text = "1") ¬
						then set allowLevelUpdate to false
				
				
				--Halftones:
				set myStatus to "Halftones"
				if continueChecking and ¬
					((halftones of features of orderInfo as text) is not "0") ¬
						then set jobLevel to 5
				
				--Contractor job. No special treatment currently:
				set myStatus to "Contractor Jobs"
				if continueChecking and ¬
					(vendorName of report of orderInfo is not missing value) ¬
						then 0
				
				--High-value or complicated customer:
				set myStatus to "High Value"
				if continueChecking and ¬
					(accountStatusId of item 1 of accounts of orderInfo as text is not "1") ¬
						and jobLevel > 3 then set jobLevel to 3
				
				--Specialty print methods:
				set myStatus to "Specialty Prints"
				set hasHDD to (hdDigital of features of orderInfo)
				if class of hasHDD is boolean then set hasHDD to hasHDD as integer --all other properties return 0 or 1 as boolean, but HDD properties were set up returning TRUE and FALSE as booleans. This is supposed to be fixed. Check and coerce so it keeps working whether it's fixed or not
				
				set myStatus to "No Exceptions - Proceeding"
				if continueChecking and ¬
					((dtg of features of orderInfo) + ¬
						(embroidery of features of orderInfo) + ¬
						(printedVinyl of features of orderInfo) + ¬
						hasHDD + ¬
						(heatPress of features of orderInfo) > 0) ¬
						and jobLevel < 4 then set jobLevel to 4
				
				-->> Print area detail:
				set myStatus to "Examining Print Areas"
				if continueChecking then
					set maxInkColors to 1 --The most ink colors that appear on a single print area in the order.
					set totalRasterCount to 0
					repeat with eachTab in designs of orderInfo
						repeat with eachPrintArea in printAreas of eachTab
							set eachPrintArea to contents of eachPrintArea
							
							set myStatus to "Rasters"
							--Rasters:
							set rasterCount to (count of designFiles of eachPrintArea)
							if rasterCount > 3 then set jobLevel to 3
							set totalRasterCount to totalRasterCount + rasterCount
							
							set myStatus to "Ink Colors"
							--Ink colors:
							if printMethodId of eachPrintArea is 1 then
								set inkCount to (count of inkColors of eachPrintArea)
								if inkCount > maxInkColors then set maxInkColors to (count of inkColors of eachPrintArea)
								
								if inkCount > 1 and rasterCount > 0 then --multicolor raster flatten. Level 3 minimum
									if jobLevel < 3 then set jobLevel to 3
									if inkCount > 3 then set jobLevel to 5
								end if
							else
								set inkCount to 0
							end if
							
							if inkCount > 1 and sponsorback of eachPrintArea as text = "1" then set jobLevel to 5
							if sponsorback of eachPrintArea as text = "1" and jobLevel < 3 then set jobLevel to 3
							
						end repeat --print areas
					end repeat --tabs
					
					set myStatus to "Determining Level"
					if jobLevel is not 4 then
						if totalRasterCount > 3 and jobLevel < 3 then set jobLevel to 3
						if maxInkColors is in {3, 4} and jobLevel < 3 then set jobLevel to 3
						if maxInkColors > 4 then set jobLevel to 5
					end if
				end if
				
				set myStatus to "Bumping Level 2's"
				if continueChecking and ¬
					jobLevel = 2 and ¬
					((printAreas of report of orderInfo > 4) ¬
						or (quantity of report of orderInfo > 150) ¬
						or (orderTotal of report of orderInfo > 2000.0)) ¬
						then set jobLevel to 3
				
				set myStatus to "Bumping Back to 2"
				if continueChecking and ¬
					jobLevel < 2 then set jobLevel to 2
				
				-->> Wrap up and assign new level
				set myStatus to "Assigning Level"
				set item (jobLevel + 1) of levelCounter to (item (jobLevel + 1) of levelCounter) + 1
				if allowLevelUpdate is true ¬
					or overwriteLevels is true then
					my set_job_level(eachOrder, jobLevel)
					
					-->> Warn embroidery of new orders with 100+ placements:
					set myStatus to "Warn Embroidery"
					if allowLevelUpdate is true and jobLevel is not 0 and (embroidery of features of orderInfo as text) is not "0" then
						set orderEMBImpressions to 0
						repeat with eachDesign in designs of orderInfo
							repeat with eachPrintArea in printAreas of eachDesign
								if printMethodId of eachPrintArea is in {2, 8} then set orderEMBImpressions to orderEMBImpressions + get_design_quantity(eachDesign)
							end repeat
						end repeat
						if orderEMBImpressions ≥ 100 then
							set vendorAddonText to ""
							if vendorName of orderInfo is not missing value and vendorName of orderInfo is not "" then set vendorAddonText to "OUT OF HOUSE "
							set recipients to {"U08N1MX2N"} --Embroidery
							repeat with eachRecipient in recipients
								tell script "ROT Additions" to slack_bot_post(eachRecipient, "New order with " & orderEMBImpressions & " EMB placements scheduled for printing " & vendorAddonText & "on " & scheduledPrintDate of orderInfo & ":
https://crm2.rushordertees.com/order/" & eachOrder)
							end repeat
						end if --Impressions > 100
					end if --EMB
					
				end if -- assign level
				
				
			on error theError number errNum
				if errNum = -128 then quit
				tell script "ROT Additions" to slack_bot_post("artbotline", "Failed to level:
http://crm2.rushordertees.com/app#/order/" & eachOrder & return & "WHILE: " & myStatus & return & "Error# " & errNum & " ERROR: " & theError)
			end try
		end repeat --checkOrders
		
	on error theError number errNum
		if errNum = -128 then quit
		try
			tell script "ROT Additions" to slack_bot_post("artbotline", ">*`Art Job Level Setter failed with an error and will resume at its next scheduled run time. If an error message can be parsed, it is included below:`*")
		end try
		try
			tell script "ROT Additions" to slack_bot_post("artbotline", (">" & "Error Number: " & errNum & " Error: " & theError))
		end try
	end try
end repeat



-->> Handlers



on crm_date_offset(rawDate) --yyyy-mm-dd
	if rawDate is in {"9999-99-99", "0000-00-00"} then return 30
	try
		set oldDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "-"
		set theYear to text item 1 of rawDate
		set theMonth to text item 2 of rawDate
		set theDay to text item 3 of rawDate
		set AppleScript's text item delimiters to oldDelimiters
		set constructedDate to (theMonth & "/" & theDay & "/" & theYear) as text
		set parsedDate to date constructedDate
		set dateDifference to ((parsedDate - (current date)) / (24 * 60 * 60) + 1)
		set dateDifference to dateDifference - (dateDifference mod 1) as integer --Round down.
		return dateDifference
	on error theError number errNum
		if errNum = -128 then quit
		return 30
	end try
end crm_date_offset


on get_design_quantity(designInfo)
	set designQuantity to 0
	repeat with eachGood in |items| of designInfo
		repeat with eachSize in |data| of sizes of eachGood
			if quantity of eachSize is not missing value then set designQuantity to designQuantity + (quantity of eachSize as number)
		end repeat
	end repeat
	return designQuantity
end get_design_quantity


on set_job_level(orderNum, thisLevel)
	tell script "ROT Additions" to set authToken to get_auth_token()
	tell script "ROT Additions" to set apiDomain to get_api_domain()
	do shell script "curl -X PUT -H \"Authorization: Bearer " & authToken & "\" -H \"Content-Type: application/json\" -H \"Cache-Control: no-cache\" -d '{\"artJobLevel\": " & thisLevel & "}' \"" & apiDomain & "/orders/" & orderNum & "\""
end set_job_level


on crm_time_stamp()
	set theDate to (current date)
	set monthStamp to text 1 thru 3 of (month of theDate as text)
	set monthDate to day of theDate as text
	if monthDate ends with 1 and monthDate is not 11 then
		set dateStamp to monthDate & "st"
	else if monthDate ends with 2 and monthDate is not 12 then
		set dateStamp to monthDate & "nd"
	else if monthDate ends with 3 and monthDate is not 13 then
		set dateStamp to monthDate & "rd"
	else
		set dateStamp to monthDate & "th"
	end if
	set theTime to time string of theDate
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set hourStamp to text item 1 of theTime as text
	set minuteStamp to text item 2 of theTime as text
	set meridiemStamp to text -2 thru -1 of theTime as text
	set AppleScript's text item delimiters to oldDelimiters
	if length of hourStamp is 1 then set hourStamp to "0" & hourStamp
	set timestamp to monthStamp & " " & dateStamp & ", " & (year of theDate) & " @ " & hourStamp & ":" & minuteStamp & " " & meridiemStamp
end crm_time_stamp


on get_art_needed()
	tell script "ROT Additions" to set apiDomain to get_api_domain()
	tell script "ROT Additions" to set authToken to get_auth_token()
	
	set jsonData to do shell script "curl -X GET -H 'Authorization: Bearer " & authToken & "' -H 'Cache-Control: no-cache' '" & apiDomain & "/reports?name=ArtBotOrder'"
	if jsonData is "" then error "Could not retrieve Art Needed Report from API. Please ensure this computer is connected to the internet, and that the server is online."
	tell application "JSON Helper" to set artNeeded to |data| of (read JSON from jsonData)
	return artNeeded
end get_art_needed


on parse_print_date(aPrintDate)
	try
		set {AppleScript's text item delimiters, oldDelimiters} to {{"-"}, AppleScript's text item delimiters}
		set dYear to text item 1 of aPrintDate
		set dMonth to text item 2 of aPrintDate
		set dDay to text item 3 of aPrintDate
		set AppleScript's text item delimiters to oldDelimiters
	on error theError number errNum
		if errNum = -128 then quit
		return missing value
	end try
	return date (dMonth & "/" & dDay & "/" & dYear)
end parse_print_date

on quit
	continue quit
end quit

Since I don’t have your ROT Additions script I dan’t compile or test, but the two lines above seem problematic. What happens if you comment them out?

Thanks for the suggestion. There are 5 lines that do a slack_bot_post, but I commented out all 5 and still get the error.
It occurred to me that if this is a problem with escaping or such on the added invisible Applescript, commenting out may not be sufficient, so I deleted out all 5 lines, but still get the error.

I’d be glad to send the libraries this uses, but the libraries call libraries that call libraries… I can send the entire mess of libraries, but the libraries have many lines of “tell application [something you probably don’t have installed]” so I think the amount of work required to allow someone else to compile this is going to be problematic. I recognize that greatly complicates troubleshooting, I’m not sure what I can do about that.

Actually, good news on the dependency front. I tried deleting every “tell script…” line out of the script, and I still get the error. So this should be replicable without dependencies - others should be able to compile it without debugging on and get the error with debugging on. Here it is with all library references removed:

--*SaveFiletypes*app*SaveFiletypes*

--Automatic ArtJobLevel calculator. No longer has any effect on ArtBot's queue system.


repeat
	set myStatus to "getting started"
	try
		
		if the button returned of (display dialog "This script (" & my name & ") is meant for use by ArtBot." & linefeed & "Use by an employee will be logged and misuse will be taken seriously." & linefeed & "Continue?" buttons {"Quit", "Continue"} default button 1) is "Quit" then quit
		set userName to do shell script "id -F"
		
		
		-->> Options
		set overwriteLevels to false -- If true, allows non-level-0 orders to have their level changed.
		set filterJobs to false -- If true, runs only on Level 0 orders.
		
		
		--Get queue:
		set myStatus to "Fetching Art Needed Queue"
		set artNeeded to my get_art_needed() -- Update Date Files to check if Dropbox is syncing
		
		set myStatus to "Updating Branch Date Files"
		set theDate to (current date) - (time to GMT)
		set timeFileName to "TimeCheck.txt"
		
		set {delimitHolder, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
		set sharedScriptsFolder to (text items 1 through -3 of scriptsPath as text)
		set AppleScript's text item delimiters to delimitHolder
		set branchFolders to paragraphs of (do shell script "find " & quoted form of sharedScriptsFolder & " -name '*Scripts' -maxdepth 2 -mindepth 1")
		repeat with aBranch in branchFolders
			set branchFiles to do shell script "ls " & quoted form of aBranch
			set timeFilePath to (aBranch & "/" & timeFileName)
			if branchFiles contains "DEPRECATED BRANCH.txt" then
				try
					do shell script "rm " & quoted form of timeFilePath
				end try
			else
				if branchFiles does not contain timeFileName then do shell script "echo \"\" > " & quoted form of timeFilePath
				set timeFileAlias to (POSIX file timeFilePath) as alias
				try
					close access timeFileAlias
				end try
				open for access timeFileAlias with write permission
				set eof of timeFileAlias to 0
				write theDate to timeFileAlias starting at eof as date
				close access timeFileAlias
			end if
		end repeat
		
		
		--Get scan date range:
		set myStatus to "Getting Scan Date Range"
		
		
		set myStatus to "Building Progress Dialog"
		set progress description to "Scanning 0/" & (count of artNeeded) & " orders..."
		set scanIndex to 0
		set progress total steps to (count of artNeeded)
		set checkOrders to {}
		
		--FOR TESTING:
		--set artNeeded to {}
		--set checkOrders to {738833}
		--END TESTING
		
		set myStatus to "Preliminary Scan"
		set artNeededCount to (count of artNeeded)
		repeat with eachOrder in artNeeded -- Eliminate orders from the full check by their scheduled print date.
			ignoring case
				set scanIndex to scanIndex + 1
				set progress completed steps to scanIndex
				set progress description to "Performing cursory scan of " & scanIndex & "/" & artNeededCount & " orders..."
				if scheduledPrintDate of eachOrder is in {"0000-00-00", "9999-99-99"} then
					--skip unscheduled orders
				else if filterJobs is true and artJobLevel as text is not 0 then
					--Filter leveled orders
				else
					set parsedPrintDate to parse_print_date(scheduledPrintDate of eachOrder)
					if (startDate of scanDateRange is missing value or parsedPrintDate ≥ date (startDate of scanDateRange)) ¬
						and (endDate of scanDateRange is missing value or parsedPrintDate ≤ date (endDate of scanDateRange)) then
						set checkOrders to checkOrders & |id| of eachOrder
					end if
				end if
			end ignoring
		end repeat --artNeeded summary analysis
		
		set myStatus to "Begining Full Order Analysis"
		set progress total steps to (count of checkOrders)
		set doOrders to {}
		set checkIndex to 0
		set levelCounter to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
		repeat with eachOrder in checkOrders -- Full analysis using orderInfo
			try
				set myStatus to "Updating Progress"
				--Update tally:
				set checkIndex to checkIndex + 1
				set my progress description to "Determining Job Level of " & checkIndex & "/" & (count of checkOrders) & " orders..."
				set my progress completed steps to checkIndex
				set my progress additional description to ("Evaluating order #" & eachOrder & "

Level 0: " & item 1 of levelCounter & "
Level 1: " & item 2 of levelCounter & "
Level 2: " & item 3 of levelCounter & "
Level 3: " & item 4 of levelCounter & "
Level 4: " & item 5 of levelCounter & "
Level 5: " & item 6 of levelCounter)
				
				set myStatus to "Determinging Level"
				set jobLevel to 0

				set myStatus to "Getting Features"
				set features of orderInfo to item 1 of |data| of features of orderInfo
				set myStatus to "Getting Report"
				set report of orderInfo to item 1 of |data| of report of orderInfo
				
				set myStatus to "Setting Per Order Variables"
				set continueChecking to true --Set this to false to lock a level in place, not to be overwritten by higher levels.
				set allowLevelUpdate to true --Set this to false to stop any level from actually being updated.
				
				-->> Begin detail checks
				
				
				-->>Non-level-0 orders:
				set myStatus to "non-level-0 Orders"
				if continueChecking and ¬
					(artJobLevel of orderInfo as text is not "0") then
					set jobLevel to artJobLevel of orderInfo
					set allowLevelUpdate to false
					set continueChecking to false
					
					set myStatus to "Level 1 Orders"
					--Level 1 orders with art changes should be recalculated:
					if (artJobLevel of orderInfo as text is "1") and ¬
						((count of |data| of artworkChanges of orderInfo) > 0) then
						set jobLevel to 2
						set allowLevelUpdate to true
						set continueChecking to true
					end if
					
					--Blocked orders:
					set myStatus to "Blocked Orders"
					if ((artworkBlocked of features of orderInfo) as text = "1") then
						set jobLevel to 0
						set allowLevelUpdate to true
						set continueChecking to false
					end if
					
				end if
				
				-- S&S Activewear Orders
				set myStatus to "S&S Activewear Orders"
				if continueChecking and ¬
					(branding of orderInfo is "ssactivewear.com") then
					set jobLevel to 4
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				
				--South By Sea orders
				set myStatus to "South By Sea orders"
				if continueChecking and ¬
					(brand of report of orderInfo is "southbysea.com") then
					set jobLevel to 6
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				set myStatus to "Design Tree Orders"
				--Design tree orders
				if continueChecking and ¬
					(customerName of orderInfo is "DSGN TREE") then
					set jobLevel to 0
					set allowLevelUpdate to true
					set continueChecking to false
				end if
				
				--Blocked orders:
				set myStatus to "Blocked Orders"
				if continueChecking and ¬
					((artworkBlocked of features of orderInfo) as text = "1") ¬
						then set allowLevelUpdate to false
				
				
				--Halftones:
				set myStatus to "Halftones"
				if continueChecking and ¬
					((halftones of features of orderInfo as text) is not "0") ¬
						then set jobLevel to 5
				
				--Contractor job. No special treatment currently:
				set myStatus to "Contractor Jobs"
				if continueChecking and ¬
					(vendorName of report of orderInfo is not missing value) ¬
						then 0
				
				--High-value or complicated customer:
				set myStatus to "High Value"
				if continueChecking and ¬
					(accountStatusId of item 1 of accounts of orderInfo as text is not "1") ¬
						and jobLevel > 3 then set jobLevel to 3
				
				--Specialty print methods:
				set myStatus to "Specialty Prints"
				set hasHDD to (hdDigital of features of orderInfo)
				if class of hasHDD is boolean then set hasHDD to hasHDD as integer --all other properties return 0 or 1 as boolean, but HDD properties were set up returning TRUE and FALSE as booleans. This is supposed to be fixed. Check and coerce so it keeps working whether it's fixed or not
				
				set myStatus to "No Exceptions - Proceeding"
				if continueChecking and ¬
					((dtg of features of orderInfo) + ¬
						(embroidery of features of orderInfo) + ¬
						(printedVinyl of features of orderInfo) + ¬
						hasHDD + ¬
						(heatPress of features of orderInfo) > 0) ¬
						and jobLevel < 4 then set jobLevel to 4
				
				-->> Print area detail:
				set myStatus to "Examining Print Areas"
				if continueChecking then
					set maxInkColors to 1 --The most ink colors that appear on a single print area in the order.
					set totalRasterCount to 0
					repeat with eachTab in designs of orderInfo
						repeat with eachPrintArea in printAreas of eachTab
							set eachPrintArea to contents of eachPrintArea
							
							set myStatus to "Rasters"
							--Rasters:
							set rasterCount to (count of designFiles of eachPrintArea)
							if rasterCount > 3 then set jobLevel to 3
							set totalRasterCount to totalRasterCount + rasterCount
							
							set myStatus to "Ink Colors"
							--Ink colors:
							if printMethodId of eachPrintArea is 1 then
								set inkCount to (count of inkColors of eachPrintArea)
								if inkCount > maxInkColors then set maxInkColors to (count of inkColors of eachPrintArea)
								
								if inkCount > 1 and rasterCount > 0 then --multicolor raster flatten. Level 3 minimum
									if jobLevel < 3 then set jobLevel to 3
									if inkCount > 3 then set jobLevel to 5
								end if
							else
								set inkCount to 0
							end if
							
							if inkCount > 1 and sponsorback of eachPrintArea as text = "1" then set jobLevel to 5
							if sponsorback of eachPrintArea as text = "1" and jobLevel < 3 then set jobLevel to 3
							
						end repeat --print areas
					end repeat --tabs
					
					set myStatus to "Determining Level"
					if jobLevel is not 4 then
						if totalRasterCount > 3 and jobLevel < 3 then set jobLevel to 3
						if maxInkColors is in {3, 4} and jobLevel < 3 then set jobLevel to 3
						if maxInkColors > 4 then set jobLevel to 5
					end if
				end if
				
				set myStatus to "Bumping Level 2's"
				if continueChecking and ¬
					jobLevel = 2 and ¬
					((printAreas of report of orderInfo > 4) ¬
						or (quantity of report of orderInfo > 150) ¬
						or (orderTotal of report of orderInfo > 2000.0)) ¬
						then set jobLevel to 3
				
				set myStatus to "Bumping Back to 2"
				if continueChecking and ¬
					jobLevel < 2 then set jobLevel to 2
				
				-->> Wrap up and assign new level
				set myStatus to "Assigning Level"
				set item (jobLevel + 1) of levelCounter to (item (jobLevel + 1) of levelCounter) + 1
				if allowLevelUpdate is true ¬
					or overwriteLevels is true then
					my set_job_level(eachOrder, jobLevel)
					
					-->> Warn embroidery of new orders with 100+ placements:
					set myStatus to "Warn Embroidery"
					if allowLevelUpdate is true and jobLevel is not 0 and (embroidery of features of orderInfo as text) is not "0" then
						set orderEMBImpressions to 0
						repeat with eachDesign in designs of orderInfo
							repeat with eachPrintArea in printAreas of eachDesign
								if printMethodId of eachPrintArea is in {2, 8} then set orderEMBImpressions to orderEMBImpressions + get_design_quantity(eachDesign)
							end repeat
						end repeat
						if orderEMBImpressions ≥ 100 then
							set vendorAddonText to ""
							if vendorName of orderInfo is not missing value and vendorName of orderInfo is not "" then set vendorAddonText to "OUT OF HOUSE "
							set recipients to {"U08N1MX2N"} --Embroidery
							repeat with eachRecipient in recipients
								
							end repeat
						end if --Impressions > 100
					end if --EMB
					
				end if -- assign level
				
				
			on error theError number errNum
				if errNum = -128 then quit
			end try
		end repeat --checkOrders
		
	on error theError number errNum
		if errNum = -128 then quit
	end try
end repeat



-->> Handlers



on crm_date_offset(rawDate) --yyyy-mm-dd
	if rawDate is in {"9999-99-99", "0000-00-00"} then return 30
	try
		set oldDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "-"
		set theYear to text item 1 of rawDate
		set theMonth to text item 2 of rawDate
		set theDay to text item 3 of rawDate
		set AppleScript's text item delimiters to oldDelimiters
		set constructedDate to (theMonth & "/" & theDay & "/" & theYear) as text
		set parsedDate to date constructedDate
		set dateDifference to ((parsedDate - (current date)) / (24 * 60 * 60) + 1)
		set dateDifference to dateDifference - (dateDifference mod 1) as integer --Round down.
		return dateDifference
	on error theError number errNum
		if errNum = -128 then quit
		return 30
	end try
end crm_date_offset


on get_design_quantity(designInfo)
	set designQuantity to 0
	repeat with eachGood in |items| of designInfo
		repeat with eachSize in |data| of sizes of eachGood
			if quantity of eachSize is not missing value then set designQuantity to designQuantity + (quantity of eachSize as number)
		end repeat
	end repeat
	return designQuantity
end get_design_quantity


on set_job_level(orderNum, thisLevel)
	do shell script "curl -X PUT -H \"Authorization: Bearer " & authToken & "\" -H \"Content-Type: application/json\" -H \"Cache-Control: no-cache\" -d '{\"artJobLevel\": " & thisLevel & "}' \"" & apiDomain & "/orders/" & orderNum & "\""
end set_job_level


on crm_time_stamp()
	set theDate to (current date)
	set monthStamp to text 1 thru 3 of (month of theDate as text)
	set monthDate to day of theDate as text
	if monthDate ends with 1 and monthDate is not 11 then
		set dateStamp to monthDate & "st"
	else if monthDate ends with 2 and monthDate is not 12 then
		set dateStamp to monthDate & "nd"
	else if monthDate ends with 3 and monthDate is not 13 then
		set dateStamp to monthDate & "rd"
	else
		set dateStamp to monthDate & "th"
	end if
	set theTime to time string of theDate
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set hourStamp to text item 1 of theTime as text
	set minuteStamp to text item 2 of theTime as text
	set meridiemStamp to text -2 thru -1 of theTime as text
	set AppleScript's text item delimiters to oldDelimiters
	if length of hourStamp is 1 then set hourStamp to "0" & hourStamp
	set timestamp to monthStamp & " " & dateStamp & ", " & (year of theDate) & " @ " & hourStamp & ":" & minuteStamp & " " & meridiemStamp
end crm_time_stamp


on get_art_needed()
	
	
	set jsonData to do shell script "curl -X GET -H 'Authorization: Bearer " & authToken & "' -H 'Cache-Control: no-cache' '" & apiDomain & "/reports?name=ArtBotOrder'"
	if jsonData is "" then error "Could not retrieve Art Needed Report from API. Please ensure this computer is connected to the internet, and that the server is online."
	tell application "JSON Helper" to set artNeeded to |data| of (read JSON from jsonData)
	return artNeeded
end get_art_needed


on parse_print_date(aPrintDate)
	try
		set {AppleScript's text item delimiters, oldDelimiters} to {{"-"}, AppleScript's text item delimiters}
		set dYear to text item 1 of aPrintDate
		set dMonth to text item 2 of aPrintDate
		set dDay to text item 3 of aPrintDate
		set AppleScript's text item delimiters to oldDelimiters
	on error theError number errNum
		if errNum = -128 then quit
		return missing value
	end try
	return date (dMonth & "/" & dDay & "/" & dYear)
end parse_print_date

on quit
	continue quit
end quit

Even better news, I was able to track down the offending line.

			if the button returned of (display dialog "This script (" & my name & ") is meant for use by ArtBot." & linefeed & "Use by an employee will be logged and misuse will be taken seriously." & linefeed & "Continue?" buttons {"Quit", "Continue"} default button 1) is "Quit" then quit

I’m still not sure why it doesn’t work, but if I take that out, debugging mode works fine. I’ll try retooling it to maintain functionality without killing debugging.

Simply revising the text used for the dialog to this:

if the button returned of (display dialog "This script is meant for use by ArtBot." & linefeed & "Continue?" buttons {"Quit", "Continue"} default button 1) is "Quit" then quit

Does it, which is fine.

Experimenting a bit more, it’s

(" & my name & ")

That it does not like. I’ll leave it here, but developers may want to look further to bypass error.