MACSCRPT Archives

January 2011

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Nigel Garvey <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Sun, 23 Jan 2011 08:36:43 +0000
Content-Type:
text/plain
Parts/Attachments:
text/plain (1 lines)
"Daniel A. Shockley" wrote on Sat, 22 Jan 2011 01:05:27 -0500:



>On Fri, 21 Jan 2011 16:42:16 +1100, Shane Stanley

><[log in to unmask]> wrote:



>>  set bareName to text 1 through text item -2 of theName

>>

>> That doesn't result in a list -- and it doesn't needlessly make a list and

>> then coerce it to a string.



>I see. My mistake - and very interesting. I don't think I've ever seen

>"text #" used in a "through" range with a "text item #" of a string.

>It didn't occur to me to mix and match that way - another area where

>AppleScript allows quite a bit of flexibility.



More flexibility than you're ever likely to be able to use in some

cases.  :)  Range references have had this flexibility since at least

when I started AppleScripting back in 1997. But so many people have

posted 'text items … as text' and 'characters … as text' code over the

years that you're still more likely to have heard of that construction

than the "proper" one, despite the efforts of many to point out that

coercing a list to text produces an result of class 'text' (which

mattered when 'text' and 'Unicode text' were separate classes) and is

affected by the value of AppleScript's text item delimiters.



The same sort of flexibility exists when extracting other elements from

text, eg.:



  set txt to "There was a young man from Strathclyde

  Who fell down a sewer and died.

  The next day his brother

  Fell into another

  And now they're interred side by side."

  

  words 1 thru paragraph 2 of txt

  --> {"There", "was", "a", "young", "man", "from", "Strathclyde",

"Who", "fell", "down", "a", "sewer", "and", "died"}

  paragraphs from word 10 to character -50 of txt

  -- Or: paragraphs (word 10) thru (character -50) of txt

  --> {"down a sewer and died.", "The next day his brother", "Fell in"}

  characters from word 10 to -100 of txt

  --> {"d", "o", "w", "n", " "}

  

Similarly with lists:

  

  set lst to {"cat", true, 17, {"a list"}, {a:"a record"}, "dog",

"aardvark", 27, false, false, 1000000, true, {"another list"}, -1,

"ergotism", {a:"another record"}}

  

  text 1 thru 3 of lst

  --> {"cat", "dog", "aardvark"}

  text from list 1 to integer 2 of lst

  --> {"dog", "aardvark"}

  booleans 3 thru record -1 of lst

  --> {false, true}





NG

ATOM RSS1 RSS2