Re: Suggestions for speeding up code
Re: Suggestions for speeding up code
- Subject: Re: Suggestions for speeding up code
- From: Kai <email@hidden>
- Date: Sat, 01 Mar 2003 01:03:47 +0000
on Fri, 28 Feb 2003 08:23:28 -0500, James Burns <email@hidden>
wrote:
>
I wonder why doesn't my mind work this way? I guess I'm good at other
>
things (like Graphic Design).
Like everything else, James - it comes with practice. Hang in there! ;-)
>
Things I found particularly useful/helpful:
>
1/ The method of extracting the info - neat and elegant
That's the benefit of using tids - they're faster and more powerful than one
might initially imagine.
>
2/ The mention that you can loop through a container by "paragraphs."
>
- do you mean that the tids can be set to "paragraph" the same way it
>
can be set to "space" or "return", or what? I didn't run across that
>
word in any of the AppleScript docs.
No, you wouldn't have - not in connection with tids, anyway.
The term 'paragraph' is a text element of strings - like 'character' and
'word'. (Check out the ASLG: Chapter 3: Common Value Class Definitions:
Values and Constants: String [starts page 80].)
If you want to identify a string's paragraphs, words or characters, you
don't need tids:
===========================
set txt to "keyword1 value1
keyword2 value2
keyword3 value3"
txt's paragraphs
--> {"keyword1 value1", "keyword2 value2", "keyword3 value3"}
txt's paragraph 2
--> "keyword2 value2"
txt's paragraph 3's word 1
--> "keyword3"
txt's paragraph 1's word -1
--> "value1"
txt's paragraph 2's word 2's character -1
--> "2"
===========================
So if the keyword/value you want is always in the same paragraph number of
some text, you could access it directly - without any tids or repeat loops.
>
3/ The use of "run script." Do you mean it evaluates a variable? Can I
>
have test="Bite me!" and use "do script test" to return Bite me? I'll
>
have to read up on it...
Not exactly. The easiest way to evaluate a variable is to simply refer to it
(or return it from a script/handler). If the variable is a reference, you
can evaluate it by using 'contents of':
======================
set test to "Bite me!"
set testRef to a reference to test
test --> "Bite me!"
testRef --> test of <<script>>
testRef's contents --> "Bite me!"
======================
The Standard Additions 'run script' command runs a specified script (either
a reference to a script or a string). Obviously the contents of such a
string must translate into a valid script:
======================
run script "beep"
======================
Since a string can be built within a script, so can a script string:
======================
"beep" & return & "display dialog "
result & "\"Hello World!\" buttons \"Goodbye\" "
result & "default button 1 with icon 1"
run script result
======================
Anyway, here's the point I was really trying to make: Since a script can
return a value, then so can any string that will translate into a valid
script. The 'run script' command is simply the means by which a string can
be evaluated as a script.
In the example below, the variable 'txt' is set to a string - in which each
paragraph is a potential value class. To demonstrate that strings can be
evaluated as valid AS classes, the script loops through each paragraph,
evaluates it (using 'run script') and adds the class of the resulting value
to 'classList'.
Before running the script, you'll need to amend the last paragraph of 'txt'
- so that it refers to a valid alias:
======================
set txt to "\"this is a string\"
\"x\" as Unicode text
3
2.4
{1, 2, 3}
{a:1, b:2, c:3}
true
current date
paragraph
alias \"Macintosh HD:James Burns:text file\""
set classList to {}
repeat with p in txt's paragraphs
set classList's end to (run script p)'s class
end repeat
classList
--> {string, Unicode text, integer, real,
--> list, record, boolean, date, class, alias}
======================
So, while each paragraph started out as a string, it has now been evaluated
as a value class.
>
By the way... I set the variable oldDelims at the start of my (larger)
>
script.
I wondered if you might have done that (just making sure :-).
(BTW, I usually try to restore tids as soon as possible after I'm done using
them. Just helps to avoid confusion.)
>
Thanks for all of your help. When I release my (free) utility, I'll be
>
sure to mention all of your help.
I'm just pleased to know I might have helped in some way... :-)
--
Kai
_______________________________________________
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.