Re: Paragraph parsing technique?
Re: Paragraph parsing technique?
- Subject: Re: Paragraph parsing technique?
- From: "Adam Bell" <email@hidden>
- Date: Fri, 28 Apr 2006 10:34:15 -0300
To drive this point home (because it has stung my waaay to often) all of these work without "contents" so you can get lulled into forgetting. Of course they all work with contents too.
set aList to {"A", "B", "C"}
repeat with x in aList
if x as string = "B" then beep
end repeat
set aList to {1, 2, 3}
repeat with x in aList
if x as integer is 2 then beep
end repeat
set aList to {"A", "B", "C"}
repeat with x in aList
if x's text is "B" then beep
end repeat
set aList to {1, 2, 3}
repeat with x in aList
if x contains 2 then beep
end repeat
On 4/28/06, Emmanuel <email@hidden> wrote:
At 7:47 AM -0400 4/28/06, David wrote:
>
>I'm new to AppleScript and was hoping to get some syntactical help here (at
>least I hope it's only a syntax issue!) :-)
David, you're hitting two well-known features of AppleScript.
First, the "whose blah blah is", "where it is" etc don't work with
AppleScript's native stuff (namely: lists.) It only works with an
application's objects - yet not all the time.
For instance, you can't expect to do "get every item of theList where
it contains "foo"" but you can expect (yet don't hold your breath) to
be able to do "tell app "Finder" to get every window whose name
contains "foo"".
So, as long as you don't use specific text-processing tools, your
loop approach is the good one - and it may run faster than you can
imagine: AppleScript is a rather fast language where your script
doesn't communicate with an application or a Scripting Addition.
Second tricky point: in "repeat with anItem in aList", you would
expect that "anItem" takes successively all the values contained in
the list, that's wrong, it contains successively a reference to those
values. Sometimes it makes no change, since AppleScript will evaluate
by itself the reference. Sometimes it makes a difference.
The best help to understand IMHO is to be aware that you can do:
------------------
repeat with anItem in aList
set contents of anItem to "hello" -- this will change the
contents in aList
etc
------------------
so, "anItem" is carrying a reference able to change the values in aList.
What is the fix? Very simple: do take the habit to add one line to
every loop you write which will explicitly dereference the loop
variable, that is:
------------------
repeat with theParagraph in (get the paragraphs of theDescription)
set theParagraph to contents of theParagraph -- always do
that unless you know why you don't
if theParagraph etc.
------------------
Finally, in order to be fully aware of the absurdity you can face if
you don't do as I say, run that:
------------------
set aList to {1, 2, 3}
repeat with x in aList
if x is 2 then beep
end repeat
------------------
------------------
set aList to {1, 2, 3}
repeat with x in aList
if (contents of x) is 2 then beep
end repeat
------------------
Emmanuel
>I would like to retrieve the paragraphs that contain a specific string from
>the description field of an Entourage 2004 contact.
>
>So far, I've tried this _expression_:
>
> set theDescription to description of contact "Aaron Patterson"
> set theParagraphs to (get every paragraph of theDescription where it
>contains ":ABPerson")
>
>To which Script Editor replies:
>
>"AppleScript Error. Can't get {"List","Of","Paragraphs","123:ABPerson"}
>whose it contains ":ABPerson"
>
>
>I can loop through the paragraphs and use a contains ":ABPerson" to pick out
>those that contain the string, but that seems inefficient.
>
> repeat with theParagraph in (get the paragraphs of theDescription)
> if theParagraph contains ":ABPerson" then
> set theABID to theParagraph
> end if
> end repeat
>
>To which Script Editor replies:
>
>item 4 of {"List", "Of", "Paragraphs", "123:ABPerson"}
>
>Can anyone offer a suggestion for how to filter a list of paragraphs
>containing a specific string?
>
>TIA!
>
>
> _______________________________________________
>Do not post admin requests to the list. They will be ignored.
>Applescript-users mailing list (
>
>This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden