Jump to content

Find-Replace Editing Program from AutoIt


Recommended Posts

i'm one with no programming backgrounds. i've read a lot from the forum posts,

yet, this obstacle is clearly beyond me. all i could accomplish so far is

to automate some tasks.

i'm involved in a scripture translation work--accent marking, to be precise.

i know that the kind of tool i'm about to ask for is possible through autoit

but i just can't manage to code it myself. this is the only place i know of

where i may make requests of this kind.

allow me:

a1. the program should be visible (always on top) like orbit downloader's

drop box (because i have disabled my windows' tray),

OR minimizable to the taskbar

OR TSR-like.

a2. my work platform is ms office word. but it ought to work elsewhere too.

a3. i'm dealing with vowels only.

the main task:

b1. using a hotkey, find a vowel starting from the cursor point on, highlight it.

b2. determine its case.

b3. replace it (case-sensitive) with a hotkey.

b4. find the next vowel.

replace it with what? this is the detailed (and hard) part (i guess):

c1. there are basically 6 symbols--3 for long vowels; 3 for short.

c2. hit F6, for instance, to insert this diacritical sign: Â (circumflex)

F7 for À (grave)

F8 for Á (acute)

etc.

for maximum ease of use, all functions should be possible through

assignable/customizable hotkeys (single-key hotkeys should be fine).

if somebody could write it out for me, that'd be more than i could hope for.

else, i'd love to get tips/directions so that i can attempt the coding myself.

the benefits possible in terms of output is beyond any reckoning.

thank you. hope i won't get flamed for such a request.

Edited by bahtea
Link to comment
Share on other sites

  • Moderators

bahtea,

Welcome to the AutoIt forum. :(

We do not normally do requests, but this looked like fun. ;) Besides, "the benefits possible in terms of output is beyond any reckoning" means I will certainly count it as my "good deed" for the day - even if I cannot imagine what these benefits might be. :)

This script does not meet all your requirements, but goes a fair way towards them:

- a1-3: It will stay on top because it is a stand-alone app which works with text files. It does NOT work inside Word or any other app.

- b1-4: You use a Start button to begin rather than a HotKey - other than that you have them all.

- c1-2: I am not too sure what you want here - I have assumed that all vowels can be replaced by their case equivalent with any of the 3 accents or left alone. The 4 HotKeys to do this are:

F5 - Skip this vowel
F6 - Add a circumflex accent to the existing vowel
F7 - Add a grave accent to the existing vowel
F8 - Add an acute accent to the existing vowel

So, how to use it:

Save the main text you wish to edit as a .txt file in the same folder as the script. Run the script - it will ask you to select the file to edit. Once the file is loaded, place the cursor at the point from which you wish the search to start and press "Start". The script highlights the vowels and you use the HotKeys F5-F8 to skip/change them. Pressing "Save" saves the current text, overwriting the existing file, and asks if you want to exit. If not, then the file is reloaded and a new start position can be set. When there are no more vowels to find, the script tells you and saves the file.

#include <GUIConstantsEx.au3>
#Include <GUIEdit.au3>
#Include <ScrollBarConstants.au3>
#Include <WindowsConstants.au3>

Global $sText, $iActChar = 1, $iFirstChar = 0, $sCurrChar = ""

; Set HotKeys
HotKeySet("{F5}", "_Skip")
HotKeySet("{F6}", "_Circumflex")
HotKeySet("{F7}", "_Grave")
HotKeySet("{F8}", "_Acute")

; Choose a file to edit
Global $sFile = FileOpenDialog("Select file to edit", @ScriptDir, "Files (*.txt)", 3)

; Create a GUI
Global $hGUI = GUICreate("Test", 1000, 800)

Global $hEdit = GUICtrlCreateEdit($sText, 10, 10, 980, 740)
GUICtrlSetFont(-1, 14, Default, Default, "Courier New")

Global $hLabel_Line = GUICtrlCreateLabel("",  10, 770, 100, 20)
Global $hLabel_Col  = GUICtrlCreateLabel("", 120, 770, 100, 20)

Global $hStart_Button = GUICtrlCreateButton("Start", 810, 760, 80, 30)
Global $hSave_Button = GUICtrlCreateButton("Save", 910, 760, 80, 30)
Global $hDummy = GUICtrlCreateDummy()

GUISetState()

