properties reset

Hi
How to reset a changed property in a stay - open application ?
Here a snippet of my script:

property start_sess :0

--routine
my quitme()
--routine end

on quitme()
	set start_sess to 0
	ignoring application responses
		quit
	end ignoring
end quit

Hi,

ignoring application responses is useless anyway while sending Apple Events to self.
I would use the on quit handler to clean up things


property start_sess : 0

--routine
quit
--routine end

on quit
	set start_sess to 0
	continue quit
end quit


Hi Stefan,

quit-handler usually block my stay-open applications to close-i don’t know why. I put the ‘quit’ command only once, obviously.

I think that when an idling stay-open script tells itself to quit, it doesn’t finally let go until the next time it’s polled by the system; so the longer the idle time, the longer it is before the script actually quits. Putting ‘return 1’ immediately between the ‘quit’ command and the end of the ‘idle’ handler can reduce the waiting time:


property start_sess : 0

on idle
	-- Blah blah.
	
	if (bored) then
		quit
		return 1 -- Short idle time after 'quit' command.
	else
		return 5 * minutes -- Normal idle time.
	end if
end idle

on quit
	set start_sess to 0
	continue quit
end quit

Or if you wanted to be a smart-arse: :wink:


property start_sess : 0

on idle
	-- Blah blah.
	
	if (bored) then return quit -- Short idle time returned by the 'quit' handler.
	
	return 5 * minutes -- Normal idle time.
end idle

on quit
	set start_sess to 0
	continue quit
	return 1
end quit

Hello!

I thought I just should mention it here, unsolicitied, and not even necessarily totally on spot, that the problem with stay open applets and quit handlers may be more complex, somtimes, the run handler will be executing during the quit handler, if an error is raised.

It may actually be more complex than that depending on how the quit is activated, I have used the code from the Applescript the Definitive Guide second editon By Matt Neuburg p. 432-434 (should be available on Google Books) … to both study and solve the problem. It is a nice piece to look at to understand how things can be messed up between the different handlers in an stay open app.

Yes. It’s something which needs to be studied and treated carefully.

With regard to the “reset property on quit” problem, if the property’s to be reset to a definite value every time the script quits, it would be better to do it when the script starts instead, in the ‘run’ handler. There’s no need then even for a property: a global would do.

If the property’s only to be reset when the script’s quit under particular circumstances within the ‘idle’ code, the sequence should be:

Reset the property
Issue the ‘quit’ command
Either issue error number -128 (to stop the script) or return 1 (for the shortest possible delay) from the ‘idle’ handler.

Hello!

I’d settle for using a global when you are going to reset the property anyway! :wink:

This code below, by Berkowitz illustrates the trickery


global shouldQuit
global didCleanup
on run
	set shouldQuit to false
	set didCleanup to false
	try
		-- lengthy operation goes here
		repeat with x from 1 to 10
			if shouldQuit then error
			say (x as string)
			delay 5
		end repeat
	on error
		tell me to quit
	end try
end run
on quit
	if not didCleanup then
		-- cleanup operation goes here
		say "cleaning up"
		set didCleanup to true
	end if
	set shouldQuit to true
	continue quit
end quit

And when you mix an Idle handler as well into this then I think you have to program the idle handler to take the same precautions as the run handler in this example And I do own the book (with regards to the display of the code.