Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Terminal Script for commuication between scripts


  • Please log in to reply
8 replies to this topic
Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
Terminal Script:
;Msg Terminal Script, used for passing msgs with
;ControlSetText
;ControlGetText
#NoTrayIcon
;Port Amount, How many ports should this terminal have?
P_Amt = 300
TerminalName = Msg_Terminal

;Text used as for Identifying the script
gui, add, text, , %TerminalName%

Loop, %P_Amt%
{
	Gui, Add, Edit, vMsg%A_Index% -wantreturn
} 

SetTimer, WatchExit
Return

WatchExit:
Gui, Submit
If Msg1 = Exit
	ExitApp
If Msg1 = Reload
	Reload
Return


Function for sending and retrieving from the terminal script.
DetectHiddenWindows, On
SetTitleMatchMode, 3

Var := Msg("s", 2, "Msg_Terminal", "SomeTextToSendToTheTerminal")
MsgBox %Var%
Return

; Dir = Direction of msg, S for Send, R for Recieve
; Port = The number of the port to call upon.
; Terminal Name = The name of the terminal to accept call
; MsgText = The Text to be sent.
Msg(Dir,Port,TerminalName,MsgText)
{
	;Checks if terminal window exists
	IfWinNotExist, ahk_class AutoHotkeyGUI, %TerminalName%
		MsgBox Error! `nTerminal does not exist.	
	;Checks if port exists
	ControlGetPos, Xpos_,,,, Edit%Port%, ahk_class AutoHotkeyGUI, %TerminalName%
	If XPos_ =
		Msgbox Error! `nPort may not exist.
	;Send
	If Dir = S
		ControlSetText, Edit%Port%, %MsgText%, ahk_class AutoHotkeyGUI, %TerminalName%
	;Recieve
	If Dir = R
		ControlGetText, ReturnText, Edit%Port%, ahk_class AutoHotkeyGUI, %TerminalName%
	Return %ReturnText%
}

Function Syntax
Msg("S/R", PortNumber, "TerminalName", "Text")

The Terminal script has two options, the amount of ports that will hold information(Text only) and the name of the Terminal. The name of the terminal is used when calling it in the function, so that multiple terminals may exist, each must have a seperate name. The amount of ports affects the size of the script. This one has 300, and you may not need that many. The maximum is 10,999, and use caution with amounts greater than 4999.

The First port is reserved for handling 'commands' sent to the terminal via the function. The following are supported and explained. More may be added later on.

Exit -exits the terminal and closes it.
Reload - Restarts the terminal(clearing the contents of all ports)

Example For Closing the Terminal Script
Msg("s", 1, "Msg_Terminal", "Exit")

The terminal script should be run alone, while its calling function can be used in any seperate script. I find this method useful for notifying other scripts of status changes among themselves. It also seems to be an easy way for a new user to communicate between script as I have so many threads asking about it.
my lame sig :)

  • Guests
  • Last active:
  • Joined: --
intresting idea, i will look into it once i get into it :D

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Wow..
Beautiful idea and easy to understand. I like it!
:D

David Andersen
  • Members
  • 140 posts
  • Last active: Jun 28 2011 04:54 PM
  • Joined: 15 Jul 2005
I love the creativity behind this solution. Easy and simple. One could probably solve this problem in ways that are amazingly complex, but there is no reason to.

Helpy
  • Guests
  • Last active:
  • Joined: --
A possible gotcha could be concurrent access/update, but I don't know if that's really an issue.
Anyway, it is a very creative idea!

PFP
  • Guests
  • Last active:
  • Joined: --
Hmm, that could be very useful at work. I will check it out in more details tomorrow.

q455
  • Guests
  • Last active:
  • Joined: --
I want to revive this topic.... IThe basic question is, besides (1) Clipboard (2) Write to File and (3) ControlSet on a hidden window, is there any other "Memory" where all scripts can access -- since we don't have access to direct memory? I'm sure there are more ways out there.

It seems like a nice idea to include into the next implementation of autohotkey -- virtual clipboards, registers, whatever.

q335r49
  • Members
  • 28 posts
  • Last active: Mar 30 2010 04:05 AM
  • Joined: 26 Oct 2005
Really, the storage is no big deal. Any script can handle the storage as an array. But I don't know of any way to send like a long text string to another script, besides the above named methods.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
So if I have a script that I want to use to control four other scripts, would I want to use 5 teminals or can I have all the receiving scripts watch the same terminal? Part of my issue is having them execute the commands they receive from the sending script as synced as possible.