Sharing values of variable between 2 scripts

I know … I know … I should know this, and I should be embarrassed to ask … but
It is possible to share values between to different script ?

Script A

global sharedValues
set sharedValues to 0
repeat 100 times
	set sharedValues to sharedValues + 1
	delay 1
end repeat

while script A is running
I want to call script B which is

global sharedValues
display dialog (sharedValues as text) 

I could write the value of sharedValues to a file, but this make thing to slow (I added the “delay 1” just for the debugging, in real life things will be without the delay.

Thanks !

Hi. You can access properties from discrete script objects in the same script.

script storeThis
	property thing : 5
	1 + 1
end script

script addThing
	repeat with counter from 1 to 5
		set storeThis's thing to (storeThis's thing) + 1
	end repeat
end script

--storeThis's thing -->5
--run storeThis -->2
run script addThing -->10
--run addThing -->variable (due to persistence)

You can also load a script stored elsewhere.

run script (load script file ((path to desktop as string) & "whatever.scpt"))'s storeThis's thing

See also Apple’s developer page on AS variables and properties for more in-depth explanation.

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_variables.html

The process of writing/reading to/from a file, as I believe, is the only safe way to exchange data from 2 scripts (or apps) working in “parallel” mode. That is, as 2 different threads.

But this is not necessarily a slow process. You will be able to increase the speed of writing/reading data to a file hundreds of times by creating a RAM disk. You can create a RAM disk like this:

  1. Create RAM disk on your Mac programatically:

-- Created 2017-01-24 by Takaaki Naganoya
-- 2017 Piyomaru Software

set dName to "RAM Disk"
set dCapacity to 512 * 4 * 1024 --1GB
set aCmd to "diskutil erasevolume HFS+ '" & dName & "' `hdiutil attach -nomount ram://" & (dCapacity as string) & "`"
try
   do shell script aCmd
end try
  1. Do write/read operations, as with usual disk, but much faster.
  2. Tell to Finder to eject it, when complete read/write jobs.

The second way is using the clipboard (subject to confusion, and therefore to errors):

1st script:

set anAddress to 599
set the clipboard to anAddress

2nd script:

set anAddress to integer of (the clipboard)
display dialog (anAddress as text)

You can also use user defaults to share a value. So to share:

use framework "Foundation"
use scripting additions

set defaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.domain.I.own.something" -- unique identifier
defaults's setObject:3 forKey:"some_key"

And to fetch:

use framework "Foundation"
use scripting additions

set defaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.domain.I.own.something" -- unique identifier
set theValue to (defaults's objectForKey:"some_key") as integer

Depending on how often your first script is changing the value, it might make sense to use defaults for storage of the value all the time, rather than a variable.

Thanks a lot to both !
This open up a lot of new possibilities … at least for me !