Jump to content

AutoTab


JohnBailey
 Share

Recommended Posts

Purpose:

Many times I want to correct the "format" of a portion of my script and AutoFormat (or Tidy AutoIt Source in AutoIt) just doesn't do simply what I want it to or in an isolated area. That's why I started this project awhile back. This allows you to simply to "format" an area of your code in any IDE by adding tabs. A good example of this is if you add an if-statement to a portion of your script and your like me and want it to be all nice and lined up, so now you can easily tab things over or remove tabs.

Note:

This little script is a work in progress and has problems. I'm sharing it because it's helpful for me and might be for others too, also there are some bugs with it that I'd like to get some assistance with resolving them.

Mods, if I've posted this in the example area prematurely, then you are free to move it (of course) and I won't be upset at all! :)

Script:

cs -------------------------------------------------
    Author:         John Bailey
    Modifier:       name
    Date Modified:  07/05/2007
    ScriptFunction: This helps a lot to format script
    AutoIt Ver:     version
    
    THANKS:     Richard Robertson for the AddTab code revsion
                Martin
                Ssubirias3 for the Key Detection

    Script Version: 1.0.0 (base code).(improved/added features).(script errors corrected)
    
    Script Update History:
    
        1.1.0 - F4 toggles AutoTab on and off
        
        1.3.0 - When minimized F4 displays a TrayTip with the status
                Add Remove Tab feature
                Add Up and Down Arrow feature
                Changed the visual arrangement of other elements
                
        1.3.0 - Added defaults to Down
                Added tray msg now shows more info when pressing F4
                
        1.4.0 - DoubleClicking F4 opens AutoTab
                TrayMenu has Remove/Add options

#ce -------------------------------------------------
Global $version = '1.4.0'

#include <Constants.au3>
#include <GUIConstants.au3>
#Include <Misc.au3>
#Include <File.au3>
#Include <Array.au3>
Opt("WinSearchChildren", 1)
Opt("WinTitleMatchMode", 4) 
Opt ("GUIOnEventMode", 1)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown.

HotKeySet("{F4}", "_Detected")
;HotKeySet('{ESC}','terminate')

;Initiate Toggle
Global $toggle = false

Global $dll = DllOpen("user32.dll")

#Region ### START Koda GUI section ### Form=i:\sai\auto-it files\gui designs\autotab.kxf
$ParentWin = GUICreate("AutoTab v"&$version, 336, 131, 193, 115)
$Label2 = GUICtrlCreateLabel("Select the options to use and then check Use", 11, 106, 314, 17, $SS_CENTER)
$Group1 = GUICtrlCreateGroup("", 57, 44, 144, 37)
    $UpCH = GUICtrlCreateCheckbox("Up", 74, 57, 35, 17, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_RIGHTBUTTON,$BS_RIGHT,$WS_TABSTOP))
    $DownCH = GUICtrlCreateCheckbox("Down", 128, 57, 51, 17, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_RIGHTBUTTON,$BS_RIGHT,$WS_TABSTOP))
    GUICtrlSetState(-1,$GUI_CHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$CopyCH = GUICtrlCreateCheckbox("CopyMethod", 237, 50, 75, 17, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_RIGHTBUTTON,$BS_RIGHT,$WS_TABSTOP))
GUICtrlSetState($CopyCH,$GUI_DISABLE)
$Checkbox1 = GUICtrlCreateCheckbox("Use ", 265, 68, 47, 22, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_RIGHTBUTTON,$BS_RIGHT,$WS_TABSTOP))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("1", 120, 22, 43, 21)
$Label1 = GUICtrlCreateLabel("Tab Amount", 56, 24, 62, 17)
$RemoveCH = GUICtrlCreateCheckbox("Remove Tabs", 179, 24, 91, 17, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_LEFT,$WS_TABSTOP))
#EndRegion ### END Koda GUI section ###

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

