Idle handler and constant values

Hello, I wanted to ask what the “good” way to delay would be! I’m thinking if the delay is to long, a simple < delay xxxx > wouldn’t be good for the mac. I know there is a delay handler, searched bbs and didn’t find anything very quickly. Basically so far, in iTunes I’m delaying the “duration of user playlist the_playlist” and then shutting down the computer, but there should be a better way. The other thing one of my script might do is wait until it’s xx O’Clock… so I would need to check time and compare with a saved variable every now and then… 10 seconds?

Anyway, my other question was: could I define something constant with AS ?? There is a reserved word “constant” but I haven’t figured out how to use it properly. Maybe some “constant property : xxxxx” (doesn’t work, but similar I mean)

Thanks a lot

Hi,

In OSX AppleScript, you cannot set a universal constant that I know of. The following script is like what you describe for iTunes:

global is_playing, next_midnight

on run
set cur_date to (current date)
set date_string to (date string of cur_date)
set next_midnight to (date date_string) + 1 * days
tell application “iTunes”
set pl_ref to (first playlist whose name is “SleepTime”)
play pl_ref
end tell
set is_playing to true
end run

on idle
set cur_date to (current date)
tell application “iTunes”
if player state is not playing or cur_date > next_midnight then
set is_playing to false
quit – iTunes
end if
end tell
if not is_playing then quit – this script
return 2
end idle

(*
on quit
tell application “System Events” to shut down
continue quit
end quit
*)

The run handler is used to initialize stuff. After the statements of the run handler it jumps to the idle handler. This idle handler gives up its cpu time for 2 seconds and returns running its statements. If next midnight happens or the playlist “SleepTime” stops, then iTunes quits and the script quits. If you uncomment the quit handler the computer will shut down. it’s commented for obvious reasons.

gl,

A “constant” is a thing with a predefined and “unmodifiable” value. Eg:

set picube to pi ^ 3

Some times you can modify some constants, but is it useful?

set oldPi to my pi
set my pi to 5
pi ^ 2 --> 25.0
set my pi to oldPi

--> store some values for later use
set prevSpace to my space
set prevTab to my tab
set prevReturn to my return
set prevTID to AppleScript's text item delimiters

--> modify these constants
set my space to "KAPULLO "
set my tab to "KAPULLO "
set my return to "KAPULLO "
set AppleScript's text item delimiters to "KAPULLO"

set x to {"eres un " & space & tab & return, ""} as text

--> reset constants
set my space to prevSpace
set my tab to prevTab
set my return to prevReturn
set AppleScript's text item delimiters to prevTID

x --> --> "eres un KAPULLO KAPULLO KAPULLO KAPULLO"

More constants are true/false, October, anything or italic.
So, you can’t create a constant, but use existing ones. The more similar concept could be a “property” or “global”: a variable which holds a value available to the entire script object.
Some time ago there was a scripting addition called “Akua Sweets” which could create something similar, called “universal”, which were the same than properties but available to all the OS – any AppleScript. Installed in memory and accessible and modifiable through Akua Sweets. Actually, you can emulate such thing writing a value to a text file in a known place and reading it later when needed…
When you say “if the delay is to long, a simple < delay xxxx > wouldn’t be good for the mac”… Are you meaning AS’s “delay” command is very cpu-intensive? Some folks here use a shell script, which seems less intensive…

do shell script "sleep 5"