Scoox Posted September 3, 2018 Posted September 3, 2018 (edited) This may not be possible without a lot of technical mambo jambo but what I'm trying to do is something I took for granted in AutoHotkey, where it was achieved with a one-liner: #SingleInstance, Force With this directive in place, re-running the script causes a old instance to terminate first and then the new instance runs again. When writing AHK scripts in my preferred text editor, I can get the script path from the editor window title, which allows me to use Ctrl+S to save the script and re-run it. The old instance exits and the new one, with the saved changes now in place, starts. This is very convenient for quickly saving and testing code. I wonder if there's a way to do something like this in AutoIT? I've searched high and low and played around with _Singleton but this UDF can only effect the new instance not the old one. I'm not a programmer so whatever I come up with is bound to be a botch. At this point the only solution I can think of is to have the first instance write its own @AutoItPID to a temp file so the new instance can fetch it later and kill the old one. Thanks in advance! Edited September 4, 2018 by Scoox
careca Posted September 3, 2018 Posted September 3, 2018 I was in the process of getting a solution for this when i got stuck with the most basic thing: $aProcessList = ProcessList(@AutoItExe) ;@AutoItPID @AutoItExe @ScriptFullPath MsgBox(64, '$aProcessList[0][0]', $aProcessList[0][0]) Supposed to give me at least 1, but it's always 0. With that i would then build up on it and eventually compare the pid and kill the proccess that has the same name and a different pid than @AutoItPID Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
Danp2 Posted September 3, 2018 Posted September 3, 2018 @careca Did you try removing the path before passing the process name to ProcessList? Latest Webdriver UDF Release Webdriver Wiki FAQs
Bert Posted September 3, 2018 Posted September 3, 2018 2 hours ago, Scoox said: This may not be possible without a lot of technical mambo jambo but what I'm trying to do is something I took for granted in AutoHotkey, where it was achieved with a one-liner: #SingleInstance, Force With this directive in place, re-running the script causes a old instance to terminate first and then the new instance runs again. When writing AHK scripts in my preferred text editor, I can get the script path from the editor window title, which allows me to use Ctrl+S to save the script and re-run it. The old instance exits and the new one, with the saved changes now in place, starts. This is very convenient. I wonder if there's a way to do something like this in AutoIT? I've searched high and low and played around with _Singleton but this UDF can only effect the new instance not the old one. I'm not a programmer so whatever I come up with is bound to be a botch. At this point the only solution I can think of is to have the first instance write its own @AutoItPID to a temp file so the new instance can fetch it later and kill the old one. Thanks in advance! And why can't you use AHK to do this job? Is there something AHK can't do that AutoIt can? They are both similar in design and in fact AHK is an offshoot of AutoIt. Why the need to switch languages? The Vollatran project My blog: http://www.vollysinterestingshit.com/
Bert Posted September 3, 2018 Posted September 3, 2018 I can tell you what you are looking for is _Singleton Example: #include <Misc.au3> #include <MsgBoxConstants.au3> If _Singleton("test", 1) = 0 Then MsgBox($MB_SYSTEMMODAL, "Warning", "An occurence of test is already running") Exit EndIf MsgBox($MB_SYSTEMMODAL, "OK", "the first occurence of test is running") The Vollatran project My blog: http://www.vollysinterestingshit.com/
careca Posted September 3, 2018 Posted September 3, 2018 OP already mentioned singleton, but as he said, it doesn't affect the already running instance. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
careca Posted September 3, 2018 Posted September 3, 2018 1 hour ago, Danp2 said: Did you try removing the path before passing the process name to ProcessList? Tried the macros, and the process name in quotes. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
Danp2 Posted September 4, 2018 Posted September 4, 2018 2 hours ago, careca said: Tried the macros, and the process name in quotes. How about this -- #include <File.au3> Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit ( @AutoItExe, $sDrive, $sDir, $sFileName, $sExtension ) $aProcessList = ProcessList($sFileName & $sExtension) MsgBox(64, $sFileName & $sExtension, $aProcessList[0][0]) Latest Webdriver UDF Release Webdriver Wiki FAQs
Scoox Posted September 4, 2018 Author Posted September 4, 2018 (edited) 9 hours ago, Bert said: And why can't you use AHK to do this job? Is there something AHK can't do that AutoIt can? They are both similar in design and in fact AHK is an offshoot of AutoIt. Why the need to switch languages? It's the GDI+ and MIDI libraries that caught my eye. The other thing I like about AutoIT is that the syntax is... well, not a mess. I'm sure AHK v2 will sort that out but it's been in the alpha stage for a long time and I already know Visual Basic so I thought learning a bit of AutoIT couldn't hurt. Here's what I did to emulate #SingleInstance, Force: SingleInstanceForce() ; Your code here While 1 Sleep(100) WEnd Func SingleInstanceForce() $PidFile = @ScriptFullPath & ".pid" $PID = FileReadLine($PidFile) ProcessClose($PID) FileDelete($PidFile) FileWriteLine ($PidFile, @AutoItPID) EndFunc It works exactly as expected but a 'ghost' tray icon lingers until the mouse pointer passes over it. If someone has a more elegant solution I'd be very interested. Edited September 4, 2018 by Scoox
careca Posted September 4, 2018 Posted September 4, 2018 I tried again, now it works, but the tray icon is still there. But maybe with this: https://www.autoitscript.com/forum/topic/94447-_refreshtrayicons-_refreshnotificationareaicons-udf-tray-icons-refresh-redux/ It can go away. Local $aProcessList = ProcessList(@ScriptName) If $aProcessList[0][0] >= 2 Then For $p = 1 To $aProcessList[0][0] If $aProcessList[$p][1] <> @AutoItPID Then ProcessClose($aProcessList[$p][1]) EndIf Next EndIf Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
mikell Posted September 4, 2018 Posted September 4, 2018 Personally to close the first instance and keep the 2nd one I use the window title, and the code is nearly the same I have no 'ghost icon' problem (with my OS) Func _killOriginal($winTitle) Local $processID = WinGetProcess($winTitle) While WinExists($winTitle) WinClose($winTitle) Sleep(10) ; allow window to exit ProcessClose($processID) Sleep(10) ; allow process to exit WEnd EndFunc ;==>_killOriginal
Tersion Posted September 5, 2018 Posted September 5, 2018 Scoox, About 'ghost' icon in a tray - maybe would help using #NoTrayIcon ?
Deye Posted September 5, 2018 Posted September 5, 2018 Testing this method with this new release I know its not full proof but i'm letting it run for now the same idea can be done with just a file .. expandcollapse popup#include <WinAPIHObj.au3> #include <WinAPIFiles.au3> Global $hMapping, $pAddress, $tData SingleInstanceForce() While 1 Sleep(100) WEnd Func SingleInstanceForce() $hMapping = _WinAPI_CreateFileMapping(-1, 2048, 'MyFileMapping') $pAddress = _WinAPI_MapViewOfFile($hMapping) $tData = DllStructCreate('wchar[1024]', $pAddress) Local $PID = DllStructGetData($tData, 1) DllStructSetData($tData, 1, '') If Not $PID Then MsgBox(262208, "", " I'm PID: " & @AutoItPID, 2) Else While ProcessExists($PID) Sleep(3500) MsgBox(0, "Wow, A Quote from the Ghost Rider ? ", @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "D'oh!", 4) WEnd MsgBox(262208, "^_^", " just PID: " & @AutoItPID, 2) EndIf DllStructSetData($tData, 1, @AutoItPID) AdlibRegister("_Noghosts") EndFunc ;==>SingleInstanceForce Func _Noghosts() If DllStructGetData($tData, 1) Then Return AdlibUnRegister("_Noghosts") _WinAPI_UnmapViewOfFile($pAddress) _WinAPI_CloseHandle($hMapping) Exit MsgBox(262208, "^_^", '"No ghosts where ever been seen by two pairs of eyes".' & @CRLF & _ @CRLF & @CRLF & "I'm PID: " & @AutoItPID & " And your PID is .. ?", 6) EndFunc ;==>_Noghosts
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now