$remove = TrayCreateItem("Remove Tabs", -1, -1, 1)
$add = TrayCreateItem("Add Tabs", -1, -1, 1)
TrayItemSetState(-1, $TRAY_CHECKED)
TrayCreateItem("")
$on = TrayCreateItem("AutoTab on", -1, -1, 1)
$off = TrayCreateItem("AutoTab off", -1, -1, 1)
TrayItemSetState(-1, $TRAY_CHECKED)
TrayCreateItem("")
$TrayExititem = TrayCreateItem("Exit")

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE,"TrayEvents")
TrayItemSetOnEvent($remove,"TrayEvents")
TrayItemSetOnEvent($add,"TrayEvents")
TrayItemSetOnEvent($on,"TrayEvents")
TrayItemSetOnEvent($off,"TrayEvents")
TrayItemSetOnEvent($TrayExititem,"TrayEvents")


TraySetState()
TraySetClick(16)

GUISetState(@SW_SHOW)


loop()

Func loop()
    While 1
        If BitAND(GUICtrlRead($Checkbox1), $GUI_CHECKED) = $GUI_CHECKED Then
            $toggle = true
            TrayItemSetState($on, $TRAY_CHECKED)
            TrayItemSetState($off, $TRAY_UNCHECKED)
            If BitAND(GUICtrlRead($CopyCH), $GUI_CHECKED) = $GUI_CHECKED Then
                If _IsPressed("71", $dll) Then
                    CopyTab()
                EndIf
            Else
                If BitAND(GUICtrlRead($DownCH), $GUI_CHECKED) = $GUI_CHECKED Then
                    If _IsPressed("28", $dll) Then
                        AddTab('down')
                    EndIf
                EndIf
                If BitAND(GUICtrlRead($UpCH), $GUI_CHECKED) = $GUI_CHECKED Then
                    If  _IsPressed("26", $dll) Then
                        AddTab('up')
                    EndIf
                EndIF
            EndIF
        Else
            $toggle = False
            TrayItemSetState($on, $TRAY_UNCHECKED)
            TrayItemSetState($off, $TRAY_CHECKED)
        EndIf
        Sleep(40)
    WEnd
EndFunc

Func _Detected()
    If _IsPressed("73") Then
        $start = TimerInit()
        While _IsPressed("73")
            Sleep(50)
        WEnd
        $diff = TimerDiff($start)
        Sleep(10)
        If $diff >= 100 Then 
            toggle()
        ElseIf ($diff > 0) And ($diff < 100) Then 
            WinSetState($ParentWin, "", @SW_SHOW)
                WinSetState($ParentWin, "", @SW_RESTORE)
                TrayTip(@ScriptName, "", 0)
        EndIf
    EndIf
EndFunc  ;==>_Detected

#region not working
Func CopyTab()
    ClipPut('')
    Send('^c')
    Local $string = ClipGet()
    Local $stringArray = StringSplit($string,@LF,'1')
    Local $printString = ''
    Local $tabcount = Number(GUICtrlRead($Input1))
    Local $tab = ''
    Local $forvar
     For $forvar = 1 To $tabcount
        If BitAND(GUICtrlRead($RemoveCH), $GUI_CHECKED) = $GUI_CHECKED Then
        ;$tab &= @tab;'+{TAB}' <--- won't work
            $tab &= '+{TAB}'
        Else
        ;$tab &= @tab;'{TAB}' <--- won't work
            $tab &= '{TAB}'
        EndIF
    Next
    For $x=1 to UBound($stringArray)-1
    ;$printString &= $tab&$stringArray[$x]&@LF
        ConsoleWrite($stringArray[$x]&@LF)
        Send($tab&$stringArray[$x])
    ;$printString &= StringReplace($tab&$stringArray[$x],@LF,'')
        If $x<>UBound($stringArray)-1 Then
        ;$printString &= StringReplace($tab&$stringArray[$x],@LF,'')&@LF
        Else
        ;$printString &= StringReplace($tab&$stringArray[$x],@LF,'')
        EndIf
    Next
;ClipPut($printString)
;Send('^v')
;Send($printString)
    loop()
EndFunc
#endregion
;


Func AddTab($atDirection); Thanks to Richard Robertson
    Local $tabcount = Number(GUICtrlRead($Input1))
    If $atDirection = 'down' Then
        Local $sendstring = '{UP}'
    ElseIf $atDirection = 'up' Then     
        Local $sendstring = '{DOWN}'
    EndIf
    Local $forvar
    For $forvar = 1 To $tabcount
        If BitAND(GUICtrlRead($RemoveCH), $GUI_CHECKED) = $GUI_CHECKED Then
            $sendstring &= '+{TAB}'
        Else
            $sendstring &= '{TAB}'
        EndIF
    Next
    If $atDirection = 'down' Then
        $sendstring &= '{DOWN}{HOME}'
    ElseIf $atDirection = 'up' Then
        $sendstring &= '{UP}{HOME}'
    EndIf
    Send($sendstring)
    loop()
