Trying to figure out Error -1708

Hello, all. I’ve managed to cobble together a script that does pretty much what I want it to do in the Script Editor, but when I run it within an app I get an error message: “Error Number: -1708 " @[a-zA-Z]+[(]?[a-zA-Z0-9-]+” doesn’t understand the “«event SATIFINd»” message."

The script offers up some simple stats on an archive file from Folding Text or any other text-based task manager that uses @tags.

Seems the error comes from the fact that I’m using Satimage scripting additions. I’d like to know whether a) I’ve just written the script incorrectly (I’m learning AppleScript as I go, and this is a bit of a Frankenstein’s monster of code I’ve found and tried to understand as best as I could), or whether I just need to find some other way of generating my list of tags from source text (as opposed to the “find text” function that makes use of a regular expression).

Script below. Thanks in advance for any advice/pointers…


-- account for any tabs

on stripChar(str, chrs)
	tell AppleScript
		set oldTIDs to text item delimiters
		set text item delimiters to characters of chrs
		set TIs to text items of str
		set text item delimiters to ""
		set str to TIs as string
		set text item delimiters to oldTIDs
	end tell
	return str
end stripChar

-- this counts tasks and projects from the clipboard, but we can switch to front most document for ease... 

set theClipboard to the clipboard
set theClipboard to stripChar(theClipboard, tab)

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
- "
set aList to the text items in the theClipboard
set theTasks to (the count aList) - 1
set AppleScript's text item delimiters to "# "
set aList to the text items in the theClipboard
set theProjects to (the count aList) - 1
set AppleScript's text item delimiters to tid

set arText to the text of theClipboard
set tagList to find text " @[a-zA-Z]+[(]?[a-zA-Z0-9-]+" in arText with regexp, all occurrences and string result
set t to the text of tagList
set olddelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set tagList to every text item of t
set AppleScript's text item delimiters to olddelimiter
set nTags to (the count tagList) - 1
set theList to {}

repeat with theWord in (every text item in tagList)
	copy theWord & ": " & countMatches(tagList, (contents of theWord)) as string to end of theList
end repeat

-- log "Number of tags: " & (count text items in tagList) as string

set tagOccur to removeduplicates(theList)
considering numeric strings
	set tagOccur to simple_sort(tagOccur)
end considering
set tagOccur to replaceString(tagOccur, " @", "
")

return "Tasks: " & theTasks & "; Projects: " & theProjects & "
" & tagOccur & " 
---- "

-- FUNCTIONS AND SUBROUTINES

on countMatches(lst, val)
	local lst, val, counter
	try
		if lst's class is not list then error "not a list." number -1704
		script k
			property l : lst
		end script
		set len to count k's l
		set counter to 0
		repeat with i from 1 to len
			if k's l's item i is val then set counter to counter + 1
		end repeat
		return counter
	on error eMsg number eNum
		error "Can't countMatches: " & eMsg number eNum
	end try
end countMatches

on removeduplicates(lst)
	local lst, itemRef, res, itm
	try
		if lst's class is not list then error "not a list." number -1704
		script k
			property l : lst
			property res : {}
		end script
		repeat with itemRef in k's l
			set itm to itemRef's contents
			-- note: minor speed optimisation when removing duplicates 
			-- from ordered lists: assemble new list in reverse so 
			-- 'contains' operator checks most recent item first
			if k's res does not contain {itm} then ¬
				set k's res's beginning to itm
		end repeat
		return k's res's reverse
	on error eMsg number eNum
		error "Can't removeDuplicates: " & eMsg number eNum
	end try
end removeduplicates

on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort

on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

Model: MacBook Air
Browser: Safari 601.2.7
Operating System: Mac OS X (10.10)

Could you expand on what you mean by that? What app, and how?

Right. So I’m using this with FoldingText archives. I generally tend to copy my FoldingText archives out into nvAlt, so I’ve set this up to run on text in the clipboard. My thinking is that I’ll copy the archive over into a new nvAlt window, invoke the script and have my stats appear in the same window above the pasted text (or wherever the cursor is).

I’ve tried to invoke the text with a TextExpander shortcut (won’t compile), and via FastScripts. I can’t quite figure out why I can get the script to run as expected in Script Editor, but not elsewhere. I’m now wondering if it’s something to do with the way I’m referencing the clipboard…

I’m not quite following: the script has a return line, and that’s not going to do anything when run from an app.

One step at a time: when you save it as a compiled script and run it from FastScripts, what happens?

Noted. Queue potentially embarrassing error number 1!

When I try to run it with FastScripts, that’s when I get the error “Error Number: -1708 " @[a-zA-Z]+[(]?[a-zA-Z0-9-]+” doesn’t understand the “«event SATIFINd»”…

I’m assuming it’s the same Mac and same account. Try changing:

set theClipboard to the clipboard

to:

set theClipboard to the clipboard as text

And change:

set arText to the text of theClipboard
set tagList to find text " @[a-zA-Z]+[(]?[a-zA-Z0-9-]+" in arText with regexp, all occurrences and string result

to:

set tagList to find text " @[a-zA-Z]+[(]?[a-zA-Z0-9-]+" in theClipboard with regexp, all occurrences and string result

Now this bit:

set t to the text of tagList

What’s that trying to do? As far as I can see it does nothing here, but set to a copy of tagList. And if t is a list, I’m not sure what “every text item of t” a couple of lines later is going to achieve.

Edits done. Thanks. And you’re right”

set t to the text of tagList

…is pretty much redundant. I’ve excised that and the “every text item of t” that comes later. I’ve tried a few different ways of making things happen in this script” I think that was a legacy of an earlier version!

Got it sorted. Rebooted. Turns out I hadn’t restarted my Mac since I installed the OSAX. Seems to have done the trick. Still keen to tidy this script up and make it more efficient, but thanks for casting an eye over it thus far. It’s appreciated.