Main.scpt problem

I have few scripts which for some reason modify its own main.scpt file when quitting.

This causes problems with my backups. If my another AppleScript launches this problematic AppleScript (tell app x to run/launch/open), then it is launched from backup folder (which is not Time Machine folder but backup i have made manually).

I compared 2 main.scpt files using FileMerge and it shows that in end of file is string which this script saves to disk by "do shell script “echo…”. This don’t happen in all scripts which uses echo.

How i can prevent that main.scpt is not modified?

It’s not clear to me what you mean but when you’re using persistent variables your script will be changed continuously. If that is the case you should use local variables and store persistent data in preference files.

Thanks.

My scripts first row is “global…”. It don’t have any rows like this “property something : 0”.

Are persistent variables same as property?

Hello.

If you have no run handler in a script (an implicit run handler), or a run handler, with any undeclared variables, those variables are regarded as global, (implicit globals) and will be saved with your script, in addition to any declared properties.

If you don’t need, nor want this persistency, then you should declare them as local.

In addition, for extra measure you can declare a property parent like this:


property parent : AppleScript

This will see too that no context of your script is saved when it quits.

Unfortunately i don’t understand most of your geek talk. :wink:

I don’t have any fancy variables, just normal global row and normal “set xxx to z” type of variables.

I added property parent : AppleScript, but it didn’t help. Did i put in wrong place?

property parent : AppleScript
global my_variables_here

on run
   some stuff here
   my_stuff
end run

on my_stuff()
   set something to x
end my_stuff

on idle
end idle

on quit
end quit

You are on the right track, but your globals, will be saved with your script, so they can start up again with the last value you set for them.

if you quit by your quit handler, and has a handler like this:


to undef()
    return
end undef

Then you can assign the value of undef to your globals in your quit handler, if you really want to not save anything with your script, but needs the globals to communicate between your handlers.


set myvar to "implicit global"
say "I am an implicit run handler"


global myvar
set myvar to "Explicit global"
on run
   set thisvar to " I am an implicit global"
    say "I am an explicit run handler"
end run

on otherHandler()
 set thevar to "I am not an implicit global, I am just declared here."
end otherHandler

This should also be of interest, another approach: MacScripter / Faster applets, that doesn’t store properties not stored explicitly

Thanks again. I tried your example code and read thru link, but all that stuff is too complicated to me.

I need in almost all of my scripts variables, which can communicate between handlers. Like this:

on run
	set my_var to 0
	my_handler()
end run

on my_handler()
	say my_var -->0
	set my_var to my_var + 1
end my_handler

on idle
end idle

on quit
	say my_var -->1
end quit

I never need variables, which saves value between launches.

Hello.

Do like below in your quit handler, for every gloabal you use, the the quit handler should be used at all times.

tell me to quit

I think the run handler will allways run before the idle handler, so you are good if you initialize your globals there.


on quit
	say my_var -->1
	set my_var to undef()
	continue quit
end quit

on undef()
	return
end undef