Re: TextEdit and Text Item Delimiters
Re: TextEdit and Text Item Delimiters
- Subject: Re: TextEdit and Text Item Delimiters
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 31 Jul 2002 15:06:01 -0700
On 7/31/02 1:35 PM, "email@hidden" <email@hidden> wrote:
>
Another TextEdit question (pity the newbie):
>
>
Does TextEdit not understand Text Item Delimiters? Given a TextEdit
>
document containing the following:
>
foo=bar
>
sna=foo
>
bar=pub
>
kung=foo
>
I try:
>
tell app "TextEdit"
>
set AppleScript's text item delimiters to {"="}
>
set keyParagraph to (first paragraph whose first text item is
>
"foo") of document 1
>
end tell
>
>
and AppleScript pops up with a NSCannotCreateScriptCommandError.
>
>
So can TextEdit just not do this? What's a work-around that I could use?
TextEdit's AppleScript, like all the new OS X Apple apps,. is quite
primitive in OS 10.1, and seems to have been implemented by people who had
no idea what AppleScript is for. However, you can glean a few rough pebbles
from it.
First of all, everything must work with 'text of document 1'. 'text' is
actually a property, not an element (!) of documents. The Dictionary is
screwed up.
Now 'paragraph' is an element of 'text' in TextEdit's own TextEdit Suite,
and whose filters do work. The Dictionary here correctly says 'satisfying a
test'. But 'text items', after applying text item delimiters, are _not_
TextEdit properties or elements. You get text items as part of an
AppleScript list. 'whose' filters notoriously do _not_ work with AppleScript
lists. (That is probably the single most demanded feature request for
AppleScript. Will it ever be fulfilled?)
So, what will work is:
tell application "TextEdit"
set keyParagraph to first paragraph of text of document 1 where it
starts with "foo"
end tell
--> "foo=bar"
(This is assuming that all your paragraphs actually start with their first
word and not with a tab or spaces as you put into this email. If they start
with blank space then no paragraph has "foo" as its first text item.)
Otherwise you have to use a repeat loop, if you really to need those text
items:
tell application "TextEdit"
set allPars to every paragraph of text of document 1
end tell
set AppleScript's text item delimiters to {"="}
repeat with i from 1 to (count allPars)
set aLine to item i of allPars
if text item 1 of aLine = "foo" then
set keyParagraph to aLine
exit repeat
end if
end repeat
set AppleScript's text item delimiters to {""}
keyParagraph
--> "foo=bar"
-------------
You need to do that 'item i' line outside the TextEdit tell block, or else
it gets compiled as 'Abstract object'.
Or you can do it this way:
tell application "TextEdit"
set allPars to every paragraph of text of document 1
set AppleScript's text item delimiters to {"="}
repeat with i from 1 to (count allPars)
set aLine to paragraph i of text of document 1
if text item 1 of aLine = "foo" then
set keyParagraph to aLine
exit repeat
end if
end repeat
set AppleScript's text item delimiters to {""}
end tell
keyParagraph
--> "foo=bar"
-------------
or this, which is more efficient:
tell application "TextEdit"
set theText to text of document 1
set allPars to every paragraph of theText
set AppleScript's text item delimiters to {"="}
repeat with i from 1 to (count allPars)
set aLine to paragraph i of theText
if text item 1 of aLine = "foo" then
set keyParagraph to aLine
exit repeat
end if
end repeat
set AppleScript's text item delimiters to {""}
end tell
keyParagraph
--> "foo=bar"
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.