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)