saving properties in the script for use at another run

I’m trying to save a property in the script file “Up L” with another script.

First I run the script to save the property…


set thePath to "/Users/DJ/Music/iTunes/Scripts/Up L.scpt"
set thePath to POSIX file thePath

set theScript to load script file thePath
set theVariable of theScript to "test"
store script theScript in file thePath with replacing

And then when I run the script “Up L” with the contents, (should return “test” here), it returns 0.



property theVariable : 0
return theVariable


What is the correct way to store a property in a script file?

Hi,

your’re mixing up the file specifier (. file . POSIX file),
try it with just an alias specifier


set thePath to ((path to music folder as text) & "iTunes:Scripts:Up L.scpt") as alias

set theScript to load script thePath
set theVariable of theScript to "test"
store script theScript in thePath with replacing


Hi fhill,

You can also see the AppleScriptLanguageGuide.pdf. Look at page 69. Not sure but I think that’s what you’re trying to do. In oop or oops :wink: I forget but I don’t think you can change the property of a compiled script without unix. I think they call it an instance where you initialize the script. I’ve been away so try reading that. If that’s not it then disregard.

gl,
kel

Here’s an example, but I haven’t tried storing the script yet:

on MakeDialog(the_text)
	script SaySomething
		property t : the_text
		display dialog t
		return
	end script
end MakeDialog
set NewDialog to MakeDialog("hello")
run script NewDialog

gl,
kel

I’m not sure that my understanding matches your needs.

script 1 stored as Up L.scpt on the desktop

property theVariable : 0

script 2 : used to store a value in the script 1

"~/Desktop/Up L.scpt"
do shell script "echo " & result
set thePath to POSIX file result

set theScript to load script thePath
set theVariable of theScript to "Napoléon attendait Grouchy, ce fut Blücher"
store script theScript in thePath with replacing

script 3 : extracts a value from script 1

"~/Desktop/Up L.scpt"
do shell script "echo " & result
set thePath to POSIX file result

set theScript to load script thePath
get theVariable of theScript

When I run script 2 then script 3 I got the wanted value :
Yes, it really was Blücher.

An explanation may be useful : the instruction
do shell script "echo " & result
expands the tilde used as a shortcut for (Unix path to the Home folder).
I like it because it gives the ability to write a script running on my machine for tests without entering my user name.
Most of the time I use:
path to home folder
but askers are often posting questions using Unix paths so tilde is the alternative.

Now that we know how to store a variable in a script so that an other one may use it, I wonder if it’s the good way to do that.
Isn’t it better to store the value in a preference file ?

Yvan KOENIG (VALLAURIS, France) lundi 16 juin 2014 11:03:25

Here’s a bit of what I was saying:
http://macscripter.net/viewtopic.php?id=40684

Edited: note that you can now send parameters through osascript to the run handler of the compiled script.

gl,
kel