Jump to content

Include a user selected script


Recommended Posts

I have a main script that includes he GUI and all the functions. I have a separate "configuration" script that holds all the variable definitions that users would want to configure often. I want to be able to have multiple "configuration" scripts, that users through the GUI can select to load that configuration.

Ideally I'd like to have something like this:

Func LoadCharacterSettings()
$setupAu3Path = FileOpenDialog ( "Select Configuration Au3 file (Default: Setup.au3)", @Scriptdir, "Autoit Scripts (*.au3)" , 3,"Setup.au3")
If @error OR FileExists($setupAu3Path) = 0 Then
    msgbox(0x40000,"Error","File does not exsist")
    Return
Else
    #include $setupAu3Path
    msgbox(0x40000,"Settings Loaded","Current Settings Loaded for character: " & $CharacterName)
EndIf
    
EndFunc

Of course you can not use a variable in an include statement, so I'm looking for a creative way to load an au3 on demand. I don't want to use an ini file, becuause there are massive amounts of needed comments.

Link to comment
Share on other sites

Form1: AutoIt3.exe [/ErrorStdOut] [/AutoIt3ExecuteScript] file [params ...]

Execute an AutoIt3 Script File

/ErrorStdOut Allows to redirect fatal error to StdOut which can be captured by an application as Scite editor. This switch can be used with a compiled script.

To execute a standard AutoIt Script File 'myscript.au3', use the command:

'AutoIt3.exe myscript.au3'

Form2: Compiled.exe [/ErrorStdOut] [params ...]

Execute an compiled AutoIt3 Script File produced with Aut2Exe.

Form3: Compiled.exe [/ErrorStdOut] [/AutoIt3ExecuteScript file] [params ...]

Execute another script file from a compiled AutoIt3 Script File. Then you don't need to fileinstall another copy of AutoIT3.exe in your compiled file.

Form4: AutoIt3.exe [/ErrorStdOut] /AutoIt3ExecuteLine "command line"

Execute one line of code.

To execute a single line of code, use the command:

Run(@AutoItExe & ' /AutoIt3ExecuteLine "MsgBox(0, ''Hello World!'', ''Hi!'')"')

The tray icon will not be displayed when using /AutoIt3ExecuteLine

NOTE: Correct usage of single- and double- quotation marks is important, even double single.

Link to comment
Share on other sites

Yeah I'm not sure if that is going to work. the external au3 script redefines variables in the main script. So I'm really looking to change a bunch of variables on the fly from an external au3 file.

The main script already defines the variables, and loads them from an external au3 file when the script starts all from an #include command. However I'm looking to redefine those variables on the fly from a different user selected(via a fileopendialog() command) au3 file, that would include all the same variables but with different values.

Link to comment
Share on other sites

Ok I ran into a snag...

I created these functions in my main script:

