ASObjC script fails in debug mode

asobjc

#1

Recently, I run into an issue where a script works fine in “normal” mode but fails in debug mode. For example, the following script – built using handlers provided by @ComplexPoint – works fine in Script Editor and Script Debugger’s “normal” mode, but fails when using debug:

I’m using Script Debugger 7.0.3 with macOS High Sierra 10.13.5.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

regexMatches("\\d+", "123 jjj. 23")

-- GENERIC FUNCTIONS ----------------------------------------------------

-- https://github.com/RobTrew/prelude-applescript

-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
	tell mReturn(f)
		set lng to length of xs
		set lst to {}
		repeat with i from 1 to lng
			set end of lst to |λ|(item i of xs, i, xs)
		end repeat
		return lst
	end tell
end map

-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
	if class of f is script then
		f
	else
		script
			property |λ| : f
		end script
	end if
end mReturn

-- enumFromToInt :: Int -> Int -> [Int]
on enumFromToInt(m, n)
	if m ≤ n then
		set lst to {}
		repeat with i from m to n
			set end of lst to i
		end repeat
		return lst
	else
		return {}
	end if
end enumFromToInt

-- regexMatches :: String -> String -> [[String]]
on regexMatches(strRegex, strHay)
	set ca to current application
	-- NSNotFound handling and and High Sierra workaround due to @sl1974
	set NSNotFound to a reference to 9.22337203685477E+18 + 5807
	set oRgx to ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬
		options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) ¬
		|error|:(missing value)
	set oString to ca's NSString's stringWithString:strHay
	
	script matchString
		on |λ|(m)
			script rangeMatched
				on |λ|(i)
					tell (m's rangeAtIndex:i)
						set intFrom to its location
						if NSNotFound ≠ intFrom then
							text (intFrom + 1) thru (intFrom + (its |length|)) of strHay
						else
							missing value
						end if
					end tell
				end |λ|
			end script
			map(rangeMatched, ¬
				enumFromToInt(0, ((numberOfRanges of m) as integer) - 1))
		end |λ|
	end script
	
	map(matchString, (oRgx's matchesInString:oString ¬
		options:0 range:{location:0, |length|:oString's |length|()}) as list)
end regexMatches

(Shane Stanley) #2

Your hitting a known limitation. Debugging relies on being able to store values, and AppleScript can’t save script objects containing pointers.