NSRegularExpression doesn’t understand ... message

Coming from a Google + copy ‘n’ paste background / a very small amount of knowledge is dangerous, I’d appreciate some guidance on an issue I’m stuck on. I misunderstood the sandbox related errors in Console.app to be the source of my woes but that turns out not to be the case. Learning about ‘on error’ has been revealing! My issue is ‘NSRegularExpression doesn’t understand the “regularExpressionWithPattern_options_error_” message.’

Here’s my usage and scripts:

  • I’ve decided to use a Mail.app rule (I’m on 10.9.3 - Mail just has OmniGroup’s Clip-o-tron installed) which calls an AppleScript to process incoming tasks from MS TFS
  • I’m using the Mail rule template for this along with a library created based on http://macscripter.net/viewtopic.php?pid=168797#p168797
  • In ~/Library/Script Libraries/ I have FindTFSTaskinContentString.scptd (AppleScript/Objective-C Library is checked on the Bundle Contents sidebar) which is:

use framework "Foundation"

on findTask:taskPattern inContent:theContent
     set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:taskPattern options:0 |error|:(missing value)
     set theRange to theRegEx's rangeOfFirstMatchInString:theContent options:1 range:{location:0, |length|:length of theContent}
     set contentString to current application's NSString's stringWithString:theContent
     set theTask to contentString's substringWithRange:theRange
     return theTask as text
end findTask:inContent:

(obviously this could/should be written more generically to make it obvious that it searches a string for a pattern match)

  • In ~/Library/Application Scripts/com.apple.mail/TFS_Messages.scpt I have

use theLib : script "FindTFSTaskinContentString"
use scripting additions

using terms from application "Mail"
	
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			set the message_count to the count of these_messages
			repeat with i from 1 to the message_count
				set this_message to item i of these_messages
				try
					set this_subject to (subject of this_message) as Unicode text
					set this_content to (content of this_message) as Unicode text
					set this_source to (source of this_message) as Unicode text
					set taskPattern to "#[0-9]+" as Unicode text
					set theContent to "Task #35912: Blah blah blah" as Unicode text
					set theTask to (theLib's findTask:taskPattern inContent:theContent)
					using terms from application "Evernote"
						tell application "Evernote"
							create note with text this_subject title theTask
						end tell
					end using terms from
				on error errStr number errorNumber
					display dialog errStr
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

When I trigger the script from Mail.app by ‘Apply Rules’ on a message, I get the error:
‘NSRegularExpression doesn’t understand the “regularExpressionWithPattern_options_error_” message.’

However, if I use the following


use theLib : script "FindTFSTaskinContentString"
use scripting additions

set taskPattern to "#[0-9]+"
set theContent to "Task #35912: Blah blah blah"
set theTask to (theLib's findTask:taskPattern inContent:theContent)
do shell script "echo " & quoted form of theTask

from the command line (as osascript testOsascript.scpt), I get the expected output (“#35912”).

It is a sort of sandbox problem. The error you’re getting is really an indication that your lib script is being interpreted as vanilla AS, not ASObjC. And I’m afraid that happens when an AppleScript’s target application is modified, which is how sandboxed apps run scripts. Basically, it’s a bug in AppleScript itself.

I wish I could offer a workaround, but I can’t think of anything obvious.

Thanks very much for taking a look Shane. I’ll look at using the more ‘traditional’ text item delimiters methods for parsing text.