func ParseSetupau3($setupAu3Path)
    
    IF FileExists($setupAu3Path) = 0 then return 0 
    
    $i = 1
    $commentStartLine = 0
    While 1
        $lineread = FileReadLine($setupAu3Path, $i)
        If @error = -1 Then ExitLoop ;Read last line
        
        IF Stringinstr($lineread,"#comments-start",2) or Stringinstr($lineread,"#cs",2) then ;line starts a comment section
            $commentStartLine = $i
            $i = $i + 1
            ContinueLoop
        EndIf
        IF $commentStartLine >0 then ;Current lines being read are still in comments
            IF Stringinstr($lineread,"#comments-end",2) or Stringinstr($lineread,"#ce",2) then ;line ends a comment section
                $commentStartLine = 0
                
            EndIf
            
            IF $commentStartLine >0 then ;if comments haven't ended yet keep looping
                $i = $i + 1
                ContinueLoop
            EndIf
            
        EndIf
        
        $PositionOfEndOfVariable = 0 ;Initialize variable
        $VariableIsInQuotes = 0 ;Initialize variable
        $VariablePosition = StringInStr($lineread,"$",2)
        IF $VariablePosition > 0 then ;$ found
            
            $CommentPosition = StringInStr($lineread,";",2)
            IF $CommentPosition > 0 AND $CommentPosition < $VariablePosition then ;line has a ";" comment symbol in i, but its not in it
                    $i = $i + 1
                    ContinueLoop
            EndIf
                
                    $PositionOfEqualSign = StringinStr($lineread,"=",2)
                
                    $VariableName = Stringmid($lineread, $VariablePosition,$PositionOfEqualSign-$VariablePosition)
                    
                    $FirstNonWhiteCharacter = FindFirstNonWhiteSpaceInString($lineread, $PositionOfEqualSign +1 )
                
                    $PositionOfStartOfVariable = $FirstNonWhiteCharacter
                    IF stringmid($lineread,$PositionOfStartOfVariable,1) = """" or stringmid($lineread,$PositionOfStartOfVariable,1) = "'" then
                        $VariableIsInQuotes = 1
                        $PositionOfStartOfVariable = $PositionOfStartOfVariable +1 ;Check to see if the start of the variable is a quotation
                    EndIf
                    
                    Select
                        Case $VariableIsInQuotes = 0
                            $Variable = stringmid($lineread,$PositionOfStartOfVariable,Stringlen($lineread))
                                $WhiteSpacePosition = STringinstr($Variable," ")
                                $CommentPosition = Stringinstr($Variable, ";") ;Checks if there is a comment at the end of the string
                            
                                If $WhiteSpacePosition < $CommentPosition AND $WhiteSpacePosition < Stringlen($Variable) then
                                    $PositionOfEndOfVariable = $WhiteSpacePosition - 1
                                ElseIf $CommentPosition < $WhiteSpacePosition AND $CommentPosition < Stringlen($Variable) then
                                    $PositionOfEndOfVariable = $CommentPosition - 1
                                Else
                                    
                                    $PositionOfEndOfVariable = Stringlen($Variable)
                                EndIf
                                
                            $Variable = Stringmid($Variable,1, $PositionOfEndOfVariable)
                            
                        Case $VariableIsInQuotes = 1
                            
                            IF $VariableIsInQuotes = 1 then ;Find the end of the variable based on quotes
                                $PositionOfEndOfVariable = stringinstr($lineread,"""",2,1,$PositionOfStartOfVariable)
                                IF $PositionOfEndOfVariable = 0 Then ;Its not a double quote, find it using single quotes
                                    $PositionOfEndOfVariable = stringinstr($lineread,"'",2,1,$PositionOfStartOfVariable)
                                EndIf
                                IF $PositionOfEndOfVariable = 0 then
                                    msgbox(0,"Error","Could not find end of variable on line: " & $i)
                                    return 0
                                EndIf
                            EndIf
                            
                            $variable = Stringmid($lineread, $PositionOfStartOfVariable, $PositionOfEndOfVariable - $PositionOfStartOfVariable)
                            
                    endselect
                    msgbox(0,"",$VariableName & "=" & $Variable)
                    $VariableName = $Variable
    
        EndIf       
    
        $i = $i + 1
    WEnd    
    return 1
EndFunc
;------------------------------------------------------------------------
Func FindFirstNonWhiteSpaceInString($SearchString, $PositionToStart)
    ;Returns the position in the search String that ist he first non white space
    ;Returns 0 if doesn't find anything
    $j = 0
    For $i = 1 to Stringlen($SearchString) - $PositionToStart
        ;Loop To Find the start of the variable by finding the first non-white space after $Position of Equal Sign
        $WhiteSpaceCheck = StringIsSpace (Stringmid($SearchString,$PositionToStart +$j,1))
        IF $WhiteSpaceCheck = 0 Then
            Return $PositionToStart + $j
        EndIf
        $j = $j + 1
        
    Next
    Return 0 
EndFunc
;------------------------------------------------------------------------

When you feed Func: ParseSetupau3($setupAu3Path) a path to an au3 file it will read it line for line skipping any and all comments. But I'm having trouble actually re-assigning the variables.

On the line: " $VariableName = $Variable", this is where it would assign the new variable. Unfortunatly stored in $VariableName is just that..something that looks like: "$CharacterName".

How would you suggest I actually reassign the variables in my main script?

Link to comment
Share on other sites

Wow, I've never seen that function...

Anyway, I tested it before work today and it works beautifully. I'll clean up the code, put some more comments, add a comment header, and post the finished function in example scripts.

Thanks for your assistance Richard.

Link to comment
Share on other sites

Wow, I've never seen that function...

Anyway, I tested it before work today and it works beautifully. I'll clean up the code, put some more comments, add a comment header, and post the finished function in example scripts.

Thanks for your assistance Richard.

This is what INI, XML, and databases are designed for. You don't want users to be able to modify an entire script that gets included with an application, this is a security problem. This is why we have configuration files.

Link to comment
Share on other sites

I have an ini for saving some data used by my main script like waypoints(this is a bot for LOTRO). However since I havn't yet included a way for users to change their settings via the GUI(like drop down menus and such), I have them edit a separate au3 called Setup.au3. This Au3 only defines variables. I didn't want to use an ini file because I wanted to load it up with comments, which you can't do as nicely in an ini file.

This function or rather the one I will post later tonight, shouldn't mess up on a function call, all it does is read the script line by line looking for "$", then extracting the variable name and variable from it, while ignoring comments.

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