Persistant Properties (the trick)

Hello to All :slight_smile:

Seeking to finalize a personal AS-S project, I tried to understand why the variables of the type “property” do not work with AS-S and I believe to have found an easy way (the trick), very simple of use, but not without risks, to return the faculty of persistence to the variables of the type “property” in the AS-S applications.

The trick consists, quite simply, to saving, the totality of the code charged in memory, on its original script file, by crushing it, but by preserving the whole of the values of the global variables used.

Here the code to be used, in a simple “will quit” event handler connected to the application:


on will quit theObject
	set thePathToOriginalScriptFile to POSIX file (path for script "theNameOfOriginalScriptFile" extension "scpt")
	store script me in thePathToOriginalScriptFile replacing yes
end will quit

Attention !!!

If you work with significant projects, it is not guaranteed that the command “store script” functions in a sure way in any circumstance, and the least problem during the execution of this command, can thus make your application completely unusable to following launching… prudence!

In any case, for my part, I decline any responsibility for the use which you will make of this trick.

If you wish in knowing more about this trick, I wrote a long message in a French forum, that I could never translate into English, too difficult for me.

Thank you :slight_smile:

property properyName : "property value"

Works for me in AppleScript Studio.

What’s the issue? Something different? Surely there’s a better way to solve it than this method.

If you’re trying to save preferences/settings/whatever through log in/log out, read these:

http://macscripter.net/unscripted/unscripted.php?id=80_0_1_0_C
http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/sr3_app_suite/chapter_3_section_19.html

See the “Archive Maker” example included with the Dev Tools, as well.

Hi :slight_smile:

What, do you try to say to me that your varaible property propertyName preserves the last value allotted to the following launching of your ASStudio application??? :o

If it is the case, I am curious to know how made to you that? :confused:

In any case, at home, your example does not work with XCode 1.5. :frowning:

Thank you for other information, for my part, I often use the “user defaults” mechanism, that I find practical and powerful… :slight_smile:

You post was a bit ambiguous as to what you wanted or expected the outcome to be, and I made a (correct) guess in my post as to what you expected property to do.

“Variables of the type ‘property’ do not work.” This is incorrect. They work correctly, but the result you require is not the result of the function of AppleScript’s “property” feature.

Property is persistent, but only until you log out. A session variable, for all intents and purposes; an environment variable expiring when the user’s session terminates.

You should use the user defaults access (or, failing that, /usr/bin/defaults) for storing permanent settings. Clean and portable.

Ho… ok ok ok… really afflicted, thousand excuses Mr Mikey-San!

Ok, but then, can you explain for which reason please?

Ok, but then, why the variables of environment in a simple AppleScript script also do not expire at the time to leave script?
Would you have an explanation to this “odd” phenomenon?

What I note, Mr Mikey-San, which you évertuez yourselves to show the lack of exactitude and ambiguities of my remarks, without holding account one second that these “inaccuracies” are of advantage which had with a lack of mastery of the English language, that with a érronée or ignorant approach of the treated question.

Will know, Mr Mikey-San, that I spent more than one hour to be tried to write my first message, by using the assistance of the translators on line, who, although they are very practical, they are not famous for their exactitude, and, unfortunately, my low English level prevents me from seeing these “approximations”.

But, Mr Mikey-San, before pointing the errors of my message, did you take the trouble, would be this only one moment, to go to look at and read the long message which I wrote on the question in the French-speaking forum of which I entrusted the address?

Did you agree to make the least effort to read a text in a language who is not yours, as I endeavour to do it to each time I come here?

Then, before announcing, with as much perfectionism and sufficiency, my errors and approximations, I invite you, courteously, to make you also an effort for reading and understanding what I said in connection with variables “property” in a ASStudio application in the evoked French-speaking forum…

If, with the reading of these messages, you still find that my remarks are completely incorrect, then, Mr Mikey-San, I you will be liking to agree to bring explanations to us and specified able to inform us, with all the amateurs like me, who do not ask better than to learn, on the mechanisms of operation and the characteristics of these variables “property”…

