[Library] GuiVar - Use Gui as Variable Between AHK Scripts

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
tzucker
Posts: 42
Joined: 28 Oct 2014, 14:07
Location: Bronx, New York
Contact:

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scrip

06 Nov 2014, 15:57

I figured something out with my scripts, although I am NOT sure what it means. The backup script seems to work fine, but if I try to use it on a large database there is an audible ding right after the save dialog appears and the name of the db is not correct. instead of being <drive>:\<datetimestamp>_<libraryname>.enlx it is truncated to part or all of the library name and sometimes part of the datetime and saved in the default directory. For instance, something that should be named O:\20141106_1545_aecclib.enlx is named _aecclib.enlx and saved in the documents directory or 45_aecclib.enlx or _1545_aecclib.enlx or even lib.enlx

When I use a msgbox the variables are correct and present. When I use a smaller db the information is sent correctly. Should I be using a WinWaitClose?

update: I used winwaitclose and problem resolved.

Code: Select all

; ------------------------------------------------------------------------------------------------------------------------------
; ----- This script performs the following actions                                                                         -----
; -----                                                                                                                    -----
; -----     1. Sets the title matching mode for a partial beginning string                                                 -----
; -----     2. Checks for the existence of the window for EndNote X7 -, then activates that window                         -----
; -----     3. Isolates and puts in a variable the name of the open library                                                -----
; -----     4. Backs up the current library to an endnote library folder of your choosing                                                   -----
; -----     5. Names the backup file (with extension .enlx) with today's date and time and the name of the library         -----
; -----                                                                                                                    -----
; ------------------------------------------------------------------------------------------------------------------------------
; -----                                                                                                                    -----
; -----                                This script was written by Thomas Zucker-Scharff                                    -----
; -----                                                                                                                    -----
; ------------------------------------------------------------------------------------------------------------------------------
Return

#!b:: ; assign [Win]+[ALT]+b to backup script
MsgBox, Have you opened your library yet, if not please do so, then click Okay.
; *********************************************************
; ***** get shared variables using GuiVar library     *****
; *********************************************************
	MyFolder := GuiVar_Get("SelectedFolder")
	MyFile := GuiVar_Get("NowText")
	BaseFileName := GuiVar_Get("BaseFileName")
	Destination := GuiVar_Get("Destination")
; *********************************************************
FileSelectFolder, MyFolder,,6,Select a Destination Folder ; ask for preferred destination folder
datetime = %A_Year%%A_MM%%A_DD%_%A_Hour%%A_Min%
FullFileName = %MyFolder%\%datetime%
; **************************************************************
; ***** Determine if chosen directory is folder or drive   *****
; ***** If it is a directory put extra \ in, else not      *****
; **************************************************************
	slashes = \\
	ifInString FullFileName, %slashes%
	{
		FullFileName = %Myfolder%%datetime%
	}
	Else
	{
		FullFileName = %MyFolder%\%datetime%
	}
    ; **************************************************************
SetTitleMatchMode,1 ; title must begin with String
IfWinExist,EndNote X7 - ; Endnote must have a library open to display the - after the title name
{
   WinActivate
   WinGetActiveTitle, title
   mytitle:=substr(title, instr(title, "["), -5) ; -5 gets rid of ".enl]"is to end of line
   libname:=substr(mytitle, 2) ; basename of library
   CompressedFileName = %FullFileName%_%libname% ; get GuiVar variable basefilename and put into variable destination
   Send !f ; send alt f to access the File menu
   Send {UP 2} ; send 2 up keystrokes to access the compressed file function
   Send {ENTER} ; send enter key to choose function
   Send {ENTER} ; send the enter key to continue with no changes
   sleep 1000
   Send %CompressedFileName%
   Send {ENTER}
   SetTitleMatchMode,1 ; title must begin with String
   WinWaitClose, Save Compressed Library
}
else
	MsgBox, Please open One Library, No Open Library was found or more than one library is open. Please rectify the situation and rerun the script.
I added the settitlematchmode and winwaitclose lines.
Tom Zucker-Scharff
An AutoHotKey Newbie
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scrip

06 Nov 2014, 18:16

