• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Paragraph parsing technique?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Paragraph parsing technique?


  • Subject: Re: Paragraph parsing technique?
  • From: Emmanuel <email@hidden>
  • Date: Fri, 28 Apr 2006 14:15:53 +0200

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 (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ 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
References: 
 >Paragraph parsing technique? (From: David <email@hidden>)

  • Prev by Date: Paragraph parsing technique?
  • Next by Date: Re: ASR Error
  • Previous by thread: Paragraph parsing technique?
  • Next by thread: Re: Paragraph parsing technique?
  • Index(es):
    • Date
    • Thread