; Load file and set defaults
_Load_File()
_GUICtrlEdit_SetSel($hEdit, 0, 0)

; Wait until start position set
_Start_Select()

; Run through file
While 1

    ; Look for next vowel
    StringRegExp($sText, "(?i)[a|e|i|o|u]", 1, $iActChar)
    ; If none left - save file and exit
    If @error Then
        MsgBox(64, "End of file", "There are no more vowels to change")
        _Write_File()
        Exit
    EndIf
    ; Get position for next match
    $iActChar = @extended
    ; Read vowel found
    Global $sCurrChar = StringMid($sText, $iActChar - 1, 1)
    ; Set selection to highlight vowel
    _GUICtrlEdit_SetSel($hEdit, $iActChar - 2, $iActChar - 1)

    ; Show line and column at bottom of GUI
    Global $iActLine = _GUICtrlEdit_LineFromChar($hEdit)
    ; Show line
    GUICtrlSetData($hLabel_Line, "Line: " & $iActLine + 1)
    ; Calculate and show column
    GUICtrlSetData($hLabel_Col, "Col : " & $iActChar - _GUICtrlEdit_LineIndex($hEdit, $iActLine) - 1)

    ; Ensure selected vowel is always in view
    While _GUICtrlEdit_GetFirstVisibleLine($hEdit) + 15 < $iActLine
        ; Skip it too close to end
        If _GUICtrlEdit_GetLineCount($hEdit) - $iActLine < 20 Then ExitLoop
        ; Scroll down 1 line
        _GUICtrlEdit_Scroll($hEdit, $SB_LINEDOWN)
    WEnd

    While 1

        Switch GUIGetMsg()
            ; [X]
            Case $GUI_EVENT_CLOSE
                Exit
            ; Used to move on when a char is changed
            Case $hDummy
                ExitLoop
            ; Save file
            Case $hSave_Button
                ; Write the changed file
                _Write_File()
                ; Ask about exit
                If MsgBox(36, "Exit?", "Do you want to exit now?") = 6 Then Exit
                ; Reread file and reset all defaults
                _Load_File()
                GUICtrlSetState($hEdit, $GUI_FOCUS)
                GUICtrlSetState($hStart_Button, $GUI_ENABLE)
                ; Select start position - default is last selection
                _Start_Select()
                ExitLoop

        EndSwitch

    WEnd

WEnd

Func _Skip()

    ; Activate the dummy to break out of the loop
    GUICtrlSendToDummy($hDummy)

EndFunc

Func _Circumflex()

    ; Call replace function with offset of 2
    _Replace_Char(2)

EndFunc

Func _Grave()

    ; Call replace function with offset of 0
    _Replace_Char(0)

EndFunc

Func _Acute()

    ; Call replace function with offset of 1
    _Replace_Char(1)

EndFunc

Func _Replace_Char($iAdd)

    Local $sNewChar
    ; Get ASCII value of found vowel
    Local $iAscCurrChar = Asc($sCurrChar)
    ; Select replacement depending on case and letter
    Switch $iAscCurrChar
        ; A, a
        Case 65, 97
            $sNewChar = Chr($iAscCurrChar + 127 + $iAdd)
        ; E, I, O, e, i, o
        Case 69, 73, 79, 101, 105, 111
            $sNewChar = Chr($iAscCurrChar + 131 + $iAdd)
        ; U, u
        Case 85, 117
            $sNewChar = Chr($iAscCurrChar + 132 + $iAdd)
    EndSwitch
    ; Replace the vowel in the text
    $sText = StringReplace($sText, $iActChar - 1, $sNewChar, 1)
    ; Reload the text into teh edit and reset the selection
    GUICtrlSetData($hEdit, $sText)
    _GUICtrlEdit_SetSel($hEdit, $iActChar - 2, $iActChar - 1)
    ; Activate the dummy to break out of the loop
    GUICtrlSendToDummy($hDummy)

EndFunc

Func _Start_Select()

    While 1
        Switch GUIGetMsg()
            ; [X]
            Case $GUI_EVENT_CLOSE
                Exit
            ; Start position set
            Case $hStart_Button
                GUICtrlSetState($hEdit, $GUI_FOCUS)
                ; Get start position
                $aArray = _GUICtrlEdit_GetSel($hEdit)
                ; Set SRE offset
                $iActChar = $aArray[0]
                GUICtrlSetState($hStart_Button, $GUI_Disable)
                ExitLoop
        EndSwitch
    WEnd

