HurleyShanabarger Posted May 28, 2021 Share Posted May 28, 2021 Hello, during development I often work in a script that I include in the main program. The included script has only functions that are called from the main script. When devloping I press often F5 to run the script and when doing this in the include nothing happens. Is there a way to generate an error when I try to run a included file? Only thing I can imagine write now is to declare a variable in the main script and check with IsDeclared in the included file and generate a MsgBox. Any better ideas? Link to comment Share on other sites More sharing options...
Developers Jos Posted May 28, 2021 Developers Share Posted May 28, 2021 3 hours ago, HurleyShanabarger said: Is there a way to generate an error when I try to run a included file? Sure, just add this at the top of the Include file and adapt the "includename.au3" the real name of the include file. if @ScriptName = "includename.au3" then msgbox(0, "NO you ....", " ..... dumbass ... you are doing it again!!!!! ;)") EndIf Jos argumentum 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
JockoDundee Posted May 28, 2021 Share Posted May 28, 2021 Generic One Liner - Insert as first line to any #include files, no changes to main script, context free. If StringLeft(FileReadLine(@ScriptFullPath),47)="If StringLeft(FileReadLine(@ScriptFullPath),47)" Then Exit MsgBox(1,"Cannot Run!", "Exiting...") Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
argumentum Posted May 28, 2021 Share Posted May 28, 2021 (edited) #include-once ;~ #include <DontRunIncludeOnce.au3> ; include this UDF in all your other UDFs. __DontRunIncludeOnce() ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name ..........: __DontRunIncludeOnce ; Description ...: it runs to stop user from waiting on something that will not happen. ; Syntax ........: __DontRunIncludeOnce() ; Parameters ....: None ; Return values .: None ; Author ........: argumentum ; Modified ......: ; Remarks .......: Just include this UDF in your other UDFs. Exit error is set to 3. ; Save this code as "DontRunIncludeOnce.au3" in the path of your includes. ; Related .......: ; Link ..........: https://www.autoitscript.com/forum/index.php?showtopic=205963&view=findpost&p=1482941 ; Example .......: Yes, this is also the example. If you run this UDF it will show what it does. ; =============================================================================================================================== Func __DontRunIncludeOnce() If Not StringRight(@ScriptName, 4) = ".au3" Then Return ;~ ConsoleWrite('+ Func DontRunIncludeOnce()' & @CRLF) Local $n, $aArray = StringSplit(FileRead(@ScriptFullPath, 2048), @CRLF) For $n = 1 To $aArray[0] If StringInStr(StringStripWS($aArray[$n], 8), "#include-once") = 1 Then MsgBox(0, @ScriptName, "oops on:" & @CR & @ScriptName & @CR & @CR & "this is an ""#include"" file", 3) Exit 3 EndIf Next EndFunc ;==>__DontRunIncludeOnce I wasn't going to post. Jos' solution is as good as it gets. But then again, I'm a copy'n'paste coder and just as prone to err. With this, you can just copy and paste #include <DontRunIncludeOnce.au3>. It's easier Edit: This change is thanks to @JockoDundee observation. Edited May 29, 2021 by argumentum better code Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
JockoDundee Posted May 28, 2021 Share Posted May 28, 2021 If StringInStr(FileRead(@ScriptFullPath, 2048), "#include-once") Then What if #include-once is mentioned in comments? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
argumentum Posted May 28, 2021 Share Posted May 28, 2021 hmm, good comment. Is this troll food ? JockoDundee 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
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