Jump to content

Exit Parameter for this Script


Go to solution Solved by TheXman,

Recommended Posts

I have this little script that essentially just moves desktop icons to my desired position, and then if they are moved, they "snap" back into place. I have it setup to run the .exe via login script.

I run into some issues if there are two instances of this script running at the same time. How can I make an exit parameter to detect if there are two instances of this script running, and close one of the two (the 'longer running' one would be best, but either would work).

Here is the code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\Downloads\Logo-for-Pax-Machine-in-RGB.ico
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GuiListView.au3>

Local $arrShortcutNames[19] = ["Company Postings", "Redacted", "Redacted Database", "Redacted", "Mobile Redacted", "Redacted Private", "Redacted Records", "Redacted Log Files", "Mfgx-edge-v3", "Search by Redacted in Redacted", "Redacted Schedule", "Redacted UX", "Production Card", "Calculator", "Certification", "Redacted", "CRedacted Log Files", "Notepad", "Redacted"]
Local $arrXPos[19] = [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 116, 116, 116, 116, 116, 116, 116, 116, 116]
Local $arrYPos[19] = [29, 105, 192, 278, 364, 449, 535, 621, 707, 793, 29, 105, 191, 277, 363, 535, 621, 707, 793]

While 1
    For $i = 0 To UBound($arrShortcutNames) - 1
        _SetShortcutPos($arrShortcutNames[$i], $arrXPos[$i], $arrYPos[$i])
        sleep(100)
    Next
    Sleep(1000)
WEnd

Func _SetShortcutPos($Name = "", $X = 0, $Y = 0)
    Local $Desktop = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]")
    Local $Pos = _GUICtrlListView_FindInText($Desktop, $Name)
    _GUICtrlListView_SetItemPosition($Desktop, $Pos, $X, $Y)
EndFunc

Thank you!

Link to comment
Share on other sites

;Put on top of your script the follow command, to avoid running it multiple times!
;You probably also want to test whether the script is already running to avoid running it multiple times!
;If _Singleton(@ScriptName, 1) = 0 Then Exit
#include <Misc.au3>
_Singleton(@ScriptName)

Edit: something similar
210280-_guictrllistview_findintext-and-windows-10/#comment-1518544

Edited by ioa747
correction

I know that I know nothing

Link to comment
Share on other sites

  • Solution
3 hours ago, ioa747 said:
If _Singleton(@ScriptFullPath, 1) = 0 Then Exit

Using @ScriptFullPath as the _Singleton()'s occurrence name will not work because it will contain one or more prohibited backslashes (\), which will make the _Singleton() function fail and return 0.  That syntax would exit upon every invocation.  If you want to use a macro, then @ScriptName should be unique enough.  If all you want the duplicate process to do when launched is exit, then you just need:

#Include <misc.au3>

_Singleton(@ScriptName)

You only need to set $iFlag to 1 if you want to capture the fact that a duplicate process has been launched, for example, if you want to show a message box before exiting.  By default ($iFlag = 0), upon executing the function above, the script will immediately exit with an exit code of -1 (if a duplicate script is already running).

 

The example below shows when using the @ScriptFullPath macro, if you were checking the return of _Singleton(@ScriptFullPath, 1) to 0 in order to exit, then it would have exited upon every execution of the function.  When using @ScriptName, only executions after the initial execution would exit.

#include <Misc.au3>

ConsoleWrite("_Singleton(@ScriptFullPath, 1) = 0: " & (_Singleton(@ScriptFullPath, 1) = 0) & @CRLF)
ConsoleWrite("_Singleton(@ScriptFullPath, 1) = 0: " & (_Singleton(@ScriptFullPath, 1) = 0) & @CRLF)
ConsoleWrite(@CRLF)
ConsoleWrite("_Singleton(@ScriptName, 1)     = 0: " & (_Singleton(@ScriptName, 1) = 0) & @CRLF)
ConsoleWrite("_Singleton(@ScriptName, 1)     = 0: " & (_Singleton(@ScriptName, 1) = 0) & @CRLF)

Console output:

_Singleton(@ScriptFullPath, 1) = 0: True
_Singleton(@ScriptFullPath, 1) = 0: True

_Singleton(@ScriptName, 1)     = 0: False
_Singleton(@ScriptName, 1)     = 0: True

 

Edited by TheXman
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...