Applescript error 1708— regex to find text...

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'stext item delimiters

set AppleScript'stext item delimiters to "

- "

set aList to the text items in the theClipboard

set theTasks to (the count aList) - 1

set AppleScript'stext item delimiters to "# "

set aList to the text items in the theClipboard

set theProjects to (the count aList) - 1

set AppleScript'stext 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'stext item delimiters

set AppleScript'stext item delimiters to " "

set tagList to every text item of t

set AppleScript'stext 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'stext item delimiters

try

considering case

set AppleScript'stext item delimiters to oldString

set lst to every text item of theText

set AppleScript'stext item delimiters to newString

set theText to lst as string

end considering

set AppleScript'stext item delimiters to ASTID

return theText

on error eMsg number eNum

set AppleScript'stext item delimiters to ASTID

error "Can't replaceString: " & eMsg number eNum

end try

end replaceString

Posted on Oct 14, 2015 5:27 PM

Reply
7 replies

Oct 14, 2015 8:36 PM in response to Jacob Sam-La Rose

If could be a couple of different things, but what I'd try first is fairly simple. go to the line where you have the find text command, delete the words 'find text', retype them by hand (don't copy and past), and then recompile. This should force AppleScript to check in with the Satimage osax and rebuild the compiled command rather than reusing the precompiled version that's in the script already.


For what it's worth, I don't get that error when I run your script. I may be using a different version of the osax than you, though - mine's 3.5.0; I should probably update.

Oct 14, 2015 8:39 PM in response to Jacob Sam-La Rose

Well, I'll try downloading that version and see if I get the same error; it could be a problem in the osax itself. You might also try this: copy the script and paste it into a plain text editor (like TextWrangler), then recopy the plain text and paste it into a new script window. That should clear out all of the information that applescripts carry around with them behind the scenes, and force the entire script to recompile from scratch.The kind of error you're getting means that the script cannot find a reference for the command SATIFINd anywhere in its commands list. There are only three ways I know of that that could happen:

  1. the script has not loaded the Satimage osax (unlikely, unless the osax is corrupt or you're calling the command in a very weird way)
  2. the compiled version of the command in the script is not matching up with the proper command in the commands list (either because the command changed somehow in version upgrade of the osax, or the command got corrupted in the script)
  3. you are calling the osax command from within an application tell block (since 10.7 that has been blocked by the system; you have to use the my keyword to change context to the script level).

I don't see you doing 3 and 1 seems unlikely, so something odd with the script itself is the most reasonable explanation.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript error 1708— regex to find text...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.