Lastly, will know, Mr Mikey-San, whom I took part a long time in this forum in the past, but, unfortunately, for various reasons, I had to reduce my interventions, but I always kept an eye on certain exchanges here, quite simply because I appreciate much the talent and the knowledge to make various participants, who always reserved a cordial and benevolent reception to me.

I am not a professional developer, just a simple amateur, but I always thought that the knowledge and knowledge were to be shared between all, without any border, which I endeavour to make when there is something of interesting to divide, that it is the fact of my lucky finds, or those of other French-speaking script writers, of which I have the chance of côtoyer…

I do not have an other thing to add, just to thank the permanent members for this forum, for the quality of their exchanges and their broadmindedness.

So long…

PS. I made a machine translation, with translators on line, but without taking the trouble to correct the translations, you will be able to thus see the number of inaccuracies with which I am confronted when I write a message for this forum, and I join, Ci below, the original French message, I am certain, Mr Mikey-San, that you will try hard to make a small effort to try to understand my message, such as I wrote it in the beginning… Thank you.


Ho… ok ok ok… vraiment désolé, mille excuses Monsieur Mikey-San !

D’accord, mais alors, pouvez-vous expliquer pour quelle raison s’il vous plaît ?

D’accord, mais alors, pourquoi les variables d’environnement dans un simple script AppleScript n’expirent pas également au moment de quitter le script ?
Auriez-vous une explication à ce phénomène “bizarre” ?

Ce que je constate, Monsieur Mikey-San, ce que vous vous évertuez à démontrer le manque d’exactitude et les ambiguïtés de mes propos, sans tenir compte une seconde que ces “inexactitudes” sont d’avantage dûs à un manque de maîtrise de la langue Anglaise, qu’à une approche érronée ou ignorante de la question traitée.

Sachez, Monsieur Mikey-San, que j’ai passé plus d’une heure à essayer de rédiger mon premier message, en utilisant l’aide des traducteurs en ligne, qui, bien qu’ils soient très pratiques, ils ne sont pas réputés pour leur exactitude, et, malheureusement, mon faible niveau d’anglais m’empêche de voir ces “approximations”.

Mais, Monsieur Mikey-San, avant de pointer les erreurs de mon message, avez-vous pris la peine, ne serait-ce qu’un instant, d’aller regarder et lire le long message que j’ai rédigé sur la question dans le forum francophone dont j’ai confié l’adresse ?

Avez-vous consenti à faire le moindre effort pour lire un texte dans une langue qui n’est pas la vôtre, comme je m’efforce de le faire à chaque fois que je viens ici ?

Alors, avant de signaler, avec autant de perfectionnisme et de suffisance, mes erreurs et approximations, je vous invite, courtoisement, à faire vous aussi un effort pour lire et comprendre ce que j’ai dit à propos de variables “property” dans une application ASStudio dans le forum francophone évoqué…

Si, à la lecture de ces messages, vous trouvez encore que mes propos sont totalement incorrectes, alors, Monsieur Mikey-San, je vous serez gré de bien vouloir nous apporter des explications et précisions capables de nous éclairer, à tous les amateurs comme moi, qui ne demandent pas mieux que d’apprendre, sur les mécanismes de fonctionnement et les particularités de ces variables “property”…

Enfin, sachez, Monsieur Mikey-San, que j’ai participé longtemps dans ce forum par le passé, mais, malheureusement, pour diverses raisons, j’ai dû réduire mes interventions, mais j’ai toujours gardé un oeil sur certains échanges ici, tout simplement parce que j’apprécie beaucoup le talent et le savoir faire des différents participants, qui m’ont toujours réservé un accueil chaleureux et bienveillant.

Je ne suis pas un développeur professionnel, juste un simple amateur, mais j’ai toujours pensé que le savoir et les connaissances devaient être partagés entre tous, sans aucune frontière, ce que je m’efforce de faire lorsqu’il y a quelque chose d’intéressant à partager, que ce soit le fait de mes trouvailles, ou de celles d’autres scripteurs francophones, dont j’ai la chance de côtoyer…

Je n’ai pas d’autre chose à ajouter, juste remercier les membres permanents de ce forum, pour la qualité de leur échanges et pour leur ouverture d’esprit.

