Jump to content

How to get name of currently running script


Recommended Posts

I'd like to know how I can get the name of the script which is run by AutoIt3.exe. I know I could do it if I compiled the scripts and checked the processes for the name of the script + .exe. But how can I get the name of the running script if I've started it by clicking the right mouse button on the script-au3 and choosing "Run Script". Maybe it would be possible by getting the name of the system tray icon which is started, but I'd like to know if there's a better way.

Link to comment
Share on other sites

You can use something similar to this:

If ProcessExists("Process name") Then
     MsgBox(0, "", "The process exists")
Else
     MsgBox(0, "", "The process doesn't exist")
EndIf
Edited by dantay9
Link to comment
Share on other sites

Here is a way to get the path of the file that created the process. The process name would be "AutoIt3.exe".

Func _WinAPI_ProcessGetFilename($vProcessID, $bFullPath = False)
    ; Not a Process ID? Must be a Process Name
    If Not IsNumber($vProcessID) Then
        $vProcessID = ProcessExists($vProcessID)
        ; Process Name not found (or invalid parameter?)
        If $vProcessID == 0 Then Return SetError(1, 0, "")
    EndIf

    Local $hProcess, $stFilename, $aRet, $sFilename, $sDLLFunctionName

    ; Since the parameters and returns are the same for both of these DLL calls, we can keep it all in one function
    If $bFullPath Then
        $sDLLFunctionName = "GetModuleFileNameEx"
    Else
        $sDLLFunctionName = "GetModuleBaseName"
    EndIf

    ; Get process handle (lod3n)
    Local $hProcess = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'int', BitOR(0x400, 0x10), 'int', 0, 'int', $vProcessID)
    If @error Or Not IsArray($hProcess) Then Return SetError(2, 0, "")

    ; Create 'receiving' string buffers and make the call
    ;If @AutoItUnicode Then
    ; Path length size maximum in Unicode is 32767 (-1 for NULL)
    $stFilename = DllStructCreate("wchar[32767]")
    ; we append 'W' to function names because these are the 'Wide' (Unicode) variants
    $aRet = DllCall("Psapi.dll", "dword", $sDLLFunctionName & 'W', _
            "ptr", $hProcess[0], "ptr", Chr(0), "ptr", DllStructGetPtr($stFilename), "dword", 32767)
    ;Else
    #cs
        ; Path length size maximum otherwise is 260 (-1 for NULL)
        $stFilename=DllStructCreate("char[260]")
        $aRet=DllCall("Psapi.dll","dword",$sDLLFunctionName, _
        "ptr",$hProcess[0],"ptr",Chr(0),"ptr",DllStructGetPtr($stFilename),"dword",260)
        EndIf
    #ce

    ; Error from either call? Cleanup and exit with error
    If @error Or Not IsArray($aRet) Then
        ; Close the process handle
        DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $hProcess[0])
        ; DLLStructDelete()'s:
        $stFilename = 0
        $hProcess = 0
        Return SetError(2, 0, "")
    EndIf

    ;$aRet[0] = size of string copied over, minus null-terminator
    ;$stFilename should now contain either the filename or full path string (based on $bFullPath)
    $sFilename = DllStructGetData($stFilename, 1)

    DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $hProcess[0])
    ; DLLStructDelete()'s
    $stFilename = 0
    $hProcess = 0

    Return SetError(0, 0, $sFilename)
EndFunc   ;==>_WinAPI_ProcessGetFilename
Link to comment
Share on other sites

Thanks again. I've tried it, but the function returns "AutoIt3.exe" when clicking "Run Script" test.au3. Moreover, sorry for not knowing, but, provided it works, how can I get the names of the scripts running using this script?

Link to comment
Share on other sites

I was going to suggest trying something with _Singleton, but I really don't understand how this mutex thing works, so I was going to suggest creating a hidden GUI window with the script name.

#include <GUIConstants.au3>

$Form1 = GUICreate("Autoit3:"& @ScriptName, 237, 104, 193, 125)
GUISetState(@SW_hide)

While 1
    sleep(2000)
WEnd

Save that as several au3 files with different names, and execute.

Optionally just define the GUI title with something unique for each script, then it will return the same regardless of whether it's an au3, exe, or renamed.

Next execute this script

#include <GUIConstants.au3>
#include <array.au3>
$Form1 = GUICreate("Autoit3:"& @ScriptName, 237, 104, 193, 125)
GUISetState(@SW_hide)

$array=WinList("Autoit3:")
_ArrayDisplay($array)

;_ArraySearch($array)       ;You can use Arraysearch to search for a specific occurrence, 
;Or just use Winlist or Winexist or WinGetHandle to begin with
While 1
    sleep(2000)
WEnd
Edited by TurionAltec
Link to comment
Share on other sites

Hey, thanx for the quick answer! One more thing, is there a way to get all scripts which are running? I have a script which is always running and this script needs to know which other scripts are started.

Have each script name its hidden window something unique. See here for the code:

http://www.autoitscript.com/forum/index.php?showtopic=103366&view=findpost&p=733081

Then have the script that is always running look for those windows with a series of "If WinExists" lines.

[size="1"][font="Arial"].[u].[/u][/font][/size]

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...