Jump to content

Snip -- screen capture tool


CarlD
 Share

Recommended Posts

Snip is a CLI screen-capture tool. See below for the command-line usage. The compiled executable is HERE -- it comes bundled with my TSK task viewer (q.v.).

A cool feature of Snip is that it has a "burst" mode for taking multiple, rapid-fire screenshots, great for capturing video stills.

Usage:

SNIP.exe: Screen-Capture Utility [CLD rev.2021-03-26]

Usage:

SNIP [["]window_title["]|["]sub$tring["]|PID|hWnd] [/D d:\path] [/MAX] [/Q] [/R n] [/I m]
  default = current window
  default PNG location = C:\Users\CLD\Screenshots
Options (relaxed order):
/D [[d:]\path]  PNG location: current directory [alternate directory]
/MAX            Maximize target window before capture
/Q              Quiet -- do not display screenshot
/R n            Repeat screenshot n times (burst) [implies /Q]
                  default n = 5
/I m            Interval between repeats, in milliseconds [implies /R]
                  default = 100 ms (intervals are approximate)

SNIP /?|/H      Show this Help

Source code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=snip.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=n
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
------------------------------------------
Screen-Capture Utility [CLD rev.2021-03-26]

Usage
-----
Snip [window_title] [d:\path]
....
Default window_title is current window
Default d:\path is %HOMEDIR%%HOMEPATH%\Screenshots
Image is saved in .PNG format
Image (.png) is saved to %HOMEDIR%%HOMEPATH%\Screenshots
------------------------------------------
#ce

#include <MsgBoxConstants.au3>
#include <Misc.au3>
#include <ScreenCapture.au3>
#include <StringConstants.au3>

AutoItSetOption("WinTitleMatchMode", -2)

Global $sCMline = $CmdLineRaw
If StringInStr($sCMline, "/?") Or StringInStr($sCMline, "/h") Then _
    Exit _ShowHelp()
Global $sCW = WinGetTitle("[active]")
Global $sWn = "[active]"
If $CmdLine[0] > 0 Then
    If StringInStr($CmdLine[1], "/") <> 1 Then $sWn = $CmdLine[1]
EndIf
If StringIsDigit($sWn) Then
    Global $aWinList = WinList()
    Global $hTmp = _GetHwndFromPID($sWn)
    If $hTmp Then $sWn = $hTmp  
EndIf
If StringInStr($sWn, "0x") = 1 Then
    If StringIsXDigit(StringTrimLeft($sWn, 2)) Then $sWn = HWnd($sWn)
