• 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: zeroing in on a string inside quotes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: zeroing in on a string inside quotes


  • Subject: Re: zeroing in on a string inside quotes
  • From: "Serge Belleudy-d'Espinose" <email@hidden>
  • Date: Thu, 29 Nov 2001 01:57:48 +0100

At 18:29 -0500 28/11/01, email@hidden wrote:

>I've got a string in an AppleScript variable that I've parsed from a
>web-page. It looks like this:
>
> <input type=hidden id="UserId" name="UserId" value="swordfish">
>
>If I wan't to zero in on the word inside the quotes, after the "value=", and
>put it in a variable, how could I best do that in AppleScript? My Macromedia
>Director Lingo experience is getting in my way on this string-parsing logic.
>I'd use some combination of "strpos" and searching for quotes, right?

You can do it the 'traditional' way with offset:

--

set xString to "<input type=hidden id=\"UserId\" name=\"UserId\" value=\"swordfish\">"
set xSubString to ""

set xBeginning to offset of "value=\"" in xString

if xBeginning > 0 then
set xSubString to text (xBeginning + 7) thru end of xString
set xEnd to offset of "\"" in xSubString

if xEnd > 0 then
set xSubString to text 1 thru (xEnd - 1) of xSubString

end if

end if

return xSubString

--

but if you don't know about applescript's text item delimiters - tids - it's the time to learn.
tids are a powerful tool with strings; for example, they allow you to break a string into list items using tids as the separator.
In your example, you will break your initial string into list items, then check if the resulting list has more than one item, if it does, you will apply the same kind of check in the second item of the resulting list searching for the trailing ".
It can take some time to get used to them but they are really useful. Also, for repeating the same operation hundred of times, you will find that tids based methods are faster than offset based ones.
On a side note, when you use tids, it's a very good thing to save their initial value and restore it at the end of your script.

--

set xString to "<input type=hidden id=\"UserId\" name=\"UserId\" value=\"swordfish\">"
set xSubString to ""

set oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "value=\""

set xSubString to text items of xString

if (length of xSubString) > 1 then
set AppleScript's text item delimiters to "\""
set xSubString to text items of (item 2 of xSubString)

if (length of xSubString) > 1 then
set xSubString to item 1 of xSubString

end if

end if

set AppleScript's text item delimiters to oldTIDs

return xSubString


Serge
--
\\//\//\// Serge Belleudy-d'Espinose Institut Jacques Monod
// // // http://www.ijm.jussieu.fr/ Universite Paris VII - Jussieu
//\//\//\\


References: 
 >zeroing in on a string inside quotes (From: email@hidden)

  • Prev by Date: Re: FTP Uploads using URL Access Scripting (3rd Posting)
  • Next by Date: OS X Trash script
  • Previous by thread: zeroing in on a string inside quotes
  • Next by thread: Re: zeroing in on a string inside quotes
  • Index(es):
    • Date
    • Thread