#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=NPSort.ico #AutoIt3Wrapper_Outfile_x64=NPSort.exe #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;=============================================================================== ; Notepad extension to do sorting. ; ; To use this extension: ; ; 1) Start it (If not already started). Icon for it will be on task bar. ; 2) Left click column of Notepad text to select first sort column ; (all columns to right will be included as sort columns). ; 3) Left or right click the task bar icon. ; 4) Left click "Sort ascending" or "Sort descending". ;=============================================================================== #include #include #include #include _Singleton(@Scriptname, 0) Global $handle, $savedclipboard, $tempfqn1, $tempfqn2 Opt('TrayMenuMode', 1) Opt('TrayOnEventMode', 1) TrayCreateItem('Notepad Sort') TrayItemSetState(-1, $TRAY_DISABLE) TrayCreateItem('') TrayItemSetState(-1, $TRAY_DISABLE) $Aitem = TrayCreateItem('Sort Ascending') TrayItemSetOnEvent(-1, 'Aevent') $Ditem = TrayCreateItem('Sort Descending') TrayItemSetOnEvent(-1, 'Devent') TrayCreateItem('Exit') TrayItemSetOnEvent(-1, 'ExitEvent') TraySetToolTip('Notepad Sort') Opt('WinWaitDelay', 10) Sleep(2147483647) ;------------------------------------------------------------------------------- ; Initiate ascending sort here Func AEvent() Sort('A') TrayItemSetState($Aitem, $TRAY_UNCHECKED) EndFunc ;------------------------------------------------------------------------------- ; Initiate descending sort here Func DEvent() Sort('D') TrayItemSetState($Ditem, $TRAY_UNCHECKED) EndFunc ;------------------------------------------------------------------------------- ; All sorting done here Func Sort($ad) ;--------------------------------------------------------------------------- ; Copy Notepad text to clipboard Opt('WinTitleMatchMode', 2) $handle = WinWait(' - Notepad', '', 1) If Not $handle Then Return $savedclipboard = ClipGet() If @error Then $savedclipboard = '' $status = StatusbarGetText($handle, '', 2) $col = StringMid($status, StringInStr($status, ', Col ') + 6) WinActivate($handle) WinWaitActive($handle) WinSetOnTop($handle, '', $WINDOWS_ONTOP) Send('^a') Send('^c') ;--------------------------------------------------------------------------- ; Write "text copy" to temp file $tempfqn1 = _TempFile('', '', '.txt') _FileCreate($tempfqn1) $temptitle = StringMid($tempfqn1, StringInStr($tempfqn1, '\', 0, -1) + 1) & ' - Notepad' Run('notepad.exe ' & $tempfqn1) Opt('WinTitleMatchMode', 1) WinActivate($temptitle) WinWaitActive($temptitle) Send('^v') Send('^s') WinClose($temptitle) ;--------------------------------------------------------------------------- ; Sort the temp file to another temp file If $ad = 'A' Then $order = ' ' Else $order = ' /R ' EndIf $tempfqn2 = _TempFile('', '', '.txt') $rc = RunWait('sort /+' & $col & $order & $tempfqn1 & ' /O ' & $tempfqn2, '', @SW_HIDE) If $rc Then Cleanup() MsgBox($MB_OK, @ScriptName, 'Sort failed with rc=' & $rc) Return EndIf ;--------------------------------------------------------------------------- ; Reload the sorted temp file text to the clipboard Run('notepad.exe ' & $tempfqn2) $temptitle = StringMid($tempfqn2, StringInStr($tempfqn2, '\', 0, -1) + 1) & ' - Notepad' WinActivate($temptitle) WinWaitActive($temptitle) Send('^a') Send('^c') WinClose($temptitle) ;--------------------------------------------------------------------------- ; Replace the Notepad text with the sorted text WinActivate($handle) WinWaitActive($handle) Send('{DEL}') Send('^v') ;--------------------------------------------------------------------------- ; Cleanup Cleanup() EndFunc ;------------------------------------------------------------------------------- ; Cleanup Func Cleanup() ClipPut($savedclipboard) WinSetOnTop($handle, '', $WINDOWS_NOONTOP) FileDelete($tempfqn1) FileDelete($tempfqn2) EndFunc ;------------------------------------------------------------------------------- ; Shut down this extension Func ExitEvent() Exit EndFunc