Jump to content

How do I pass data to another copy of script?


Rarst
 Share

Recommended Posts

I am writing small program (GUI that processes file with help of CLI utility "avdump") and learning AutoIt while at it. :) Making program easy and fast to use I am packing it with as much ways to open file as I can. I have working file open dialog, drag'n'drop file into GUI... Last thing I added was accepting file from cmd line. Looks like this:

Case $msg = $open Or $msg = $GUI_EVENT_DROPPED Or ($msg = 0 And $CmdLineRaw And @ScriptName <> "AGR.au3")
$file = 0
If $msg = $GUI_EVENT_DROPPED Then
 $file = @GUI_DragFile
 $start = @GUI_DropID
ElseIf $CmdLineRaw And @ScriptName <> "AGR.au3" Then
 $file = $CmdLine[1]
 $CmdLineRaw = 0
Else
 $file = FileOpenDialog("Open file", "", "", 1)
EndIf

Accepting cmd line allows it to work with "Open with" in windows, and dragging file on compiled exe (or shorcut to it). But I also want to only run one copy of program, using code suggested in help file.

$g_szVersion = "AGR v0.23 window"
If WinExists($g_szVersion) Then
    WinActivate("AGR v0.23 window")
    Exit
EndIf
AutoItWinSetTitle($g_szVersion)

But now I have a problem - first copy of program has no problem accepting file from command line, but dropping file on shortcut obviously stops working (scripts detects running copy and quits). So if script detects another copy of itself - can he throw some data to that copy before quiting?

Link to comment
Share on other sites

Use the function _Singleton() in the Misc.au3 UDF to detect if it's already running, and if so pass the data to the other instance before exiting.

How to pass the data depends on the script. The second instance can interact with the GUI controls of the first, but you may not want the first instance interfered with. You could set up hidden controls just for that purpose, or use registry entries, or intermediate files, etc. There are other choices that depend on what the script does specifically.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

So if script detects another copy of itself - can he throw some data to that copy before quiting?

You can use ControlSend. For example, the script could have an edit box. The second script could just write into the edit box in the first script then exit. The first script could keep checking the contents of the edit box and do something.

You can also use messages and memory functions. I did an example for mary here where number of copies of the same script can run.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I had tried putting it in AutoItWinSetTitle of second copy and making first one look for it, but it made it needlesly eat some CPU.

After that came to pressing invisible button and passing data in file. Thanks for help. :)

By the way is _Singleton() function better than detecting window title? Result is pretty much same, so is it more reliable or something?

Link to comment
Share on other sites

I had tried putting it in AutoItWinSetTitle of second copy and making first one look for it, but it made it needlesly eat some CPU.

After that came to pressing invisible button and passing data in file. Thanks for help. :)

By the way is _Singleton() function better than detecting window title? Result is pretty much same, so is it more reliable or something?

Window titles can change.

_Singleton() is looking for an "occurance name", which is coded into the script and is not the same as the title:

#include <misc.au3>

If _Singleton("Test - One, Two, Three", 1) Then
    MsgBox(64, "OK", "All clear...")
Else
    MsgBox(16, "Error", "Already running...")
    Exit
EndIf

HotKeySet("{ESC}", "_Quit")

While 1
    Sleep(20)
WEnd

Func _Quit()
    Exit
EndFunc

One of the consequences of this is that a compiled script and the same one running from SciTE or command line can still recognize each other, which wouldn't work by counting on the window title or process name.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...