Re: My Bad, Idle Handler question fixed & asked again
Re: My Bad, Idle Handler question fixed & asked again
- Subject: Re: My Bad, Idle Handler question fixed & asked again
- From: Mike Fischer <email@hidden>
- Date: Sat, 7 Dec 2002 01:32:54 +0100
Hi Brandon,
Am Freitag, 06.12.02 um 22:30 Uhr schrieb "Brandon Carpenter"
<email@hidden>:
Subject: My Bad, Idle Handler question fixed & asked again
To: email@hidden
From: "Brandon Carpenter" <email@hidden>
Date: Fri, 6 Dec 2002 08:20:09 -0600
I corrected the script (thanks to Ted Wood) and will ask the question
again. The idle handler again only runs once then the script stops.
Are you running this script in the Script Editor or as an applet?
IIRC in the Script Editor the idle mechanism will not work because the
script is terminated after the implicit or explicit run handler is
executed. Save the script as an applet and make sure to check the don't
terminate automatically or stay open checkbox* when saving.
* I'm on a German system, so I don't know what the exact name on an
English system is. On my machine it's called "Nicht automatisch
beenden". (X 10.2.2, AS 1.9) The ASLG calls it a "stay-open script
application".
The purpose of the script is to determine if a CD in the drive,
continually, and will then determine if it is a multi session cd.
property DiskStatus : false
on run
idle of me
You should not need to call idle yourself. You can but the point is
that it is called for you when the script application is in fact idle.
If you do call it yourself then I'm not sure if the return value will
actually set the new timeout. My guess would be that it would behave
like any other handler and that the idle timeout will not be set.
end run
on idle
set idleTime to 60
CheckDiskStatus()
try
if DiskStatus is true then
display dialog "there is a disk"
else if DiskStatus is false then
display dialog "No Disk Found"
end if
on error errMsg number errNum from errFrom partial result
errResult
to errTo
display dialog errMsg as text
end try
1) I'm not sure what you're trying to accomplish with your try block.
As it is it will catch potential errors from the very simple boolean
comparisons and from one of the first 2 display dialog statements but
not from CheckDiskStatus(). If display dialog can fail then the the
third display dialog in the on error block could as well and that will
not be caught.
2) Also are you expecting DiskStatus to have a value other than true or
false? If not you could simplify the if statement to:
...
if DiskStatus then -- If you are a purist you might also use: if
DiskStatus = true then
display dialog "there is a disk"
else
display dialog "No Disk Found"
end if
...
3) You don't need to specify unused parameters in on error: number,
from, partial result. If you are just using errMsg then this will
suffice:
on error errMsg
display dialog errMsg -- as text is unnessesary I think.
return idleTime
end idle
Putting all this together I would write your idle handler like this:
on idle
try -- The try block is probably unnessesary but I'll leave it in for
now.
my CheckDiskStatus() -- In my experience it's always safer to refer
to handlers
-- in the same script this way though it might not be
-- strictly nessesary in all cases.
if DiskStatus then
display dialog "there is a disk"
else
display dialog "No Disk Found"
end if
on error errMsg
display dialog errMsg
end try
return 60 -- You're not changing this so just use a constant instead
of a variable.
end idle
Of course I assume this is just testing code? I sure wouldn't want an
app to nag me with a dialog once a minute.
My notes above are all cosmetic. The basic logic is correct. To test
the idle mechanism try this minimal example in Script Editor and also
saved as an applet:
--------------------
on run
beep
end run
on idle
beep
return 5
end idle
--------------------
In Script Editor it should beep once (from the run handler). As an
stay-open applet it should beep once (run) and then every 5 seconds
(idle) until quit.
on CheckDiskStatus()
tell application "Finder"
-- Check for Disks
if (exists (some disk whose (ejectable is true))) then
set DiskStatus to true
else
--Check later for Disks
set idleTime to idleTime + 60
idleTime is a local variable which will not be seen inside on idle. If
you want to make it global then you need to declare it as:
global idleTime
at the beginning of every handler that wants to use it - in your case
on CheckDiskStatus() and on idle. Even if you do this your code will
reset its value to 60 before calling CheckDiskStatus() at the beginning
of your on idle handler so you only get either 60 or 120 as a value.
Don't know if that's what you wanted or not. If it is there are easier
ways to achieve this.
end if
end tell
end CheckDiskStatus
As for the actual functionality of CheckDiskStatus() it will probably
check for ejectable disks but not do what you described as its purpose.
It will not reset DiskStatus to false if there is no ejectable disk
though. I think your question was about the idle mechanism so I ignored
this.
HTH
Mike
--
Mike Fischer Softwareentwicklung, EDV-Beratung
Schulung, Vertrieb
_______________________________________________
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.