Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

2008-10-05

Work around AutoIT stdin limitation

The fastest way to implement a small script and to process a file (or a chunk of data) is to use the standard input rather than file-handles.

1/ It is easier to redirect a file instead of providing parameters to a program.

2/ When you use redirections, the operating system works for you: it opens the file and feeds the program with its content.
C:\> AutoIt3.exe DiffPrettyfier.au3 < MyInput.txt > MyOutput.txt
 
The content of the script is straight forward:
$myInput = ConsoleRead()
; do something here
; (...)
ConsoleWrite($myOutput)



But there is one problem that makes this a bit harder: AutoIT 'ConsoleRead' doesn't handle input bigger than '65535' characters. So here is a little workaround: use the function 'StdinRead' below instead of 'ConsoleRead':
;-----------------------------------------------------------------
; Name .........: StdinRead
; Description ..: Wrapper for ConsoleRead because it doesn't
; handle big files
; Warning ......: This is a workaround but it has limitations too,
; I used a big file to test this function but it
; might fail on a VERY big file (eg. if the file
; contains 2 147 483 647 characters)
; original big file tested: 18 030 950
; biggest string size allowed: 2 147 483 647
; Parameters ...: n/a
; Return .......: String read on STDIN or empty String "" if
; nothing found on STDIN
;-----------------------------------------------------------------
Func StdinRead()
Const $MAX_BUFFER = 65535
$eofReached = False
$fileContent = ""
While Not $eofReached
$buffer = ConsoleRead($MAX_BUFFER)
If @error Or $buffer = "" Then $eofReached = True
$fileContent &= $buffer
WEnd
Return $fileContent
EndFunc ;==>StdinRead



NOTE: For "ConsoleWrite" to work as expected (redirecting output to stdout), you need to compile the script using the "/console" switch.




2008-01-01

BringBackFromTray: for MinimizeToTray extension users (Firefox or Thunderbird)




The first extension I installed on Firefox was MinimizeToTray. I love it.

Sadly, It's easy to minimize my prefered browser (Ctrl+Shift+M) but there is no keystroke to bring it back from the Tray: I though for a long time about implementing a global hook that would wait for the keyboard shortcut and activate the Firefox window.

My brand new Logitech keyboard provided a much easier solution! This keyboard has special buttons that can be configured to launch the program of my choice. So I wrote the program below (AutoIT language). Feel free to use it. But, as usual, take note that this program is provided without any guarantee:


; static variables
$program_name="Firefox"
$program_path=_
"C:\Program Files\Mozilla Firefox\firefox.exe"

; set AutoIT options
AutoItSetOption("WinTitleMatchMode", 2)

; Get state of the firefox window
; (eg. 21 minimize to tray =  1 + 4 + 16 )
; (eg. 23 minimize = 1 + 2 + 4 + 16 )
$state = WinGetState($program_name)

; not runing ?
; -> launch the program
If Not $state Then
 Run($program_path)
; not visible ?
; -> bring from the tray
ElseIf Not BitAND($state, 2) Then
 WinActivate($program_name)
; visible and active ?
; -> MinimizeToTray
ElseIf BitAND($state, 8) Then
 ControlSend ($program_name, "", ""_
 , "{SHIFTDOWN}{CTRLDOWN}m{CTRLUP}{SHIFTUP}")
; visible but not active ?
; -> activate
Else
 WinActivate($program_name)
 WinWaitActive($program_name)
EndIf

I recommend you have a look into this great tool called AutoIT: it is extremely useful and efficient in a day-to-day basis; and its syntax is very simple (Basic-like).