Hi everyone!
During the week I wanted to find a code to communicate between two scripts (or more).
girlgamer advised me this topic
"Interprocess communication using Mailslots".
I find this script quite great but not really that I want so I've written the code provided below.
Description : The script allows a kind of communication between scripts.
The script can
- set dinamically a simple variable but no objects ( Sender command :
Clipboard := "name of the receiver.ahk var_name <= var value" )
- display a message (Sender command :
Clipboard := "name of the receiver.ahk text to be sent by the clipboard )
Sender.ahkCode:
+F1:: ; Send a message
TrayTip, Sender : Sample 1, A message should be displayed!`n(This is a message sample!)
Clipboard := "Receiver.ahk This is a message sample!"
SetTimer, RemoveTips, 5000
Return
+F2:: ; Set dynamically a variable
TrayTip, Sender : Sample 2, Look at the Receiver.ahk tray icon! [S] or [H]
Clipboard := "Receiver.ahk suspend_script <= 1"
SetTimer, RemoveTips, 5000
Return
+F3:: ; Normal use of the clipboard
TrayTip, Sender : Sample 3, Normal use of the clibpoard!
Clipboard := "It won't be used by ControlFromClipboard() !!!"
SetTimer, RemoveTips, 5000
Return
+F4:: ; Stop the two scripts
TrayTip, Sender : Sample 4, Both Sender.ahk & Receiver.ahk`nshould be stopped in 7 seconds!, 30
Sleep, 7000
Clipboard := "Receiver.ahk exit_script <= 1"
ExitApp
Return
RemoveTips:
Traytip
ToolTip
Return
Receiver.ahkCode:
#Include ControlFromClipboard.ahk
#Persistent
SetTimer, Timer, 100
OnClipboardChange:
ControlFromClipboard(A_EventInfo)
Return
Timer:
If suspend_script
Suspend
Else If exit_script
ExitApp
suspend_script := 0
Return
ControlFromClipboard.ahkCode:
/*
Name : ControlFromClipboard v0.1
Author : R3gX
Link : http://www.autohotkey.com/forum/viewtopic.php?t=72159
Description : The script allows a kind of communication between scripts.
The script can
- set dinamically a variable
- display a message
Licence :
Use in source, library and binary form is permitted.
Redistribution and modification must meet the following conditions:
- My nickname (R3gX) and the origin (link) must be reproduced by binaries, or attached in the documentation.
- If changes are made to my work, you are encouraged (yet not obliged) to post the changes for others to view, on the Autohotkey forum.
ALL MY SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESSED OR IMPLIED WARRANTIES.
*/
ControlFromClipboard(EventInfo){
static Counter := 0
If Counter<2
{ ; Don't use the function at the script beginning
Counter++
Return
}
; Extract the real value and trim the beginning/ending spaces or tabs
RegExMatch(Clipboard, "^" A_ScriptName "\s*\K(.+)", value)
value := RegExReplace(value, "^\s*")
value := RegExReplace(value, "\s*$")
; If the clipboard is empty OR contains a picture OR there is no value, return an error
If (EventInfo==0 or EventInfo==2 or not value)
Return, 1
; Checks if the function should set a variable
If RegExMatch(value, "(?P<name>\w+?)\s*?<=\s*(?P<value>.+)", var_)
{
If (StrLen(var_name) && StrLen(var_value))
{ ; Set a variable
%var_name% := var_value
Return
}
}
; Checks if the function should display a message
If RegExMatch(value, ".+", msg)
{ ; Display a message
MsgBox, % "Message received :`n" msg
Return
}
Return, 1
}