EndFunc

Func _Load_File()

    ; Read and load text
    $sText = FileRead($sFile)
    GUICtrlSetData($hEdit, $sText)
    ; Set defaults
    $iActChar = 1
    $iFirstChar = 0
    $sCurrChar = ""

EndFunc

Func _Write_File()

    ; Open and overwrite existing file
    Local $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, GUICtrlRead($hEdit))
    FileClose($hFile)

EndFunc

I hope it is somewhere near what you wanted. :)

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

salute, Melba23! this is a real pro at work.

you're so very right, this script does not meet all my requirements, but goes a fair way towards them:

1. the editing point does not really follow the cursor's position. it only takes up from its last position.

2. there is no word wrap. the current slider's behaviour is too cumbersome to put to heavy use. to edit any vowel beyond the current page boundary requires manual slider dragging.

3. ability to work with other text editors/word processors, esp ms office word will take it to the next level.

whatever, your work has provided one with the platform on which to start building. i'll try to customise it to fit my needs and IF i succeed will come back to post.

my gratitude again!

Link to comment
Share on other sites

  • Moderators

bahtea,

Glad you like the script. :(

Taking your points in turn:

- 1. Once you have started changing vowels, you moving the cursor has no effect - the hightlight just moves to the next vowel when you have pressed a HotKey F5-F8. If you want to move to another section of the text, press "Save" and do not exit when asked. Then you can reposition the cursor where you wish to recommence and press "Start" to begin changing vowels again. You did ask to move from vowel to vowel in your points b1-b4, by the way. :)

- 2. This new script now scrolls horizontally so you should not have to move the horizontal scroll bar when changing vowels. :)

- 3. Not going there! ;)

Here is the new version for you to try:

#include <GUIConstantsEx.au3>
#Include <GUIEdit.au3>
#Include <ScrollBarConstants.au3>
#Include <WindowsConstants.au3>

Opt("TrayIconDebug", 1)

Global $sText, $iActChar = 1, $iFirstChar = 0, $sCurrChar = "", $iScrollMoves = 0

; Set HotKeys
HotKeySet("{F5}", "_Skip")
HotKeySet("{F6}", "_Circumflex")
HotKeySet("{F7}", "_Grave")
HotKeySet("{F8}", "_Acute")

; Choose a file to edit
Global $sFile = FileOpenDialog("Select file to edit", @ScriptDir, "Files (*.txt)", 3)

; Create a GUI
Global $hGUI = GUICreate("Test", 1000, 800)

Global $hEdit = GUICtrlCreateEdit($sText, 10, 10, 980, 740)
GUICtrlSetFont(-1, 14, Default, Default, "Courier New")

Global $hLabel_Line = GUICtrlCreateLabel("",  10, 770, 100, 20)
Global $hLabel_Col  = GUICtrlCreateLabel("", 120, 770, 100, 20)

Global $hStart_Button = GUICtrlCreateButton("Start", 810, 760, 80, 30)
Global $hSave_Button = GUICtrlCreateButton("Save", 910, 760, 80, 30)
Global $hDummy = GUICtrlCreateDummy()

GUISetState()

; Load file and set defaults
_Load_File()
_GUICtrlEdit_SetSel($hEdit, 0, 0)

; Wait until start position set
_Start_Select()

