Jump to content

Saving A File


 Share

Recommended Posts

I was experimenting in with creating a 'Windows Notepad' but, It always saves empty text.

#include <GUIConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Untitled - Notepad", 634, 470, 196, 153, BitOR($WS_MAXIMIZEBOX, _ 
$WS_MINIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_SYSMENU,$WS_CAPTION, _ 
$WS_OVERLAPPEDWINDOW,$WS_TILEDWINDOW,$WS_POPUP,$WS_POPUPWINDOW,$WS_GROUP, _ 
$WS_TABSTOP,$WS_BORDER,$WS_CLIPSIBLINGS))
Global $Edit1 = GUICtrlCreateEdit("", 0, 0, 633, 449)
$MenuItem1 = GUICtrlCreateMenu("File")
$MenuNew=GUICtrlCreateMenuItem("New   Ctrl+N", $MenuItem1)
$MenuOpen=GUICtrlCreateMenuItem("Open...    Ctrl+O", $MenuItem1)
$MenuSave=GUICtrlCreateMenuItem("Save   Ctrl+S", $MenuItem1)
$MenuSaveAs=GUICtrlCreateMenuItem("Save As...", $MenuItem1)
GUICtrlCreateMenuItem("", $MenuItem1)
$MenuPageSetup=GUICtrlCreateMenuItem("Page Setup", $MenuItem1)
$MenuPrint=GUICtrlCreateMenuItem("Print...  Ctrl+P", $MenuItem1)
GUICtrlCreateMenuItem("", $MenuItem1)
$MenuExit=GUICtrlCreateMenuItem("Exit", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("Edit")
$MenuItem3 = GUICtrlCreateMenu("Format")
$MenuItem4 = GUICtrlCreateMenu("View")
$MenuItem5 = GUICtrlCreateMenu("Help")
GUISetState(@SW_SHOW)
$SaveVar=0
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuNew
            GUICtrlSetData($Edit1, "")
        Case $MenuOpen
            _OpenAndLoadFile()
        Case $MenuSave
            If $SaveVar = 1 Then
            _SaveFile()
        Else
            _ChooseToSaveFile(GUICtrlRead($Edit1))
        EndIf
        Case $MenuSaveAs
            _ChooseToSaveFile(GUICtrlRead($Edit1))
    EndSwitch
WEnd

Func _OpenAndLoadFile()
    $File=FileOpenDialog("Choose A File", "", "")
        If Not @error Then
            Dim $FileArray
            _FileReadToArray($File, $FileArray)
            If IsArray($FileArray) Then
                GUICtrlSetData($Edit1, "")
                For $Arrayi=1 To $FileArray[0]
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $FileArray[$Arrayi]&@CRLF)
                Next
            EndIf
        EndIf
