Jump to content
Sign in to follow this  
Megadime

How to get name of currently running script

Recommended Posts

Megadime

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.

Share this post


Link to post
Share on other sites
TurionAltec

@ScriptName

Share this post


Link to post
Share on other sites
Megadime

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.

Share this post


Link to post
Share on other sites
dantay9

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

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
Megadime

Thanx, but I don't have a process corresponding to the script because I run the script by right clicking and choosing "Run Script" and not by starting a standalone executable. Is there another way?

Share this post


Link to post
Share on other sites
dantay9

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

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
Megadime

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?

Share this post


Link to post
Share on other sites
dantay9

Oops! Probably should have tested before posting. I just had it lying around so I posted it. I am out of ideas, at least for now. Will post back if I find anything.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites
TurionAltec

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

Share this post


Link to post
Share on other sites
herewasplato

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]

Share this post


Link to post
Share on other sites
rajeshontheweb

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
Sign in to follow this  

×