Passing a variable from one script to another ...

Hi,

While I doubt it is possible and because my searches and readings didn’t get me nowhere, is it possible to pass the value of a variable from on script to another ?

Thanks in advance.

Robert Lespérance

The best way I found was to write the variable to a file with one script and then read it into the second script… and vice versa.

So basically passing the variable is easy. The hard part is the notification. If one script changes the variable in the file, how does the other script know it’s been changed? I haven’t solved that yet. The only thing you can do is check the value of the variable in the file every time before you use it in either script.

Hi Regulus,

What do you precisely mean by writing a variable to a file ? Can you give me an example ?

Regards.

Robert

Make these 2 scripts. Save them as stay-open applications and run them. You will see how 2 running applications can share the same variable and each can change the value which is then noticed by the other application.

global theFile, KeyName, keyType, keyValue

on run
	set theFile to (path to desktop folder as text) & "myVariableFile"
	set KeyName to "theVariable"
	set keyType to "string"
	set keyValue to "written by script1"
	
	-- make sure the file exists or else create the file
	try
		alias theFile
	on error
		writeDefault(theFile, KeyName, keyType, keyValue)
	end try
end run

on idle
	-- read the value of the variable
	set currentValue to readDefault(theFile, KeyName)
	tell me
		activate
		display dialog "script1 says:" & return & "The current value is: " & return & return & currentValue buttons {"OK"} default button 1 giving up after 5 with icon note
	end tell
	
	-- change the value of the variable
	writeDefault(theFile, KeyName, keyType, keyValue)
	
	return 10
end idle





(*=========== SUBROUTINES ==============*)
on writeDefault(prefsFilePath, KeyName, keyType, keyValue)
	set keyType to ("-" & keyType) as Unicode text
	do shell script "/usr/bin/defaults write " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
end writeDefault

on readDefault(prefsFilePath, KeyName)
	try
		set theVal to do shell script "/usr/bin/defaults read " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName
	on error
		set theVal to "Does Not Exist"
	end try
	return theVal
end readDefault
global theFile, KeyName, keyType, keyValue

on run
	set theFile to (path to desktop folder as text) & "myVariableFile"
	set KeyName to "theVariable"
	set keyType to "string"
	set keyValue to "written by script2"
	
	-- make sure the file exists or else create the file
	try
		alias theFile
	on error
		writeDefault(theFile, KeyName, keyType, keyValue)
	end try
end run

on idle
	-- read the value of the variable
	set currentValue to readDefault(theFile, KeyName)
	tell me
		activate
		display dialog "script2 says:" & return & "The current value is: " & return & return & currentValue buttons {"OK"} default button 1 giving up after 5 with icon note
	end tell
	
	-- change the value of the variable
	writeDefault(theFile, KeyName, keyType, keyValue)
	
	return 15
end idle





(*=========== SUBROUTINES ==============*)
on writeDefault(prefsFilePath, KeyName, keyType, keyValue)
	set keyType to ("-" & keyType) as Unicode text
	do shell script "/usr/bin/defaults write " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
end writeDefault

on readDefault(prefsFilePath, KeyName)
	try
		set theVal to do shell script "/usr/bin/defaults read " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName
	on error
		set theVal to "Does Not Exist"
	end try
	return theVal
end readDefault

Thank you Regulus …

I will play around with your scripts. Should do the work.

Regards.

Robert