Jump to content

need help with my text editor script please.


Recommended Posts

Hello,

so i made this script:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("notepad by g3mini", 501, 501, 258, 127, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 500, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 500, 21)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Form1
        Case $Form1
        Case $Form1
        Case $Form1
        Case $Input1
            $name = GUICtrlRead ($Input1)
        case $edit
            $text = GUICtrlRead ($edit)
            $read = FileRead($name)
            if FileExists($name) then
                FileDelete($name)
            EndIf
            FileWrite($name,$text)
    EndSwitch
WEnd

what i want to do is that whenever $name allready exists it shows the text inside the file.

i figured i could probably do this with putting the $read (line 25) inside the $edit (line 5, instead of the "")

but it gives me an error that $read is not declared yet.

is there any way to make it read the variable $read without flipping the whole script upsidedown?

 

 

ps: what the script does is exactly what notepad would but instead it saves every change you make (when i type anything it instantly saves it so that if my laptop/pc crashes it's saved.)

ps2: it works allready but i just can't re-open files. if anyone could teach me any other way of re-opening a file it would be greatfully appreciated.

 

thanks,

dh

Edited by dragonheard
Link to comment
Share on other sites

Hi dragonheard,

  Have a look at FileRead & FileWrite to save your data like you are suggesting. Still going to be saved to Notepad though.

And to save variable between uses have a look at the INI functions very close to the other 2 in the helpfile.

very straightforward stuff...

Bill

p.s. you posted in developer chat... o:)

Link to comment
Share on other sites

  • Moderators

dragonheard,

 

when i type anything it instantly saves it

Be very careful - what you have just defined is basically a "keylogger". I realise that this is not your original intention, but I would ask you to take great care in how you code this script. Reading and saving the content of the edit when it changes is fine (search the forum for scripts dealing with the $EN_CHANGE message to see how to detect changes) but do not even think of looking for and saving actual keypresses. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

It's a very disk intensive method to use also.

If I type 100 words a minute, I'd not appreciate all those disk operations.

I'd suggest AdlibRegister to check if the control is active and has changed, then save.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

dragonheard,

 

Be very careful - what you have just defined is basically a "keylogger". I realise that this is not your original intention, but I would ask you to take great care in how you code this script. Reading and saving the content of the edit when it changes is fine (search the forum for scripts dealing with the $EN_CHANGE message to see how to detect changes) but do not even think of looking for and saving actual keypresses. ;)

M23

i 'm sorry hahaha it does not record keypresses all it does is act like a notepad wich saves whenever any change is made.

It's a very disk intensive method to use also.

If I type 100 words a minute, I'd not appreciate all those disk operations.

I'd suggest AdlibRegister to check if the control is active and has changed, then save.

it checks if a change is made & then saves it due to the case function and if i make it wait till i stop typing it kind of kills the idea behind it.

Hi dragonheard,

  Have a look at FileRead & FileWrite to save your data like you are suggesting. Still going to be saved to Notepad though.

And to save variable between uses have a look at the INI functions very close to the other 2 in the helpfile.

very straightforward stuff...

Bill

p.s. you posted in developer chat... o:)

thanks :D although i want it to show up in the textbox in the program itself & i don't know where else to post it?

thanks everyone :),

dh

Link to comment
Share on other sites

This should get you started, only the text save part works but you can see how and implement the same logic to write your title to an INI file as suggested.

Very basic....no bells and or whistle's  ;)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("notepad by g3mini", 501, 501, 258, 127, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 500, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 500, 21)
GUISetState(@SW_SHOW)

Local $saveText = FileOpen("saved.txt", 1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $edit
            $text = GUICtrlRead ($edit)
            FileWrite($saveText,$text)

    EndSwitch
WEnd
Link to comment
Share on other sites

i think i should be describing it more as a live editor there are some websites that allow this that you can even open 1 file with multiple users and instantly see what changes are being made.

 

This should get you started, only the text save part works but you can see how and implement the same logic to write your title to an INI file as suggested.

Very basic....no bells and or whistle's  ;)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("notepad by g3mini", 501, 501, 258, 127, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 500, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 500, 21)
GUISetState(@SW_SHOW)

Local $saveText = FileOpen("saved.txt", 1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $edit
            $text = GUICtrlRead ($edit)
            FileWrite($saveText,$text)

    EndSwitch
WEnd

the reason why i make it delete the file first is because i haven't found a command which empties a file first.

if i don't do so it will paste the whole text whenever a change is made behind the existing text.

example:

i type hi

it says:

hi

but when i delete hi and type bla after

it says:

hibla because it just pasts it behind the text allready there.

 

i've tried a if fileeists $name thing, it detects the file but it does not show it's content inside the edit box

which is what i'm trying to accomplish.

thanks for the help :D,

dh

Link to comment
Share on other sites

You can change the Parameters in the FilOpen command to overwrite instead of append  ;-)

Local $saveText = FileOpen("saved.txt", 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents)
Edited by billo
Link to comment
Share on other sites

thanks :) i might try that,

but i still have not found a way to make it show the content in a file if said file already exists

 

File open uses the file that already exists unless it doesnt exixt and then it creates it.

