• 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: Parsing text from a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing text from a string


  • Subject: Re: Parsing text from a string
  • From: Adam Wuellner <email@hidden>
  • Date: Wed, 16 Mar 2005 12:31:43 -0600

> If I have the string "Have a [Nice] [Day]"
>
> I want what is in the first set of brackets "Nice".  The brackets will
> always be somewhere in the string and I want what is in side of them.

The language feature you are looking for is called 'text item delimiters'.

set text_to_parse to "Have a [Nice] [Day]"
set Applescript's text item delimiters to "["
set parsed_text to text item 2 of text_to_parse
set Applescript's text item delimiters to "]"
set parsed_text to text item 1 of parsed_text
--> "Nice"

Be aware that setting Applescript's text item delimiters has global
impact... you might wish to store the existing t.i.d.'s in a variable,
and restore them when you are done.  Someone on this list had a nice
way of doing it:

set {tids, Applescript's text item delimiters} to {Applescript's text
item delimiters, "["}
-- do your stuff
set Applescript's text item delimiters to tids

Another careful strategy, if less friendly to other
subroutines/scripts, is to simply _always_ set the tids when you wish
to use them - that way there is no question what the state of the tids
is.

The default tids are {""}.  A list may be given as the value, though
IIRC only the first item in the list is currently significant.  There
is speculation that a future version of Applescript will be able to do
something with all of the items in the list (presumably you could
shorten this example by providing the list {"[", "]"} as the tids).
But none of that currently is implemented.

HTH,
Adam
 _______________________________________________
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: 
 >Parsing text from a string (From: Todd Geist <email@hidden>)

  • Prev by Date: Re: Parsing text from a string
  • Next by Date: Re: Address Book Scripts
  • Previous by thread: Re: Parsing text from a string
  • Next by thread: Re: Parsing text from a string
  • Index(es):
    • Date
    • Thread