EndFunc
Func _ChooseToSaveFile($_FromWhere)
    $FileInfo = FileSaveDialog("Save Your File...", "", "Text Files (*.txt)")
    If Not @error Then
        $_Handle = FileOpen($FileInfo&".txt", 1)
        If FileExists($FileInfo) Then
            If MsgBox(4, "Notepad", "Would you like to replace the file?") = 6 Then
                FileClose($_Handle)
                FileDelete($FileInfo)
                $_Handle = FileOpen($FileInfo&".txt", 1)
                FileWrite($_Handle, $_FromWhere)
                FileClose($_Handle)
            EndIf
    Else
            FileClose($_Handle)
        $__Handle = FileOpen($FileInfo&".txt", 1)
        RegWrite("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "SavedFile", "REG_SZ", "1")
        $Tit=StringSplit($FileInfo, "\")
        $Titl = $Tit[$tit[0]]
        WinSetTitle($Form1, "", $Titl & " - Notepad")
        FileWrite($__Handle, $_FromWhere)
    EndIf
EndIf
EndFunc
Link to comment
Share on other sites

I was experimenting in with creating a 'Windows Notepad' but, It always saves empty text.

#include <GUIConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Untitled - Notepad", 634, 470, 196, 153, BitOR($WS_MAXIMIZEBOX, _ 
$WS_MINIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_SYSMENU,$WS_CAPTION, _ 
$WS_OVERLAPPEDWINDOW,$WS_TILEDWINDOW,$WS_POPUP,$WS_POPUPWINDOW,$WS_GROUP, _ 
$WS_TABSTOP,$WS_BORDER,$WS_CLIPSIBLINGS))
Global $Edit1 = GUICtrlCreateEdit("", 0, 0, 633, 449)
$MenuItem1 = GUICtrlCreateMenu("File")
$MenuNew=GUICtrlCreateMenuItem("New   Ctrl+N", $MenuItem1)
$MenuOpen=GUICtrlCreateMenuItem("Open...    Ctrl+O", $MenuItem1)
$MenuSave=GUICtrlCreateMenuItem("Save   Ctrl+S", $MenuItem1)
$MenuSaveAs=GUICtrlCreateMenuItem("Save As...", $MenuItem1)
GUICtrlCreateMenuItem("", $MenuItem1)
$MenuPageSetup=GUICtrlCreateMenuItem("Page Setup", $MenuItem1)
$MenuPrint=GUICtrlCreateMenuItem("Print...  Ctrl+P", $MenuItem1)
GUICtrlCreateMenuItem("", $MenuItem1)
$MenuExit=GUICtrlCreateMenuItem("Exit", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("Edit")
$MenuItem3 = GUICtrlCreateMenu("Format")
$MenuItem4 = GUICtrlCreateMenu("View")
$MenuItem5 = GUICtrlCreateMenu("Help")
GUISetState(@SW_SHOW)
$SaveVar=0
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuNew
            GUICtrlSetData($Edit1, "")
        Case $MenuOpen
            _OpenAndLoadFile()
        Case $MenuSave
            If $SaveVar = 1 Then
            _SaveFile()
        Else
            _ChooseToSaveFile(GUICtrlRead($Edit1))
        EndIf
        Case $MenuSaveAs
            _ChooseToSaveFile(GUICtrlRead($Edit1))
    EndSwitch
WEnd

Func _OpenAndLoadFile()
    $File=FileOpenDialog("Choose A File", "", "")
        If Not @error Then
            Dim $FileArray
            _FileReadToArray($File, $FileArray)
            If IsArray($FileArray) Then
                GUICtrlSetData($Edit1, "")
                For $Arrayi=1 To $FileArray[0]
                    GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $FileArray[$Arrayi]&@CRLF)
                Next
            EndIf
        EndIf
EndFunc
Func _ChooseToSaveFile($_FromWhere)
    $FileInfo = FileSaveDialog("Save Your File...", "", "Text Files (*.txt)")
    If Not @error Then
        $_Handle = FileOpen($FileInfo&".txt", 1)
        If FileExists($FileInfo) Then
            If MsgBox(4, "Notepad", "Would you like to replace the file?") = 6 Then
                FileClose($_Handle)
                FileDelete($FileInfo)
                $_Handle = FileOpen($FileInfo&".txt", 1)
                FileWrite($_Handle, $_FromWhere)
                FileClose($_Handle)
            EndIf
    Else
            FileClose($_Handle)
        $__Handle = FileOpen($FileInfo&".txt", 1)
        RegWrite("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Notepad", "SavedFile", "REG_SZ", "1")
        $Tit=StringSplit($FileInfo, "\")
        $Titl = $Tit[$tit[0]]
        WinSetTitle($Form1, "", $Titl & " - Notepad")
        FileWrite($__Handle, $_FromWhere)
    EndIf
EndIf
EndFunc
Seems to work but there are a few funny things the in _ChooseToSaveFile. You create the handle $_Handle then destroy it so why create it?

You have FileDelete($FileInfo) so the extension is missing.

You have forgotten a FileClose($__Handle) at the end.

Might be worth checking if the user has already added the .txt extension otherwise you will get filename.txt.txt

EDIT: Even slower than usual tonight!

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks Martin.

Also, how would I get the print file dialog? I need it to print the edit, in the font and stuff that's set.

Edit: Also, how about the 'page setup' dialog?

Edited by Swift
Link to comment
Share on other sites

Thanks Martin.

Also, how would I get the print file dialog? I need it to print the edit, in the font and stuff that's set.

Edit: Also, how about the 'page setup' dialog?

I think that can be a bit more difficult. I usually refer people to big_daddy's Word udf in example scripts, especially if you want clever page setups. But then your whole script would have to be rewritten.

There's also _FilePrint.

My print UDF includes a page setup dialog, but there is no automatic page formatting. The "font and stuff" bit can be dealt with ok, but to print on the page you would have to work out how much text fitted on the width and so on. Not so difficult because the UDF has functions to get the text height and width. But to get anything like WYSIWYG would be very difficult and you would certainly need to use RTF. If you don't need WYSIWYG then my udf can print anything. (well, almost anything).

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...