; Run through file
While 1

    ; Look for next vowel
    StringRegExp($sText, "(?i)[a|e|i|o|u]", 1, $iActChar)
    ; If none left - save file and exit
    If @error Then
        MsgBox(64, "End of file", "There are no more vowels to change")
        _Write_File()
        Exit
    EndIf
    ; Get position for next match
    $iActChar = @extended
    ; Read vowel found
    Global $sCurrChar = StringMid($sText, $iActChar - 1, 1)
    ; Set selection to highlight vowel
    _GUICtrlEdit_SetSel($hEdit, $iActChar - 2, $iActChar - 1)

    ; Show line and column at bottom of GUI
    Global $iActLine = _GUICtrlEdit_LineFromChar($hEdit)
    ; Show line
    GUICtrlSetData($hLabel_Line, "Line: " & $iActLine + 1)
    ; Calculate and show column
    Local $iColumn = $iActChar - _GUICtrlEdit_LineIndex($hEdit, $iActLine) - 1
    GUICtrlSetData($hLabel_Col, "Col : " & $iColumn)

    ; Ensure selected vowel is always in view vertically
    While _GUICtrlEdit_GetFirstVisibleLine($hEdit) + 15 < $iActLine
        ; Skip it too close to end
        If _GUICtrlEdit_GetLineCount($hEdit) - $iActLine < 20 Then ExitLoop
        ; Scroll down 1 line
        _GUICtrlEdit_Scroll($hEdit, $SB_LINEDOWN)
    WEnd

    ; Ensure selected vowel is always in view horizontally
    If $iColumn > 75 + $iScrollMoves Then
        ; Scroll right if too far over
        _GUICtrlEdit_LineScroll($hEdit, 50, 0)
        $iScrollMoves += 50
    EndIf
    If $iColumn < 25 + $iScrollMoves Then
        ; Scroll back to keep in view if too far to left
        While $iScrollMoves > $iColumn
            _GUICtrlEdit_LineScroll($hEdit, -50, 0)
            $iScrollMoves -= 50
        WEnd
    EndIf

    While 1

        Switch GUIGetMsg()
            ; [X]
            Case $GUI_EVENT_CLOSE
                Exit
            ; Used to move on when a char is changed
            Case $hDummy
                ExitLoop
            ; Save file
            Case $hSave_Button
                ; Write the changed file
                _Write_File()
                ; Ask about exit
                If MsgBox(36, "Exit?", "Do you want to exit now?") = 6 Then Exit
                ; Reread file and reset all defaults
                _Load_File()
                GUICtrlSetState($hEdit, $GUI_FOCUS)
                GUICtrlSetState($hStart_Button, $GUI_ENABLE)
                ; Select start position - default is last selection
                _Start_Select()
                ExitLoop

        EndSwitch

    WEnd

WEnd

Func _Skip()

    ; Activate the dummy to break out of the loop
    GUICtrlSendToDummy($hDummy)

EndFunc

Func _Circumflex()

    ; Call replace function with offset of 2
    _Replace_Char(2)

EndFunc

Func _Grave()

    ; Call replace function with offset of 0
    _Replace_Char(0)

EndFunc

Func _Acute()

    ; Call replace function with offset of 1
    _Replace_Char(1)

EndFunc

Func _Replace_Char($iAdd)

    Local $sNewChar
    ; Get ASCII value of found vowel
    Local $iAscCurrChar = Asc($sCurrChar)
    ; Select replacement depending on case and letter
    Switch $iAscCurrChar
        ; A, a
        Case 65, 97
            $sNewChar = Chr($iAscCurrChar + 127 + $iAdd)
        ; E, I, O, e, i, o
        Case 69, 73, 79, 101, 105, 111
            $sNewChar = Chr($iAscCurrChar + 131 + $iAdd)
        ; U, u
        Case 85, 117
            $sNewChar = Chr($iAscCurrChar + 132 + $iAdd)
    EndSwitch
    ; Replace the vowel in the text
    $sText = StringReplace($sText, $iActChar - 1, $sNewChar, 1)
    ; Reload the text into teh edit and reset the selection
    GUICtrlSetData($hEdit, $sText)
    _GUICtrlEdit_SetSel($hEdit, $iActChar - 2, $iActChar - 1)
    ; Activate the dummy to break out of the loop
    GUICtrlSendToDummy($hDummy)

EndFunc

Func _Start_Select()

    While 1
        Switch GUIGetMsg()
            ; [X]
            Case $GUI_EVENT_CLOSE
                Exit
            ; Start position set
            Case $hStart_Button
                GUICtrlSetState($hEdit, $GUI_FOCUS)
                ; Get start position
                $aArray = _GUICtrlEdit_GetSel($hEdit)
                ; Set SRE offset
                $iActChar = $aArray[0]
                GUICtrlSetState($hStart_Button, $GUI_Disable)
                ExitLoop
        EndSwitch
    WEnd

EndFunc

Func _Load_File()

    ; Read and load text
    $sText = FileRead($sFile)
    GUICtrlSetData($hEdit, $sText)
    ; Set defaults
    $iActChar = 1
    $iFirstChar = 0
    $sCurrChar = ""

EndFunc

Func _Write_File()

    ; Open and overwrite existing file
    Local $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, GUICtrlRead($hEdit))
    FileClose($hFile)

EndFunc

Have fun! :D

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

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