1

script 2 doesn't work because variable CODE is forgotten after script 1 is executed.

script 1 (hotkey is set to 1)

code = 123

script 2 (hotkey is set to 2)

if code == 123:  
       keyboard.send_key("G")

How to make this or an analogue ? I need digits to be remembered, and then used in another script(s) with a condition like above.

SL5net
  • 2,282
  • 4
  • 28
  • 44
12oad
  • 11
  • 1

4 Answers4

2

AutoKey provides a global storage for data sharing across scripts.

Here is a link to the relevant API documentation: https://autokey.github.io/lib.scripting.Store-class.html

You can use store.set_global_value to store your data in script 1 and use store.get_global_value in script 2 to access the data.

luziferius
  • 86
  • 4
  • New API [documentation](http://autokey.github.io/autokey/api/). Also, be aware that if you attempt to get a stored variable before ever saving it (e.g. by doing things out of order), it will throw an exception, so you may want to put it in a try: block so you can handle that case should it arise. – Joe Feb 08 '23 at 13:05
0

Try to import Script 1 to Script 2 and then execute.

SL5net
  • 2,282
  • 4
  • 28
  • 44
Naveen
  • 770
  • 10
  • 22
  • 1
    I'm newbie, sorry, how can I do that ? Should I type "import [scriptname]" in the Script II's first line ? – 12oad Jun 23 '18 at 09:34
  • This tricky to get right in AutoKey and not needed for a simple case like this. – Joe Feb 08 '23 at 12:41
0

Did a 1 day research, and have come to this solution:


script 1:

//AutokEy commands, bla-bla-bla
//Now this will save the *string* into *code.txt*
f = open("code.txt", "w+")
f.write("string")
f.close()

script 2:

//other AutokEy commands...
//This will retrieve the *string* from *code.txt* and save it in the variable *code*
f = open("code.txt","r")
code = f.read()
f.close()

Then you can do what you want with variable code. More info on this can be found by googling "python manipulating text files"

SL5net
  • 2,282
  • 4
  • 28
  • 44
12oad
  • 11
  • 1
  • This a pure, Python-only solution that works, but it creates an external object that may need to be managed, e.g. deleted when no longer needed, backed up... Using an AutoKey stored variable would be the usual way to handle this. See other answers and comments for details. – Joe Feb 08 '23 at 12:46
0

Have you looked into a Python scheme for Global variables, even if a script terminates? I hava a feeling that this issue was raised and solved by the powers that be.

Or, perhaps you set up a separate script which contains the variable value and call on it from the other script?

ineuw
  • 105
  • 1
  • 9