I was sure someone had posted a trick to this, but I couldn’t remember what it was.
The only solution I can think of is using a stay open application with reopen handler. Something like this, but I haven’t got it working yet. Just got back.
property x : missing value
on run
set x to 0
end run
on reopen
set x to x + 1
end reopen
on idle
say x
quit
return 0
end idle
on quit
error
end quit
Which is not tricky at all. Don’t run the above script. Otherwise, you need to force quit it.
on run
global a
try
local probe
set probe to a
on error
set a to 0
end try
end run
on quit
global a
set a to undef()
end quit
to undef()
return
end undef
script Script1
beep 1
set my theScript to my Script2
run script my Script2
end script
script Script2
say 2
end script
property theScript : Script1
run script theScript
With help from Bill Cheeseman’s Idle thoughts article. I don’t know why it keeps running Script1.
I was mainly thinking about how to run the whole script application on the first run and part of it thereafter cutting out the first part like the if-then statement. Finally got something working:
property the_index : 1
on H1()
beep 1
set the_index to 2
my H2()
end H1
on H2()
say 2
end H2
property the_list : {H1, H2}
set the_handler to item the_index of the_list
the_handler()
The problem with this is the way properties are compiled. You have to have Script1 above the property declaration so that it exists to be the value compiled into the property. But then the property doesn’t exist at the first mention of ‘my theScript’, so the compiler makes that ‘theScript’ local to Script1. The command which resets the property theScript has to come after it.
script Script1
beep 1
end script
script Script2
say 2
end script
property theScript : Script1
run script theScript
set theScript to Script2
Or perhaps:
script Script1
beep 1
return my Script2
end script
script Script2
say 2
end script
property theScript : Script1
set theScript to (run script theScript)
(1) how to tell whether the script has been run for the very first time by this user, so that if the user runs it again next week, any “on first run” procedure will NOT be run
OR
(2) how to tell whether it has been run for the first time in this session (or some other interval)?
Possible answers will vary if you mean (1) or (2).
You don’t need the if statement per se, handlers or variables that return booleans can be run sequentially by using the operator and, drawback is that the handlers must return booleans as well.
property firstRun : true
firstRun and handler1() or handler2()
on handler1()
set firstRun to false
display dialog "handler 1"
return true
end handler1
on handler2()
display dialog "handler 2"
return true
end handler2
script Script1
beep 1
run script my Script2
return my Script2
end script
script Script2
say 2
return it
end script
property theScript : Script1
set theScript to (run script theScript)
It’s unfortunate that we have to keep setting theScipt to Script2.
Edited: yeah, cool if-then DJ.
Edited: also, this has the form of a toggler if Script2 returns Script1.
Edited: btw, for anyone who is starting out with stay open apps, here’s Bill Cheeseman’s article: http://macscripter.net/viewtopic.php?id=24568
Excellent article which I’ve been reading and reviewing for years.