Re: Extracting from text [Lecture]
Re: Extracting from text [Lecture]
- Subject: Re: Extracting from text [Lecture]
- From: Ric Phillips <email@hidden>
- Date: Fri, 01 Mar 2002 09:40:41 +1100
On 1/3/02 5:29 AM, "Nigel Garvey" <email@hidden>
wrote:
>
Apologies for this. :-)
>
Not necessary, I for one, would like to see more postings like this.
>
>
set AppleScript's text item delimiters to {":"}
>
set parentPath to (text items 1 thru -3 of folderPath) as string
>
>
This isn't perhaps as potentially calamitous as listing and then coercing
>
the individual characters of a large text, but its use does seem to
>
suggest that the authors don't know about the equivalent 'text'
>
construction, which is (in its simplest form):
>
>
text 1 thru text item -3 of folderPath
>
In keeping with the spirit of this thread I thought I'd offer the following
additional comments for new-comers who are trying out the code,
Lets say the value of the folderPath was,
"disk1:folder1:folder2:folder3:", the code,
set AppleScript's text item delimiters to {":"}
text 1 thru text item -3 of folderPath
Will return "disk1:folder1:folder2", which may have some folk wondering why
you have to use -3 to lop off what appears to be only one item.
First, the negative index (-n) is simply counting backwards from the end of
the implicit list of 'text items'. So -1 will refer to the last item the way
that 1 refers to the first item - (there are no zeros in applescript
indexes) Be careful not to mix up the concept of counting backwards with
that of subtraction.
Secondly it's worth noting that the reference,
Text item -1 of "folderPath"
Will return "", not "folder3".
To be clear what's happening lets say we have a string with the value of,
":a:b:c:",
And set the text item delimiters to ":" as in the code above. Now look at
the following 'text item' references and the values they return,
Text item 1 of ":a:b:c:" --> ""
Text item 2 of ":a:b:c:" --> "a"
Text item 3 of ":a:b:c:" --> "b"
Text item 4 of ":a:b:c:" --> "c"
Text item 5 of ":a:b:c:" --> ""
Text item -1 of ":a:b:c:" --> ""
Text item -2 of ":a:b:c:" --> "c"
Text item -3 of ":a:b:c:" --> "b"
Text item -4 of ":a:b:c:" --> "a"
Text item -5 of ":a:b:c:" --> ""
So think of it this way,
A) where you have a delimiter in a 'list' of text items, applescript
recognises a text item on both sides of that delimiter even if there appears
to be 'nothing' there. (That is it will return an empty string - "") So the
stirng ":a::b:" still has five text items in it. (The number of text items
is always 1 more than the number delimiters.)
B) the index values count forwards from the first item if they are positive,
and backwards from the last item if they are negative.
C) this means that for a string like "disk1:folder1:folder2:folder3:",
(where you have set the delimiters to ":"), text item -1 refers to the last
text item - which is "", text item -2 refers to the second last text item -
which is "folder3", and text item -3 refers to the third last text item -
which is "folder2"
So,
text 1 thru text item -3 of folderPath
Means get the text from the first text item to the third last text item,
(inclusive), which returns "disk1:folder1:folder2".
Note: In applescript you will often be dealing with paths to folders where a
trailing colon is included as in,
"disk1:folder1:folder2:folder3:"
On the other hand paths to files will not have this trailing colon, as in
"disk1:folder1:folder2:folder3:file1"
(and there may be occasions where you could end up with a path to a folder
with no trialing colon, ie: "disk1:folder1:folder2:folder3")
This path extraction business is most often used to get parent and
grandparent folders of a given folder or file. Be careful about assuming
that a generic reference to text item -3 of any path will always refer to
the 'parent' item.
Ric Phillips
Faculty Web Coordinator
Faculty of Humanities and Social Sciences
Latrobe University
Room HU3 324
Phone: 9479 2792
-----------------------------------------
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.