Jump to content

#include $variablename


ankitstr
 Share

Recommended Posts

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

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

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 by Champak
Link to comment
Share on other sites

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:

Global $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 by ankitstr

[topic="80940"] AutoScript Creator, Script to automate anything![/topic]

Link to comment
Share on other sites

Here is one way to do this:

#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
EndFunc

The 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_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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
 
AutoIt_Icon_small.pngExamples: 
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 AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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 by ankitstr

[topic="80940"] AutoScript Creator, Script to automate anything![/topic]

Link to comment
Share on other sites

@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

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? :)

Global $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_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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
 
AutoIt_Icon_small.pngExamples: 
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 AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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 by Zedna
Link to comment
Share on other sites

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

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

Yes, 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_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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
 
AutoIt_Icon_small.pngExamples: 
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 AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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