EndFunc

;test
;test
;test

Func SpecialEvents()
    Select
        Case @GUI_CTRLID = $GUI_EVENT_CLOSE
            Terminate()
            
        Case @GUI_CTRLID = $GUI_EVENT_MINIMIZE
           ;MsgBox(0, "Window Minimized", "ID=" & @GUI_CTRLID & " WinHandle=" & @GUI_WINHANDLE)
            WinSetState($ParentWin, "", @SW_HIDE)
            TrayTip(@ScriptName, "has been minimzed. Doubleclick here to restore", 10, 0)
            
        Case @GUI_CTRLID = $GUI_EVENT_RESTORE
           ;MsgBox(0, "Window Restored", "ID=" & @GUI_CTRLID & " WinHandle=" & @GUI_WINHANDLE)
    EndSelect
    
EndFunc

Func TrayEvents()
    Switch @TRAY_ID
        Case $remove
            GuiCtrlSetState($RemoveCH,$GUI_CHECKED)
        Case $add
            GuiCtrlSetState($RemoveCH,$GUI_UNCHECKED)
        Case $on
            toggle()
        Case $off
            toggle()
        Case $TrayExititem
            Terminate()
        Case $TRAY_EVENT_PRIMARYDOUBLE
            WinSetState($ParentWin, "", @SW_SHOW)
            WinSetState($ParentWin, "", @SW_RESTORE)
            TrayTip(@ScriptName, "", 0)
    EndSwitch
EndFunc

Func toggle()
    If $toggle Then
        GUICtrlSetState($Checkbox1,$GUI_UNCHECKED)
        $toggle = False
        TrayTip('AutoTab','is off',1)
    Else
        GUICtrlSetState($Checkbox1,$GUI_CHECKED)
        $toggle = True
        If BitAND(GUICtrlRead($RemoveCH), $GUI_CHECKED) = $GUI_CHECKED Then
            TrayTip('AutoTab','is on'&@LF&'tab amount '&Number(GUICtrlRead($Input1))&@LF&'removing',1)
        Else
            TrayTip('AutoTab','is on'&@LF&'tab amount '&Number(GUICtrlRead($Input1))&@LF&'adding',1)
        EndIf
    EndIf
EndFunc

Func Terminate()
    DllClose($dll)
    Exit
EndFunc

Known Bugs

Requested Features Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

Could you maybe use FileRead and use the line numbers as vaviables to set where to start? That is definately out of my league.. LOL

For the f4 sometimes not working, I have found that when we start setting this code in and playing around with this or that it sometimes gets put in places that make it unable to work while other functions are being used or shown in a gui. like setting a call to a function after the GUISetState. What I mean is with Autoit's limited one function at a time proccess it cannot perform one function whil another is running. Hope that came out right.. :)

Link to comment
Share on other sites

Could you maybe use FileRead and use the line numbers as vaviables to set where to start? That is definately out of my league.. LOL

For the f4 sometimes not working, I have found that when we start setting this code in and playing around with this or that it sometimes gets put in places that make it unable to work while other functions are being used or shown in a gui. like setting a call to a function after the GUISetState. What I mean is with Autoit's limited one function at a time proccess it cannot perform one function whil another is running. Hope that came out right.. :)

Indeed it did come out right. .. I'll look into revamping it with that in mind. If you have any suggestions for doing so, I'd appreciate the shared knowledge ;)

As for the FileRead, I wanted it to be something that works with the IDE and not the file itself, especially when dealing with IDEs like Flash Pro.

A decision is a powerful thing
Link to comment
Share on other sites

Not sure what to do with that.. Sorry

np!! :)

It's working ok and doesn't error, just does weird things with the GUI every-now-and-then. Maybe someone out there can take a look at it and give me some good nudges in the right direction ;)

thanks Gesller

A decision is a powerful thing
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...