Re: Newbie Progress Bar
Re: Newbie Progress Bar
- Subject: Re: Newbie Progress Bar
- From: Ted Wood <email@hidden>
- Date: Sun, 17 Dec 2000 11:15:02 -0800
on 12/17/2000 1:56 AM, Bruce Robertson at email@hidden wrote:
>JollyRoger wrote:
>> A better way is to avoid using a tell block to encompass
non-Finder commands
>> in the first place!
> How do you intend that to work? The general case here is
>
> tell someApplication
> do your native stuff
> get a result
> do something with the result using terms not native to the application
> do some more native stuff with this result
> end tell
>
> So you are suggesting?
>
> tell someApplication
> do your native stuff
> get a result
> end tell
>
> do something with the result using terms not native to the application
>
> tell someApplication
> do some more native stuff with this result
> end tell
>
> Thus performing many more tell statements
It's not about how many tell statements you use. Your main goal should be
to write a script that works correctly, not to write a script with as little
tell statements as possible.
What I am talking about is not sending the wrong commands to the wrong
applications. Applescript sends commands where you tell it. When you send
commands to the wrong applications, those applications may generate an
error, or may even decide to attempt to handle the command anyway, even
though the command was never intended to be performed by that application.
This is why he was having problem. He sent the command to the Finder, and
the Finder was generating an error.
The solution to this problem is really not that complicated. You can save
yourself lot of headache in the future if you just make it a practice not to
send commands to applications that don't handle them directly.
For example, this:
on AppendSuffix(thefolder, MySuffix)
tell application "Finder"
set myFolderContents to every file of thefolder
repeat with x in myFolderContents
if name of x does not end with MySuffix then
try
set name of x to (name of x & MySuffix)
UpdateGauge()
end try
end if
end repeat
end tell
end AppendSuffix
Becomes this:
on AppendSuffix(thefolder, MySuffix)
tell application "Finder" to set myFolderContents to every file of
thefolder
repeat with x in myFolderContents
tell application "Finder" to set fname to name of x
if fname does not end with MySuffix then
try
tell application "Finder" to set name of x to (name of x &
MySuffix)
UpdateGauge()
end try
end if
end repeat
end AppendSuffix
The latter snippet only sends commands to the Finder that belong to the
Finder. Everything else is handled directly by the script - as it should
be.
JR
Although JR's solution will work fine, here's a simpler alternative
using the following example:
on AppendSuffix(thefolder, MySuffix)
tell application "Finder"
set myFolderContents to every file of thefolder
repeat with x in myFolderContents
if name of x does not end with MySuffix then
try
set name of x to (name of x & MySuffix)
UpdateGauge()
end try
end if
end repeat
end tell
end AppendSuffix
... becomes....
on AppendSuffix(thefolder, MySuffix)
tell application "Finder"
set myFolderContents to every file of thefolder
repeat with x in myFolderContents
if name of x does not end with MySuffix then
try
set name of x to (name of x & MySuffix)
tell me to UpdateGauge() -- <<<< --- note the
added tell statment here
end try
end if
end repeat
end tell
end AppendSuffix
When you add "tell me to", the command is now sent directly to the
current application "Script Editor" or the script, if running as an
applet, rather than to the object of the surrounding tell block.
Note you can change the 'current application' by adding the following
property to the top of your script or any script object:
property parent : application "some application"
.... or ...
property parent : "some other script object"
Cheers.
Ted.