EndIf
If Not WinExists($sWn) Then _
    Exit MsgBox(0, @ScriptName, "No window title with """ & $sWn & """")
Global $sProgDir = @UserProfileDir & "\Screenshots"
If StringInStr($sCMline, "/d") Then
    $sProgDir = _GetArgValue($sCMline, "/d")
    If Not $sProgDir Then $sProgDir = "."
EndIf
If StringRight($sProgDir, 1) = "\" Then $sProgDir = StringTrimRight($sProgDir, 1)
If Not FileExists($sProgDir) Then DirCreate($sProgDir)
Global $iIter = 1, $iIntv = 100
If StringInStr($sCMline, "/i") Or StringInStr($sCMline, "/r") Then
    $iIter = _GetArgValue($sCMline, "/r")
    If Not $iIter Then $iIter = 5
    $iIntv = _GetArgValue($sCMline, "/i")
EndIf
If Not StringIsDigit($iIter) Then $iIter = 1
If Not StringIsDigit($iIntv) Then $iIntv = 100
Global $iX = 0

#cs
$WIN_STATE_EXISTS (1) = Window exists
$WIN_STATE_VISIBLE (2) = Window is visible
$WIN_STATE_ENABLED (4) = Window is enabled
$WIN_STATE_ACTIVE (8) = Window is active
$WIN_STATE_MINIMIZED (16) = Window is minimized
$WIN_STATE_MAXIMIZED (32) = Window is maximized
#ce

Global $hWnd = WinGetHandle($sWn)
Global $iWMn = BitAND(WinGetState($hWnd), 16)
Global $iWMx = BitAND(WinGetState($hWnd), 32)
WinActivate($hWnd)
WinWaitActive($hWnd, "", 10)
If StringInStr($sCMline, "/max") Then WinSetState($hWnd, "", @SW_MAXIMIZE)
Global $sFn
For $i = 1 To $iIter
    $sFn = $sProgDir & "\Snip-" & @YEAR & @MON & @MDAY & "T" & @HOUR & @MIN & @SEC
    If $iIter > 1 Then $sFn &= StringFormat("%03u", @MSEC)
    $sFn &= ".png"
    _SnipIt($sFn)
    Sleep($iIntv)
Next
If $iWMx <> 32 Then WinSetState($hWnd, "", @SW_RESTORE)
If $iWMn = 16 Then WinSetState($hWnd, "", @SW_MINIMIZE)

If StringInStr($sCMline, "/i") + StringInStr($sCMline, "/q") + StringInStr($sCMline, "/r") = 0 Then
    ShellExecute("nircmdc.exe", "clipboard copyimage " & $sFn, _
        @ScriptDir, "Open", @SW_HIDE)
    ShellExecute($sFn)
Else
    WinActivate($sCW)
    WinSetState("[active]", "", @SW_SHOW)
EndIf
Exit

; Function definitions
; --------------------

Func _SnipIt($sFname)
    _ScreenCapture_CaptureWnd($sFname, $hWnd)
    Local $t = TimerInit()
    While Not FileExists($sFn)
        Sleep(20)
        If TimerDiff($t) > 10000 Then _
            Exit MsgBox(0, @ScriptName, StringTrimRight(@ScriptName, 4) & " timed out")
    WEnd
EndFunc  ;==>SnipIt

Func _GetArgValue($sCmL, $sArg)
; Get value (if any) following /SWitch (arg)
    Local $sVal = "", $sSep = StringLeft($sArg, 1)
    Local $iLft = StringLen($sArg) - 1 + StringInStr($sCmL, $sArg)
    If $iLft > StringLen($sArg) - 1 Then $sVal = StringTrimLeft($sCmL, $iLft)
    If StringInStr($sVal, $sSep) Then $sVal = _
        StringLeft($sVal, StringInStr($sVal, $sSep) - 1)
    If $sVal Then $sVal = _
        StringStripWS($sVal, 3)
    Return $sVal
EndFunc  ;==>_GetArgValue

Func _GetHwndFromPID($PID)
    Local $hWnd = 0
    Local $stPID = DllStructCreate("int")
    For $i = 1 To $aWinList[0][0]
        If $aWinList[$i][0] <> "" Then
            DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $aWinList[$i][1], "ptr", DllStructGetPtr($stPID))
            If DllStructGetData($stPID, 1) = $PID Then Return $aWinList[$i][1]
        EndIf
    Next
    Return $hWnd
EndFunc ;==>_GetHwndFromPID

Func _ShowHelp()
    Local $sTxt = StringUpper(StringTrimRight(@ScriptName, 4)) & StringLower(StringRight(@ScriptName, 4)) & ": Screen-Capture Utility [CLD rev.2021-03-26]" & @CRLF & @CRLF & "Usage:" & @CRLF & @CRLF & StringUpper(StringTrimRight(@ScriptName, 4))  & " [[""]window_title[""]|[""]sub$tring[""]|PID|hWnd] [/D d:\path] [/MAX] [/Q] [/R n] [/I m]" & @CRLF & "  default = current window" & @CRLF & "  default PNG location = " & @UserProfileDir & "\Screenshots" & @CRLF & "Options (relaxed order):" & @CRLF & "/D [[d:]\path]  PNG location: current directory [alternate directory]" & @CRLF & "/MAX            Maximize target window before capture" & @CRLF & "/Q              Quiet -- do not display screenshot" & @CRLF & "/R n            Repeat screenshot n times (burst) [implies /Q]" & @CRLF & "                  default n = 5" & @CRLF & "/I m            Interval between repeats, in milliseconds [implies /R]" & @CRLF & "                  default = 100 ms (intervals are approximate)" & @CRLF & @CRLF & StringUpper(StringTrimRight(@ScriptName, 4)) & " /?|/H      Show this Help"
    _SplashEsc($sTxt, 1200, 600)
EndFunc  ;==>_ShowHelp

Func _SplashEsc($sTxt, $iWid = 800, $iHgt = 400)
; #include <Misc.au3>
    Local $hDLL = DLLOpen("user32.dll")
    SplashTextOn(@ScriptName & " | Esc quits", $sTxt, $iWid, $iHgt, -1, -1, 54, "Cascadia Mono Regular", 14)
    While 1
        If _IsPressed("1B", $hDLL) Then
            SplashOff()
            ExitLoop
        EndIf
    WEnd
    DLLClose($hDLL)
    Return
EndFunc  ;==>_SplashEsc

 

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

×
×
  • Create New...