First run script

Hi,

How can you write this better:

property first_run : true

if first_run then
	set first_run to false
	set x to 0
end if
set x to x + 1
say x

After the first run, it still checks if first_run is true or false. How do you rewrite this so it doesn’t check after the first run?

Thanks,
kel

I think I have the answer.

Would be nice if you share the answer with us as well.

In reply to your first post: If you want a counter that will be set at 0 at the first run an property will do

property NrOfRuns : 0

--somewhere in your code
set NrOfRuns to NrOfRuns + 1

When you recompile the code the counter will be set to 0

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.

I’ll be working on it.

Thanks,
kel

Here’s a not so tricky stay open application solution. :slight_smile:

property x : missing value

on run
	set x to 0
	reopen
end run

on reopen
	set x to x + 1
	say x
end reopen

Think I have it. You make 2 scripts and a property as the switch.

Hello.

Maybe this can help you?


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

Hi McUsr,

Thanks, I’ll try your script.

What I was working on is something like this:

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.

Need some sleep maybe.

Later,
kel

For what purpose? Normally a first run check is used to execute things only once.


property first_run : true

if first_run then
	doThingsOnlyTheFirstTime()
	set first_run to false
end if


One extra line of code doesn’t slow down the script and is as expensive as for example calling the reopen handler

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()

Thanks,
kel

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)

Are you asking

(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

That’s a fun way to one-line an ‘if . then . else’ statement! :lol::cool:

I think this almost does it:

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. :smiley:

Edited: yeah, cool if-then DJ. :slight_smile:

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.

Thanks a lot,
kel