is there a way in autoit script to find if current

2019-07-21 16:24发布

I mean something like get_included_files(); in php or Error().stack; in javascript or $BASH_SOURCE array in bash ?

I've only found in macros ( https://www.autoitscript.com/autoit3/docs/macros.htm ) @ScriptFullPath and @ScriptName.

2条回答
Bombasti
2楼-- · 2019-07-21 16:51

You can implement the functionality by using a global variable. Say $includeDepth. $includeDepth should be incremented by 1 before any #include and decremented by 1 after it. If $includeDepth is 0, then the code is not running as part of an #include. For example:

Global $includeDepth
$includeDepth+= 1
#include <MyScript1.au3>
#include <MyScript2.au3>
$includeDepth-= 1

; Declarations, functions, and initializations to be run in both "include" and "non-include" modes

If $includeDepth <= 0 Then
    ; Code for when in "non-include" mode
EndIf

You need to be very consistent in this usage, though. However, as long as library #includes don't include your own scripts (or scripts where this checking is done), there is no need to modify them. From this point of view, it's also not necessary to increment/decrement $includeDepth while including library #includes. However, it doesn't hurt and it reinforces the practice.

查看更多
等我变得足够好
3楼-- · 2019-07-21 17:03

Yes there is a way. Check if @ScriptName matches the name of the current script. If it doesn't your script has been included in something else. I use this method to run a unit test if an included script is run as a stand alone script. I add the following code to the end of included_script.au3...

; unit test code
if @ScriptName == "included_script.au3" Then
    MsgBox(0,"Unit Test","Running unit test...",3)
    test()
    exit
endif

func test()
    ; no test defined
EndFunc

When included in a "main.au3" file, the @ScriptName will be set to "main.au3" and it will not run test()

查看更多
登录 后发表回答