What the... Property/button problem

Hey guys.

I kinda feel dumb on that problem.


property test:{}

on clicked the object
if name of theobject is "button_A" then
set test to {"test"}
log test
end

if name of theobject is "button_B"
log test
end
end


when i click on button A i get “test” in my log. When i click on button B AFTER it gives me the predefined property value, but not “test”. Button connections are ok.
What the hell?

Hi,

the object must be one word

on clicked theobject

args.

first of all, i have to say sorry. I wrote this script while travelling by train to show the problem with hope to have an answer when im at home. At home i saw that this script WORKS (without that writing mistake…) and doesnt really show the problem i got. Sorry for that! THe real problem is:

if have a mainscript that handles all the buttons and stuff.
But this script grows tooo big, so i decided to handle some stuff in other scripts.

so in mainscript at some point another script is loaded like:

set secondscript to load script file "secondscript.scpt"
tell secondscript
do_something(a,b,c)
end tell

secondscript looks something like this

property test:{}

on do_something(a,b,c)
--some code
set test to {"test"}
end do_something

on clicked theobject

if name of theobject is "button A"
log test
end if

end clicked

so secondscript handles all of the stuff i need for a specific tool in a specific window. SO i decided to let this script handle also the buttons used on this window (e.g. button A)
But when i click on button A after i called the “do_something” function here, it shows me the list “test” empty, not filled as it should be with button A!

Any ideas?

When you launch your application, all scripts get loaded. When you tell one script to load another script using load script aScript, you are not connecting to the script that was loaded when you launched the application, but you’re making a whole new ‘instance’ of that script, so you are ending up with two loaded scripts which are actually the same, but there properties aren’t shared.

So when you load the script using load script file “secondscript.scpt” you’re actually making a new script which is NOT the script that’s connected with the button which logs the value, although they’re initialized from the same file.

You can solve this by having another script, a compiled script with the extension *.scpt not *.applescript, with the property test, load the script in one script, set the script’s test, and then store the script using the built-in command. Then when you click the button, another script will again load the scpt-file, get the value of the test property and log it.

property.scpt (make sure it is a compiled script, not saved as text):

property test : missing value

SetScript.applescript

set loadedScript to load script file ((path to desktop as text) & "SCRIPT:property.scpt") -- insert path here
set loadedScript's test to "Hello World!"
store script loadedScript in (path to desktop as text) & "Script:property.scpt" replacing yes

LoadScript.applescript

set loadedScript to load script file ((path to desktop as text) & "SCRIPT:property.scpt") -- insert path here
set v to loadedScript's test
log v

Hope it makes any sense,
ief2

I hope you understand a bit,
ief2

aaaaah!

got it!
If the scripts are initialized when compiling, is there a way to access the function in second script from main script without load it?

something like (untested… on the road actually)


set secondscript to "path_to_script_in_app"
tell secondscript
do_something(a,b,c)
end tell

are the properties shared then? otherwise i could store them in user defaults if something like this doesnt work…

Not if the function needs a global variable or a property. If the variables get passed with the handler it’ll work.

Executing Script:

set myScript to load script (path to script) -- making a second script object
myScript's doSomethingWithPassedVariables("Hello World") -- Working
myScript's doSomethingWithAGlobalAndAProperty() -- Not working

Function Script:

property dialogValue : ""
global secondDialogButton

on clicked theObject
	-- variable set on a click button
	-- will set the property of the script that's
	-- connected to the application, not when used
	-- load script aScript
	set dialogValue to "Hello World"
	set secondDialogButton to "Hello again"
end clicked

on doSomethingWithAGlobalAndAProperty()
	display dialog dialogValue
	display dialog secondDialogButton
	
	(*
	When called from the script itself in
	via an application trigger, it'll work
	
	If called with load script aScript
	it'll first show an empty dialog box (using 
	the original value of dialogValue) and then
	generate an error. The global is not set.
	*)
end doSomethingWithAGlobalAndAProperty

on doSomethingWithPassedVariables(aStr)
	display dialog aStr
end doSomethingWithPassedVariables

Hope it helps,
ief2

yes i got that point.

but how do i get access to functions in secondscript from firstscript without the “load”?
How do i access the script that is compiled without loading a new instance?

I do not think that is possible

err… damn…

ok then i got to save the properties!
Thanks for help!