I’m new to Applescript and I’m struggling. I want to parse out various values from a basic TextEdit document for use in Filemaker and elsewhere. Here’s an example of a Text document I’m working with:
My method to get data has been the following:
tell application “TextEdit”
set vel_response to word 8 of front document
end tell
This works for the most part - but when I try to get word 22 (the first ‘ATK1’ value), I hit the wall. I get repeated error messages when i try to compile and continual messages that my variable is not defined. It seems Applescript will not recognize any words after Word 21 in the above.
Can anyone explain what’s going on and suggest a workaround?
Thanks very much.
Jeff
Model: G5
AppleScript: 2.1.1
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)
Thanks very much for the response, Kel. Could you help me understand why Applescript considers this document to have only 21 words. What’s wrong with the items after “ATK1” (which it the 21st word in the document)?
I don’t know exactly why it does this. If you go strictly by TextEdit’s dictionary, all this shouldn’t work in the first place as the document object has no elements. The document object has the one property ‘text’. Using the exact terminology in the dictionary, you could do this:
tell application "TextEdit"
set t to text of front document
end tell
set w_list to words of t -- AppleScript
Note that by getting the text of the front document, you can no longer use the filter reference form on the text.
I’m afraid there’s a bit of a bug in TextEdit, Jeff.
You could try something like:
tell application "TextEdit" to word 22 of (get text of document 1)
--> "0"
Or:
set t to text of application "TextEdit"'s document 1
word 22 of t
--> "0"
For a routine that gets values without the need to count words, try this (which also converts numerical-type text values into numbers):
to get_value of v from t
set d to text item delimiters
set text item delimiters to v & ", "
if (count t's text items) is 1 then
set text item delimiters to d
return beep
end if
set v to words of paragraph 1 of t's text item 2
set text item delimiters to d
try
repeat with i in v
set i's contents to i as number
end repeat
end try
if (count v) is 1 then return v's item 1
v
end get_value
tell application "TextEdit" to set t to text of document 1
get_value of "ATK1" from t
--> {0, 0}