AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to comunicate between two AHK scripts

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
robcheese



Joined: 07 Jul 2008
Posts: 27

PostPosted: Tue Sep 09, 2008 4:25 am    Post subject: How to comunicate between two AHK scripts Reply with quote

Hi, I'm currently using two AHK scripts that run simultaneously. At some point I want the First script to trigger some code from the second script. Right now I'm just sending "q" and "w" from the first script which trigger two hot keys in the second script.

I there a cleaner way to do this? I've tried using obscure keys that I don't use but the second script doesn't pick up on the first script sending them.

Thanks

Rob
Back to top
View user's profile Send private message
wrecklass



Joined: 19 Mar 2007
Posts: 29

PostPosted: Tue Sep 09, 2008 4:40 am    Post subject: Reply with quote

Is there some reason it has to be a keystroke? Can you define a function in one script, include the script and call it in the second?

Perhaps if you told us more specifically what you need to do.
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1390
Location: The Interwebs

PostPosted: Tue Sep 09, 2008 4:50 am    Post subject: Reply with quote

Read about SendMessage() and OnMessage() to see how to send messages from one script/process to another.

You can also append text to a file, and have the other script read it—less complicated, but much slower and probably less reliable.
Back to top
View user's profile Send private message AIM Address
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Tue Sep 09, 2008 5:11 am    Post subject: Reply with quote

You might also want to check out this script from a page in the help file (don't blame you for not finding it, it took me a little while and I knew it was there Razz )
Code:
; Example: Send a string of any length from one script to another.  This is a working example.
; To use it, save and run both of the following scripts then press Win+Space to show an
; InputBox that will prompt you to type in a string.

; Save the following script as "Receiver.ahk" then launch it:
#SingleInstance
OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a is WM_COPYDATA
return

Receive_WM_COPYDATA(wParam, lParam)
{
    StringAddress := NumGet(lParam + 8)  ; lParam+8 is the address of CopyDataStruct's lpData member.
    StringLength := DllCall("lstrlen", UInt, StringAddress)
    if StringLength <= 0
        ToolTip %A_ScriptName%`nA blank string was received or there was an error.
    else
    {
        VarSetCapacity(CopyOfData, StringLength)
        DllCall("lstrcpy", "str", CopyOfData, "uint", StringAddress)  ; Copy the string out of the structure.
        ; Show it with ToolTip vs. MsgBox so we can return in a timely fashion:
        ToolTip %A_ScriptName%`nReceived the following string:`n%CopyOfData%
    }
    return true  ; Returning 1 (true) is the traditional way to acknowledge this message.
}

; Save the following script as "Sender.ahk" then launch it.  After that, press the Win+Space hotkey.
TargetScriptTitle = Receiver.ahk ahk_class AutoHotkey

#space::  ; Win+Space hotkey. Press it to show an InputBox for entry of a message string.
InputBox, StringToSend, Send text via WM_COPYDATA, Enter some text to Send:
if ErrorLevel  ; User pressed the Cancel button.
    return
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if result = FAIL
    MsgBox SendMessage failed. Does the following WinTitle exist?:`n%TargetScriptTitle%
else if result = 0
    MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
return

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle)  ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.
{
    VarSetCapacity(CopyDataStruct, 12, 0)  ; Set up the structure's memory area.
    ; First set the structure's cbData member to the size of the string, including its zero terminator:
    NumPut(StrLen(StringToSend) + 1, CopyDataStruct, 4)  ; OS requires that this be done.
    NumPut(&StringToSend, CopyDataStruct, 8)  ; Set lpData to point to the string itself.
    Prev_DetectHiddenWindows := A_DetectHiddenWindows
    Prev_TitleMatchMode := A_TitleMatchMode
    DetectHiddenWindows On
    SetTitleMatchMode 2
    SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a is WM_COPYDATA. Must use Send not Post.
    DetectHiddenWindows %Prev_DetectHiddenWindows%  ; Restore original setting for the caller.
    SetTitleMatchMode %Prev_TitleMatchMode%         ; Same.
    return ErrorLevel  ; Return SendMessage's reply back to our caller.
}

; Example: See the WinLIRC client script for a demonstration of how to use OnMessage() to receive
; notification when data has arrived on a network connection.

_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.
Back to top
View user's profile Send private message Visit poster's website
robcheese



Joined: 07 Jul 2008
Posts: 27

PostPosted: Tue Sep 09, 2008 5:44 pm    Post subject: Reply with quote

Thanks for the quick responses.
I got it all up and running and I can finally use my q and w keys again.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group