Is that what you mean?

Par 4

The file handle must be closed with the FileClose() function.

A file may fail to open due to access rights or attributes.

The default mode when writing text is ANSI - use the unicode flags to change this. When writing unicode files the Windows default mode (and the fastest in AutoIt due to the least conversion) is UTF16 Little Endian (mode 32).

>>>>>Opening a file in write mode creates the file if it does not exist. Directories are not created unless the correct flag is used.

When reading and writing via the same file handle, the FileSetPos() function must be used to update the current file position.

A file can be read as binary (byte) data by using FileOpen() with the binary flag.
Edited by billo
Link to comment
Share on other sites

that did not work,

it does open the file i think but it still does not show any content it just overwrites everything without showing it's content first.

here's the code at this moment:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("notepad by g3mini", 750, 375, 258, 127, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 750, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 750, 21)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Form1
        Case $Form1
        Case $Form1
        Case $Form1
        Case $Input1
            $name = GUICtrlRead ($Input1)
        case $edit
            $text = GUICtrlRead ($edit)
            $read = FileRead($name)
            if FileExists($name) then
        GUICtrlSetData($edit, FileRead($name))
        FileDelete($name)
EndIf
            FileWrite($name,$text)
    EndSwitch
WEnd
Link to comment
Share on other sites

You cant have both

 

You can change the Parameters in the FilOpen command to overwrite instead of append  ;-)

Local $saveText = FileOpen("saved.txt", 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents)

Its either going to append or overwrite.

if you want something to show up with Johns code you will have to use append.

Link to comment
Share on other sites

Here is a dirty solution.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

$Form1 = GUICreate("notepad by g3mini", 750, 375, 258, 127, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 750, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 750, 21)
$Dummy = GUICtrlCreateDummy()

Local $aAccelKeys[1][2] = [["{SPACE}", $Dummy]]
GUISetAccelerators($aAccelKeys)

GUISetState(@SW_SHOW)

$null = 0
$name = ''

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $name = GUICtrlRead($Input1)
        Case $Dummy
            If _WinAPI_GetFocus() = GUICtrlGetHandle($edit) Then
                GUISetAccelerators($null)
                Send("{SPACE}")
                GUISetAccelerators($aAccelKeys)
                _Work()
            EndIf
    EndSwitch
WEnd

Func _Work()
    $hFile = FileOpen($name, 2)
    FileWrite($hFile, GUICtrlRead($edit))
    FileClose($hFile)
    ConsoleWrite(FileRead($name) & @LF)
EndFunc   ;==>_Work

When you enter the target filename, you must press enter key for $Input1 to register.

EDIT: Forgot to mention, this will save the edit control to disk, each time the space bar is pressed, so most every word, not letter.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm sorry, you kinda lost me there: how do i put John's code and that code together?

 

Append - will always just add whatever you write to the end of whatever you wrote before like a diary or a ledger.

And you can set it up so what you wrote last time is visible when you open your script see post # 13

Overwrite will gve you a fresh clean empty notepad everytime you openFile anewed.

From what I understand you are going for the diary/ledger, or?

 

Bill

Edited by l3ill
Link to comment
Share on other sites

  • 4 weeks later...

sorry it took me so long to reply i was busy for a while,

Here is a dirty solution.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

$Form1 = GUICreate("notepad by g3mini", 750, 375, 258, 127, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$edit = GUICtrlCreateEdit("", 0, 24, 750, 476)
GUICtrlSetFont(-1, 12, 400, 0, "arial")
$Input1 = GUICtrlCreateInput("Filename here.", 0, 0, 750, 21)
$Dummy = GUICtrlCreateDummy()

Local $aAccelKeys[1][2] = [["{SPACE}", $Dummy]]
GUISetAccelerators($aAccelKeys)

GUISetState(@SW_SHOW)

$null = 0
$name = ''

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $name = GUICtrlRead($Input1)
        Case $Dummy
            If _WinAPI_GetFocus() = GUICtrlGetHandle($edit) Then
                GUISetAccelerators($null)
                Send("{SPACE}")
                GUISetAccelerators($aAccelKeys)
                _Work()
            EndIf
    EndSwitch
WEnd

Func _Work()
    $hFile = FileOpen($name, 2)
    FileWrite($hFile, GUICtrlRead($edit))
    FileClose($hFile)
    ConsoleWrite(FileRead($name) & @LF)
EndFunc   ;==>_Work

When you enter the target filename, you must press enter key for $Input1 to register.

EDIT: Forgot to mention, this will save the edit control to disk, each time the space bar is pressed, so most every word, not letter.

i tried to add this,

it does not show the previous content in the gui itself :(, thanks for the idea of making is save on each time a space is being sent though :) might make that a permanent.

Append - will always just add whatever you write to the end of whatever you wrote before like a diary or a ledger.

And you can set it up so what you wrote last time is visible when you open your script see post # 13

Overwrite will gve you a fresh clean empty notepad everytime you openFile anewed.

From what I understand you are going for the diary/ledger, or?

 

Bill

that's correct; what i want it to do is save any change being made to the text

and whenever filename allready exists to view it's content inside the editbox

 

thanks,

dh

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