Sign in to follow this  
Followers 0
kpmwrestler

Autoit n00b needs help

9 posts in this topic

I just started using autoit three days ago, and i am on my second script. I am familiar with 4 different languages, so autoit seemed kind of familiar to me and i caught on pretty quickly.

I decided to make a simple script that would:

1. Check if a config INI file existed, if not, create it (success)

2. Check if a file existed in the path mentioned in the config file, if not, open an inputbox and ask you the name of the new text file (success)

3. Write that name to the config file and create the text file (success)

4. Create a GUI window (success)

5. Create a button (success)

6. Create an edit area (success)

7. When the button is clicked, it will take the text from the edit area and write it to the text file specified in the config file (not working)

I had another problem too: I tried to make an IF statement so that if Cancel in the inputbox (step 2) is pressed, loop until text is entered and OK is clicked.

The last 10 or 15 lines, i started to get confused and i may have not even done that right.

Here's my code:

CODE
#include <GUIConstants.au3>

; Set the settings file as a variable

$settings = "settings.ini"

; Check if settings file exists, if not, create it

If Not FileExists ($settings) Then

IniWrite ($settings, "FileNames", "TextFile", "")

EndIf

; Find the name of the text file

$textfile = IniRead ($settings, "FileNames", "TextFile", "NotFound")

; Check if the text file exists, if not, create it

If Not FileExists ($textfile) Then

$newtextfile = MsgBox (4, "No Text File", "You need to specify the text file. Do you want to do that now?")

If $newtextfile = 7 Then

MsgBox (0, "Terminating", "You must specify a text file before you can write to it! This program will now terminate")

Exit

EndIf

$loop1 = True

Do

$textname = InputBox ("Create Text File", "Enter a name for the new text file that will be created")

If $textname = 0 Then

$loop1 = False

EndIf

Until $loop1 = False

FileWrite ($textname, "")

IniWrite ($settings, "FileNames", "TextFile", $textname)

MsgBox (0, "Please Restart", "Please restart this program to use your new settings")

Exit

EndIf

; Create the main window and focus on it

$window = GUICreate ("Main Window", 300, 200)

GUISwitch($window)

GUISetState (@SW_SHOW)

; Set the input to be the source of text to write to the file

$edit = GUICtrlCreateEdit ("", 50, 75, 150, 125)

; Create the button which will write to the text file when pressed

$writebutton = GUICtrlCreateButton ( "Write...", 0, 0)

; What to do once the button is clicked

$msg = GUIGetMsg()

Select

Case $msg = $writebutton

$text = GUICtrlRead ($edit)

EndSelect

; Write the text from the InputBox into the text file

FileWriteLine ($textfile, $text)

Do

Until GUIGetMsg() = $GUI_EVENT_CLOSE

GUIDelete()

Exit

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You must have your case's/selects inside your loop, like this:

While 1
$Msg=GUIGetMsg()
Switch $Msg
Case $WriteButton
$Text=GUICtrlRead($Edit1)
FileWriteLine ($textfile, $text)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

And...PS: You cant use FileWriteLine() with a ini! Well, you can, but it sort of sucks...use IniWrite()! Hope this helps you out, if you need anymore help, I will rewrite your code using my awesome amazing skillz! :)

Edited by Swift

Share this post


Link to post
Share on other sites

Awesome! Thanks. What about this If statement?

If Not FileExists ($textfile) Then

$newtextfile = MsgBox (4, "No Text File", "You need to specify the text file. Do you want to do that now?")

If $newtextfile = 7 Then

MsgBox (0, "Terminating", "You must specify a text file before you can write to it! This program will now terminate")

Exit

EndIf

$loop1 = True

Do

$textname = InputBox ("Create Text File", "Enter a name for the new text file that will be created")

If $textname = 0 Then

$loop1 = False

EndIf

Until $loop1 = False

FileWrite ($textname, "")

IniWrite ($settings, "FileNames", "TextFile", $textname)

MsgBox (0, "Please Restart", "Please restart this program to use your new settings")

Exit

EndIf

The inputbox never closes, i'm stuck in a loop

Share this post


Link to post
Share on other sites

Hi,

#include <GUIConstants.au3>

; Set the settings file as a variable
Global $settings = @ScriptDir & "\settings.ini"

; Check if settings file exists, if not, create it
If Not FileExists ($settings) Then
    IniWrite ($settings, "FileNames", "TextFile", "")
EndIf

; Find the name of the text file
$textfile = IniRead ($settings, "FileNames", "TextFile", "NotFound")

; Check if the text file exists, if not, create it
If Not FileExists (@ScriptDir & "\" & $textfile & ".txt") Then
    $newtextfile = MsgBox (4, "No Text File", "You need to specify the text file. Do you want to do that now?")
    If $newtextfile = 7 Then
        MsgBox (0, "Terminating", "You must specify a text file before you can write to it! This program will now terminate")
        Exit
    EndIf
    Do
        $textname = InputBox ("Create Text File", "Enter a name for the new text file that will be created")
        If $textname <> "" Then 
           FileWrite(@ScriptDir & "\" & $textname & ".txt", "")
           IniWrite ($settings, "FileNames", "TextFile", $textname)
           $textfile = $textname
        EndIf   
    Until FileExists(@ScriptDir & "\" & $textname & ".txt")
EndIf

$window = GUICreate ("Main Window", 300, 200)
$edit = GUICtrlCreateEdit ("", 70, 10, 220, 125)
$writebutton = GUICtrlCreateButton ( "Write...", 5, 10, 60, 20)
GUISetState (@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit        
        Case $writebutton
            FileWriteLine (@ScriptDir & "\" & $textfile & ".txt", GUICtrlRead ($edit))
    EndSwitch       
WEnd

Cheers

Share this post


Link to post
Share on other sites

WOW! Both of you guys are awesome! The script works perfectly now. Except, smashly, i changed:

IniWrite ($settings, "FileNames", "TextFile", $textname)

and added

IniWrite ($settings, "FileNames", "TextFile", $textname & ".txt")

Thanks to both of you for the quick replies and awesome skillz.

Share this post


Link to post
Share on other sites

WOW! Both of you guys are awesome! The script works perfectly now. Except, smashly, i changed:

IniWrite ($settings, "FileNames", "TextFile", $textname)

and added

IniWrite ($settings, "FileNames", "TextFile", $textname & ".txt")

Thanks to both of you for the quick replies and awesome skillz.

Yay! :)

Share this post


Link to post
Share on other sites

Alright, I need the awesome skillz again.

I want to be able to find how many lines there are in a text file and return it as a number. is that possible?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Course it is!

use this function, it may not be the best, but it returns the number of lines

#Include <File.au3>
Dim $Array
_FileReadToArray("filehere.txt", $Array)
MsgBox(0, "Lines", $Array[0])

Enjoy!

EDIT: Forgot include

Edited by R6V2

Share this post


Link to post
Share on other sites

Great, it worked. If I compiled a script as an executable but I had things like #Include <File.au3> in my code, would File.au3 be included inside the executable or something, or would I need to put it in the same folder as the .exe?

Share this post


Link to post
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
Sign in to follow this  
Followers 0