UPDATED SCRIPT IN FIRST POST

Change Log: Version 1.1
Added ability to destroy GuiVar windows with:
GuiVar_Destroy
GuiVar_DestroySetTimer

It is important to note that only the script that created the Gui window can destroy the Gui window. Although you can use the GuiVar_DestroySetTimer function in the creating script so that any script can then put a flag value into the GuiVar that will cause the creating script to destroy it on a set timer interval.

Instead of destroying a GuiVar you can make it equal to "" which will frees most of the its resources.

I looked at having one script destroy another script's Gui but from what I can tell even if I got Windows to do it directly then it will not cause AHK to free the resources.

Fixed a bug where two GuiVar that started with the same characters could become confused because of partial matching instead of exact matching. If you had two names like Fire and Firetruck then when you tried to Get a value from Fire it might return the value of Firetruck.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scripts

16 Nov 2017, 18:17

UPDATED SCRIPT IN FIRST POST

Added Class: Version 1.0

The class version is a little easier to use because of the class's dynamic Get and Set.

Allows stuff like this:

Script1.ahk

Code: Select all

; Script1.ahk
X := new GuiVar("Tree")
X.Value := "Apple
Script2.ahk

Code: Select all

; Script2.ahk
Y := new GuiVar("Tree") ; "Tree" must be same name used in script1.ahk to link GuiVar
MsgBox % Y.Value	; will display "Apple"
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scrip

17 Nov 2017, 16:27

Relayer wrote:I have been fascinated by classes and look for every opportunity to put another one together. I was intrigued by FG's use of a hidden Gui for cross communication between scripts. Well, here is the functionality in the form of a class. It may or may not be useful, but the more I worked with it, the more I liked it, so I thought I'd share. The level of complexity is higher at the code level but should make using this functionality more natural (er, I think :eh: ) and it enforces some discipline in defining shared variables.

Put the class and the two demo scripts in the same directory and run script#1.ahk. It will automatically start script#2.ahk and then run through a demonstration of the capabilities. Read the class documentation header first to get a primer.

Let me know what you think. -Relayer
Relayer: I'm coming across your post three years later - very cool indeed!
FanaticGuru: thanks so much for this.

:bravo:
Regards, burque505
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scrip

17 Nov 2017, 17:22

burque505 wrote:
Relayer wrote:I have been fascinated by classes and look for every opportunity to put another one together. I was intrigued by FG's use of a hidden Gui for cross communication between scripts. Well, here is the functionality in the form of a class. It may or may not be useful, but the more I worked with it, the more I liked it, so I thought I'd share. The level of complexity is higher at the code level but should make using this functionality more natural (er, I think :eh: ) and it enforces some discipline in defining shared variables.

Put the class and the two demo scripts in the same directory and run script#1.ahk. It will automatically start script#2.ahk and then run through a demonstration of the capabilities. Read the class documentation header first to get a primer.

Let me know what you think. -Relayer
Relayer: I'm coming across your post three years later - very cool indeed!
FanaticGuru: thanks so much for this.

:bravo:
Regards, burque505
I forgot that Relayer even did a class version of GuiVar. :shock:

I posted a link to this thread the other day which made me realize that this would work well as a class. I have gotten more comfortable with classes in the past three years since my original script so I did my own class version pretty quickly and posted it.

There are more complex techniques that might be better in some ways for passing information between scripts but this concept is fairly simple and works well in my experience.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
rj8810
Posts: 31
Joined: 16 Jul 2018, 22:34

Re: [Library] GuiVar - Use Gui as Variable Between AHK Scripts

28 Apr 2020, 00:03

Hello, Thanks in advance for all your posts. I want to ask you how I can use Class Guivar to share information between scripts, I managed to use [library]Guivar without problems after reading for several minutes and realizing that I needed to create a library, (I'm navato), so I want to ask you? is the same with [class] GuiVar ?, do i need a library? I have tested your example and msgbox always appears blank, please help me step by step.
I must tell you that I put the 3 scripts ([class] guivar, script 1, script 2) in the same folder, I also tried putting [class] guivar in my Lib folder, but it doesn't work, what am I doing wrong?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: KruschenZ, TAC109 and 59 guests