AutoHotkey Community

It is currently May 27th, 2012, 4:00 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: May 21st, 2011, 2:46 am 
Offline
User avatar

Joined: March 1st, 2011, 12:46 am
Posts: 254
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.ahk
Code:
+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.ahk
Code:
#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.ahk
Code:
/*
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
}

_________________
Previously known as TomXIII
Image
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2011, 10:43 am 
Offline

Joined: April 16th, 2011, 7:18 am
Posts: 176
Location: Jogjakarta, Indonesia
Wow... I don't know if this is possible before :shock:

The only thing that i know was using INI, something like this:
Code:
[script name a]
pid=1234
state=Running
[script name b]
pid=2345
state=Waiting
[script name c]
pid=4567
state=Done
....

Very cool. R3gX. :D

_________________
AHK_Lw 1.1.05, OS: XP SP3, Firefox 3


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2011, 11:27 am 
Offline
User avatar

Joined: March 1st, 2011, 12:46 am
Posts: 254
@j[]hn :
Quote:
Wow... I don't know if this is possible before :shock:
It's possible for a very long time but I never thought of before!
Quote:
The only thing that i know was using INI, something like this:
Both clipboard and files are used in Inter-Process Communication
Actually, the greatest thing is the OnClipboardChange label !

_________________
Previously known as TomXIII
Image
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2011, 11:35 am 
Offline

Joined: October 2nd, 2009, 12:43 pm
Posts: 283
I prefer communication with window messages (SendMessage, OnMessage).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2011, 1:55 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Windows messages make sending data difficult. If it comes from a different process you need to use RemoteBuffer to use more than a number.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2011, 2:48 pm 
Offline
User avatar

Joined: March 1st, 2011, 12:46 am
Posts: 254
@flak :
Quote:
I prefer communication with window messages (SendMessage, OnMessage).
I found a script that uses the SendMessage and OnMessage but it sends a message letter by letter and uses GUIs.
This is not what I need! If you have a good script, could you post it on this topic?!

@fragman :
Quote:
If it comes from a different process you need to use RemoteBuffer to use more than a number.
Have you a script sample?
BTW, what do you think about the method?!

I use the clipboard because it's the most simple way I found without using files and it doesn't need to much lines of code!

_________________
Previously known as TomXIII
Image
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group