A bientôt…

PS. J’ai fait une traduction automatique, avec des traducteurs en ligne, mais sans prendre la peine de corriger les traductions, vous pourrez ainsi voir le nombre d’inexactitudes auxquels je suis confronté lorsque je rédige un message pour ce forum, et je joins, ci dessous, le message original en français, je suis certain, Monsieur Mikey-San, que vous vous donnerez la peine de faire un petit effort pour essayer de comprendre mon message, tel que je l’ai rédigé à l’origine… Merci.

Hi all! I have another question about properties! If I have a xCode AS-S proyect with two or more applescript files in it and I would like to use a variable defined in one file as a property in another file… I can’t! Well, what type of variable can I use?

I could store values in a window inside a hidden textbox… ok as long as there is a window active… but what if the app has no active windows when you need a value?
I could store the value in the user defaults… but that would mean I’d have to read/write from/to disk everytime I needed to… and that could be every second if an idle handler exists, for example. (probably not good)
Would a global work?

Any other options? Thanks!

Hello … hola Nikel ! :wink:

Féliz de encontrar un compatriota por aqui… :slight_smile:
Me gusta mocho tu firma… yo confirmo, America es un continente !

Bueno, por respeto por los otros participantes, trato de continuar en ingles… enfin, como pueda :wink:

Ok, sorry for this small digression…

There are many ways to passing the values between various scripts in a ASStudio project…

The “user defaults”, of course, but also a simple text file, in the package of the application, could make the deal.

There is also the possibility of using a file “script”, especially dedicated to pass the informations, quite simply by using the orders “store script”, to save the values, and “load script”, to read them (see the dictionary of Osax “StandardsAdditions”).

But, personally, I use another method in my projects, by using the functions of the class “pasteboard”.

As explained in document of ADC, has there several types of “pasteboard” managed by ASStudio, for my part, I chose to use the “drag”, because my applications do not make use of it in general, therefore it rest available and does not come to disturb the data of the general “pastboard”.

Here how I makes use of it:

1. First of all, it is necessary to activate and configure the pastboard, in a event handler to launching of the application for example ("awake from nib ", “will finish launchig”, “open will”, etc.), with the following code:

2. To be able to pass a value, the code to be used here:

3. Lastly, to recover a value, in another script for example, here the code:

This technique is very simple, fast and powerful, and that avoids using external files to pass the datas and values.

Espero que este mensage te ayude un poco :slight_smile:
Hasta pronto Nikel… :wink:

All I am left with is to apologize to you, sir, for my malfeasance. :slight_smile:

The variable type “property” was designed during the days of single-user Mac OS, and back then, it would store values after reboots. You didn’t need preference files for AppleScripts back then because of that. Vanilla (not Studio) AppleScript applications and scripts still work this way, presumably to maintain compatibility with the way older scripts were written to work.

Now, I noticed another big mistake (other than my tone) that I made in my post:

In AppleScript Studio, the “property” variables are not persistent across relaunching the app itself. Simple AS-S test:

property x : "cleared"

on clicked theObject
	display dialog x
	set x to "stored"
end clicked

Launch, click the button once, click again, quit, repeat.

For some strange reason, I don’t remember it working this way. I seemed to recall “property” being akin to a session variable from several scripting expeditions in the recent past, but it would appear as though this isn’t the case. “Properties” are indeed redefined on launch, it seems.

Why does Studio work like this? I must admit ignorance here. I suspect that, since it is easier in most cases not to have to reset “properties”, Studio does it that way. AppleScript is simply reading stored data while Studio is setting them from internal script code at runtime, essentially. I’ll have to dig through the AS/AS-S documentation to see if I can find some real rhyme or reason. Perhaps the MacScripter veterans around here have some light that can be shined on the situation.

Heck, I can’t even remember why I thought it was like a session variable! :slight_smile:

I would definitely recommend using /usr/bin/defaults or the “user defaults” system built into AppleScript. They’re clean and pretty easy to use. You could write to and read from a text file and that would accomplish the same thing, but it’s not quite as easy and flexible as the two “defaults” systems.