Friday, August 23, 2019

#1 2019-04-26 09:03:19 am

ldicroce
Member
Registered: 2017-11-25
Posts: 124

Global variable across two scripts

I have a windowless stay-open applet that has a line of code of this kind:

Applescript:

on idle
If Todo_or_Not_todo then
beep -- this of course is more complicated that just "beep"
end if
end idle

The Todo_or_Not_todo is global boolean variable  that I would like to change using a second script, which I can run anytime I want.

Applescript:

set Todo_or_Not_todo to true -- or false

Is that possible? In other words is possible to share a single global variable between two different scripts?

Offline

 

#2 2019-04-26 11:44:02 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 411

Re: Global variable across two scripts

When I need to do this, I just store the information in a text file on the computer and have each script read/write it as needed. I use JSON formatting for the files to keep it clear, but you can use whatever.

Using Dropbox or a shared drive or that sort of thing, you can share data not only between scripts, but between computers.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2019-04-26 07:34:43 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 136

Re: Global variable across two scripts

I agree that a text file is the best way to do this. An alternative might be to use a property in the first script and to modify it by way of a second script as in the following:

Applescript:


set myApplet to "/Users/peavine/ScriptOne.app/"
set newValue to true
set scriptCopy to load script POSIX file myApplet
set scriptCopy's theProperty to newValue
store script scriptCopy in POSIX file myApplet with replacing

I've never done this with an stay-open applet and don't know if it would work OK.

Last edited by peavine (2019-04-27 08:09:53 am)


2018 Mac mini - macOS Mojave

Offline

 

#4 2019-04-27 12:41:41 am

ldicroce
Member
Registered: 2017-11-25
Posts: 124

Re: Global variable across two scripts

Thanks for the suggestions.
I was also considering the possibility to include in the APPLET, outside of the on idle / end idle code, several commands like those:

Applescript:

on setSendBooleanTrue()
   set SendBoolean to true
end setSendBooleanTrue

on setSendBooleanFalse()
   set SendBoolean to false
end setSendBooleanFalse

on SendBooleanStatus()
   return SendBooleanStatus
end SendBooleanStatus

Then I can trigger from the second script, by using

Applescript:

tell Application "my_APPLET"
setSendBooleanTrue
end tell

a bit like adding a custom dictionary to my APPLET. And thus let the APPLET do all the job.

Thanks anyway, I will check also your suggestions.

L.

Offline

 

#5 2019-04-27 01:05:17 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4943

Re: Global variable across two scripts

ldicroce wrote:

Then I can trigger from the second script, by using

Applescript:

tell Application "my_APPLET"
setSendBooleanTrue
end tell


It'll work with proper handler calls:

Applescript:

tell Application "my_APPLET"
   setSendBooleanTrue()
end tell


NG

Offline

 

#6 2019-04-29 04:38:56 am

ldicroce
Member
Registered: 2017-11-25
Posts: 124

Re: Global variable across two scripts

Nigel Garvey wrote:


It'll work with proper handler calls:

Applescript:

tell Application "my_APPLET"
   setSendBooleanTrue()
end tell



You are right.
Sorry for the mistake.
Thanks !

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)