ankitstr Posted September 20, 2008 Share Posted September 20, 2008 Hi, Since #include "filename" doesn't allow filename to be a variable, I was wondering if there is any workaround to pass a string variable to #include directive, instead of coding every single file to be included. Thanks in advance. [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
Champak Posted September 20, 2008 Share Posted September 20, 2008 No...I believe. Why? Link to comment Share on other sites More sharing options...
Valuater Posted September 20, 2008 Share Posted September 20, 2008 FileInstall() is another route 8) Link to comment Share on other sites More sharing options...
ankitstr Posted September 20, 2008 Author Share Posted September 20, 2008 FileInstall() doesn't like variables either. I am working on a code which includes a particular piece of code based on the function input. But since, I can't use a variable with #include, i am forced to writing each and every single includefile names in the code, which is stupid, long and inflexible. Is there any other workaround for that?? [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
Champak Posted September 20, 2008 Share Posted September 20, 2008 (edited) Either I'm not understanding what you need or I don't think you understand includes. Includes are included in the script from the start of the script to make sure the end user has all the functions the script needs. It has to be this way because it is not like the includes are being polled in the loop based on a function being called, or the end user of a compiled script are going to have all the necessary include files that can be called based on input. Besides that, only the functions being called in the script will be included when you compile your script...I believe. Your other option would be to not include the files and just copy the needed functions from the include files into your script....which is redundant because only the needed functions will be included anyway at compile time. I don't see the tedium of including a few files at the top of your script....are you including every single file? And even so, it is still a simple one time step. Just my opinion. Edited September 20, 2008 by Champak Link to comment Share on other sites More sharing options...
ankitstr Posted September 20, 2008 Author Share Posted September 20, 2008 (edited) well.. that is one of the options, but for my script i wanted it not to load the functions in advance and include them as and when needed, somewhat like dlls but i was thinking to implement #include, since if we do a #include in the middle of the code, it executes that included code and then continues to the rest of the program. But I guess its not possible to use a variable there, so no point wasting time in that direction. Thanks to all you for replying and clearing up my doubts. here is my finished script implementing #includes: expandcollapse popupGlobal $nMsg = 10 Global $lessonArray[$nMsg] For $n=0 To $nMsg-1 $lessonArray[$n]=0 Next Func lesson($RandomNo) $flag=1 For $n = 0 To $nMsg-1 If $lessonArray[$n]=$RandomNo And $lessonArray[$nMsg-1]=0 Then $flag=2 EndIf If $lessonArray[$n]==0 And $flag==1 Then $lessonArray[$n]=$RandomNo $flag=0 Switch $RandomNo Case 1 #include "L1.AU3" Case 2 #include "L2.AU3" Case 3 #include "L3.AU3" Case 4 #include "L4.AU3" Case 5 #include "L5.AU3" Case 6 #include "L6.AU3" Case 7 #include "L7.AU3" Case 8 #include "L8.AU3" Case 9 #include "L9.AU3" Case 10 #include "L10.AU3" Case Else Exit 0 EndSwitch EndIf Next If $flag=1 Then Return 0 Else Return 1 EndIf EndFunc While lesson(Random(1,$nMsg,1)) Sleep(100) WEnd Edited September 20, 2008 by ankitstr [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
MrCreatoR Posted September 20, 2008 Share Posted September 20, 2008 Here is one way to do this:expandcollapse popup#include <GUIConstantsEx.au3> $sIncludeFiles = "MyIncl_1.au3;MyIncl_2.au3;MyIncl_3.au3" _IncludeScripts($sIncludeFiles, False) ;Set $iForCompile (last param) as True to write the includes without removing them $GUI = GUICreate("Test Script", 300, 200) $Button = GUICtrlCreateButton("Button", 20, 40, 60, 20) GUISetState(@SW_SHOW, $GUI) Call("Test") ;Some function from MyIncl_N.au3 While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _IncludeScripts($sIncludeScripts, $iForCompile=False) If @Compiled Then Return ;We can't write to compiled script! Local $sIncludesContent = "", $sCurrInclContent = "" Local $iIncludeMode = StringInStr($CmdLineRaw, "/Include") Local $aSplit_Includes = StringSplit($sIncludeScripts, ";") Local $sReadScript = FileRead(@ScriptFullPath) If $iForCompile And $iIncludeMode Then Return For $i = 1 To UBound($aSplit_Includes)-1 $sCurrInclContent = "#include <" & $aSplit_Includes[$i] & ">" If Not FileExists($aSplit_Includes[$i]) Or _ (Not $iIncludeMode And StringInStr($sReadScript, $sCurrInclContent)) Then ContinueLoop $sIncludesContent &= $sCurrInclContent & @CRLF Next If $sIncludesContent <> "" Then Local $hFOpen = FileOpen(@ScriptFullPath, 2) If Not $iIncludeMode Then FileWrite($hFOpen, $sIncludesContent & $sReadScript) Else FileWrite($hFOpen, StringRegExpReplace($sReadScript, "(?s)\A" & $sIncludesContent & "(\r\n+|)(.*?)$", "\2")) EndIf FileClose($hFOpen) EndIf If $iIncludeMode Then Return Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Include') Exit EndFuncThe idea here is to write temporary includes to the script (can be changed), execute the script again with those includes, and remove them from the script. To compile the script with the includes just set last parameter in the _IncludeScripts() function to True, run once the script, and then you can compile it.Btw, the same can be done for FileInstall() limits Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
ankitstr Posted September 20, 2008 Author Share Posted September 20, 2008 (edited) this is interesting, but can these #includes be added at a line of code, like an interrupt? or in a way that the script doesn't run itself over again, and continue from where we left it after the function call, not just at the top? Thanks for your post, its really helpful. Edited September 20, 2008 by ankitstr [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
Richard Robertson Posted September 20, 2008 Share Posted September 20, 2008 Includes are the same as copying the file right into the executing script where the #include directive is. It will execute any file level code and load any functions contained as well. They aren't meant to be dynamic. Just like #include in C++. It's a static directive. Link to comment Share on other sites More sharing options...
ankitstr Posted September 20, 2008 Author Share Posted September 20, 2008 @richard: well.. would have been nice if they were, anyways, @MrCreator: I was thinking if there was some command to jump to a particular line of code, then that could be manipulated to continue the program from where it left after doing the teporary include. [topic="80940"] AutoScript Creator, Script to automate anything![/topic] Link to comment Share on other sites More sharing options...
MrCreatoR Posted September 20, 2008 Share Posted September 20, 2008 can these #includes be added at a line of code, like an interrupt? or in a way that the script doesn't run itself over again, and continue from where we left it after the function call, not just at the top?To certain line, yes it can. But without runing again no, we can not execute script from specific line. But as i understood, you only need to execute part of code (that included in seperate script) at particular point, so why you not just Execute() it? expandcollapse popupGlobal $nMsg = 10 Global $lessonArray[$nMsg] For $n = 0 To $nMsg - 1 $lessonArray[$n] = 0 Next While Lesson(Random(1, $nMsg, 1)) Sleep(100) WEnd Func Lesson($RandomNo) $flag = 1 For $n = 0 To $nMsg - 1 If $lessonArray[$n] = $RandomNo And $lessonArray[$nMsg - 1] = 0 Then $flag = 2 If $lessonArray[$n] == 0 And $flag == 1 Then $lessonArray[$n] = $RandomNo $flag = 0 $RandomNo = 3 Switch $RandomNo Case 1 To 10 $sReadIncl = FileRead("L" & $RandomNo & ".au3") _ExecuteCode($sReadIncl) Case Else Exit 0 EndSwitch EndIf Next If $flag = 1 Then Return 0 Return 1 EndFunc Func _ExecuteCode($sCode) $aExecuteCode = StringSplit(StringStripCR($sCode), @LF) For $i = 1 To $aExecuteCode[0] If $aExecuteCode[$i] <> "" Then Execute($aExecuteCode[$i]) Next EndFunc Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Zedna Posted September 20, 2008 Share Posted September 20, 2008 (edited) You seem to use #include directive where functions should be used. Make one include file: lessons.au3 with functions L1(), L2(), L3(), ... and then use: #include "lessons.au3" ... Switch $RandomNo Case 1 L1() Case 2 L2() ... Edited September 20, 2008 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Valuater Posted September 20, 2008 Share Posted September 20, 2008 I used a zip file for Welcome to Autoit 1-2-3 to "include" all of the lessons 8) Link to comment Share on other sites More sharing options...
PsaltyDS Posted September 20, 2008 Share Posted September 20, 2008 On the general topic of variables in #include or FileInstall() source lines, remember that has to be parsed and interpreted at COMPILE-TIME, not at RUN-TIME. What if you had a different include file per user and tried: #include @UserName & "_Stuff.au3"Or, perhaps: FileInstall("C:\Temp\" & @UserName & "_Stuff.txt", @ScriptDir & "\Stuff.txt")How would the compiler know who @UserName was supposed to be? (The @ScriptDir above would be parsed at Run-Time and is okay.)I also wrote a more lengthy example with #include for the modular lessons idea here. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
MrCreatoR Posted September 20, 2008 Share Posted September 20, 2008 On the general topic of variables in #include or FileInstall() source lines, remember that has to be parsed and interpreted at COMPILE-TIME, not at RUN-TIMEYes, it would be better...How would the compiler know who @UserName was supposed to be?Well, at run time we just write this name literally to the script (with my first method) . Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team 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