Re: Snipper - Window Snipping Tool
Posted: 02 May 2023, 18:06
Let's help each other out
https://www.autohotkey.com/boards/
https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115622
It appears the OCR library will not process images that are smaller than a certain size in either dimension. I am not sure why.
Code: Select all
; Snipper - Extension - PDF
Extensions.Push({ PDF: { Text: 'COPY: Acrobat PDF', Func: Clipboard2PDF } })
Extensions.Push({ PDF: { Text: 'SAVE: PDF', Func: Clipboard2PDF.Bind(,GetFullPathName('.\Snipper - PDF\')) } })
Clipboard2PDF(Borders := false, PDFSavePath := '') ; Adobe Acrobat must be installed
{
If (PDFSavePath = "")
{
; Put Active Snip on Clipboard
Snip2Clipboard(Borders)
Try App := ComObject('AcroExch.App')
Catch
{
MsgBox 'Could not Open Adobe Acrobat COM'
return
}
App.Show()
App.MenuItemExecute('ImageConversion:Clipboard')
}
else
{
If !FileExist(PDFSavePath)
DirCreate(PDFSavePath)
SavedImageFile:= Snip2File(,Settings_SavePath_Image)
TimeStamp := FormatTime(, 'yyyy_MM_dd @ HH_mm_ss')
FileName := TimeStamp '.PDF'
FullPDFPath := PDFSavePath FileName
Commands := A_ScriptDir "\lib\img2pdf.exe " "`"" SavedImageFile "`"" " -o " "`"" FullPDFPath "`""
Run A_ComSpec " /c " Commands,,"Hide"
}
}
Code: Select all
; CheckUpdate - Open Forum Post for Snipper to Manually look for updates
Extensions.Push({ SM: { Text: 'OPEN: Check Snipper Forum Post for Update', Func: CheckUpdate } })
CheckUpdate(borders := false)
{
try run "https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115622"
}
Code: Select all
OCR2Clipboard(Borders := false)
{
; Get Hwnd of Active Snip
Hwnd := WinGetID('A')
; Get hBitMap of Snip
hBitMap := SendMessage(0x173, 0, 0, guiSnips[Hwnd].GuiObj.Pic)
; OCR of hBitMap
Result := OCR.FromBitmap(hBitMap)
Text := ""
for Line in Result.Lines
Text .= Line.Text "`n"
; Put Text in Clipboard
A_Clipboard := Trim(Text)
}
Code: Select all
Loop {
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
If A_Index < 50
guiSSR.Background.Redraw()
guiSSR.Move(X, Y, W, H)
Display_WH(false)
Sleep 10
} Until !GetKeyState(Key, 'p')
SelectOn(false) ; Restore Cursor TFW
Code: Select all
SelectOn(On) {
Static CROSS := 32515
if (On)
SetSystemCursor(CROSS)
else
SetSystemCursor()
}
;----------------------------------------------------------------------------
; Set the system cursor for resizing and moving
; https://www.autohotkey.com/boards/viewtopic.php?t=13284
;----------------------------------------------------------------------------
SetSystemCursor(Cursor := "") {
Static Cursors := [32650, 32512, 32515, 32649, 32651, 32513, 32648, 32646, 32643, 32645, 32642, 32644, 32516, 32514]
Static SPI_SETCURSORS := 0x57
Static LR_SHARED := 0x8000
Static U := "Uint"
Static I := "Int"
Static P := "Ptr"
; The line of code below loads a cursor from the system set
If (Cursor == "") {
; Restore system cursor
DllCall("User32.dll\SystemParametersInfoW", U, SPI_SETCURSORS, U, 0, P, 0, U, 0)
} Else {
; LoadCursor is superseded, use LoadImage instead
; When loading a system icon or cursor, you must use LR_SHARED (0x8000) or the function will fail to load the resource
hCursor := DllCall("User32.dll\LoadImage", U, 0, U, Cursor, U, 2, U, 0, U, 0, U, LR_SHARED)
; And than we set all the default system cursors to be our choosen cursor.
; CopyImage is necessary as SetSystemCursor destroys the cursor we pass to it after using it.
For v in Cursors {
DllCall("User32.dll\SetSystemCursor", U,
DllCall("User32.dll\CopyImage", U, hCursor, U, 2, I, 0, I, 0, U, 0),
U, v)
}
}
}
anhnha wrote: ↑24 May 2023, 04:43I uninstalled v1, installed v2, and then launched the snipping tool. However, I was disappointed to find that it does not display the rectangular region while I'm dragging it. As a result, I am unable to determine its size or confirm if it is the desired region until I complete the dragging process. Is this the intended behavior, or have I made a mistake?
Code: Select all
/* ;--------------------------------------------------------------------------------------------------------------------
[Extension] CopyCut for Explorer (by Adollo)
File Name : Snipper - Extension - CopyCutForExplorer.ahk
Version : 2023 05 27
USAGE: Use the context menu item to copy or cut the image as a file object for further insertion into the desired folder through the explorer.
*/ ;---------------------------------------------------------------------------------------------------------------------
; CONTEXT MENU -------------------------------------------------------
Extensions.Push({ Explorer: { Text: 'COPY: Explorer File Object', Func: Explorer_Copy.Bind(,Settings_SavePath_Image, Settings_SavePath_Image_Ext) } })
Extensions.Push({ Explorer: { Text: 'CUT: Explorer File Object', Func: Explorer_Cut.Bind(,Settings_SavePath_Image, Settings_SavePath_Image_Ext) } })
;----- FUNCTIONS ------------------------------------------
Explorer_Copy(Borders := false, SavePath:='', FileExt:=''){
FileName := Snip2File(Borders, Settings_SavePath_Image,, Settings_SavePath_Image_Ext)
Explorer_ClipboardSetFiles FileName, "Copy"
}
Explorer_Cut(Borders := false, SavePath:='', FileExt:=''){
FileName := Snip2File(Borders, Settings_SavePath_Image,, Settings_SavePath_Image_Ext)
Explorer_ClipboardSetFiles FileName, "Move"
}
; Function Explorer_ClipboardSetFiles based on:
; Hey @just me, I got a similar problem where the application won't recognize the clipboard files with only CF_HDROP.
; I fixed it by putting CF_FILENAME in addition. Please revise this cuz I don't know anything about C++ :D .
; Also hope you can release your function in the Script and Libraries section, it's really useful.
; https://www.autohotkey.com/boards/viewtopic.php?p=63914#p63914
; Translation function to AHK version 2 by Adollo
Explorer_ClipboardSetFiles(FilesToSet, DropEffect := "Copy") {
; FilesToSet - list of fully qualified file pathes separated by "`n" or "`r`n"
; DropEffect - preferred drop effect, either "Copy", "Move" or "" (empty string)
Static A_IsUnicode := 1
Static TCS := A_IsUnicode ? 2 : 1 ; size of a TCHAR
Static PreferredDropEffect := DllCall("RegisterClipboardFormat", "Str", "Preferred DropEffect")
Static DropEffects := Map(1, 1, 2, 2, "Copy", 1, "Move", 2)
; -------------------------------------------------------------------------------------------------------------------
; Count files and total string length
TotalLength := 0
FileArray := []
Loop Parse, FilesToSet, "`n", "`r"
{
If (Length := StrLen(A_LoopField))
FileArray.Push({Path: A_LoopField, Len: Length + 1})
TotalLength += Length
}
FileCount := FileArray.Length
If !(FileCount && TotalLength)
Return False
; -------------------------------------------------------------------------------------------------------------------
; Add files to the clipboard
If DllCall("OpenClipboard", "Ptr", A_ScriptHwnd) && DllCall("EmptyClipboard") {
; HDROP format ---------------------------------------------------------------------------------------------------
; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40)
hDrop := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 20 + (TotalLength + FileCount + 1) * TCS, "UPtr")
pDrop := DllCall("GlobalLock", "Ptr", hDrop)
Offset := 20
NumPut("UInt", Offset, pDrop + 0) ; DROPFILES.pFiles = offset of file list
NumPut("UInt", !!A_IsUnicode, pDrop + 16) ; DROPFILES.fWide = 0 --> ANSI, fWide = 1 --> Unicode
For Each, File In FileArray
Offset += StrPut(File.Path, pDrop + Offset, File.Len) * TCS
DllCall("GlobalUnlock", "Ptr", hDrop)
DllCall("SetClipboardData", "UInt", 0x0F, "UPtr", hDrop) ; 0x0F = CF_HDROP
File := FileArray[1]
VarSetStrCapacity(&ShortPath, 260) ; V1toV2: if 'ShortPath' is NOT a UTF-16 string, use 'ShortPath := Buffer(260)'
ShortLength := DllCall("GetShortPathName", "Str", File.Path, "Str", ShortPath, "Uint", 260)
hName := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", ShortLength + 1, "UPtr")
pName := DllCall("GlobalLock", "Ptr", hName)
StrPut(ShortPath, pName, ShortLength, "CP0")
DllCall("GlobalUnlock", "Ptr", hName)
DllCall("SetClipboardData", "UInt", 0xC006, "Ptr", hName) ; 0xC006 = CF_FILENAME
hNameW := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", File.Len * TCS, "UPtr")
pNameW := DllCall("GlobalLock", "Ptr", hNameW)
StrPut(File.Path, pNameW, File.Len * TCS)
DllCall("GlobalUnlock", "Ptr", hNameW)
DllCall("SetClipboardData", "UInt", 0xC007, "Ptr", hNameW) ; 0xC007 = CF_FILENAMEW
; Preferred DropEffect format ------------------------------------------------------------------------------------
If (DropEffect := DropEffects[DropEffect]) {
; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40)
hMem := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 4, "UPtr")
pMem := DllCall("GlobalLock", "Ptr", hMem)
NumPut("UChar", DropEffect, pMem + 0)
DllCall("GlobalUnlock", "Ptr", hMem)
DllCall("SetClipboardData", "UInt", PreferredDropEffect, "Ptr", hMem)
}
DllCall("CloseClipboard")
Return True
}
Return False
}
Apologies for the delay in my response.FanaticGuru wrote: ↑25 May 2023, 14:32anhnha wrote: ↑24 May 2023, 04:43I uninstalled v1, installed v2, and then launched the snipping tool. However, I was disappointed to find that it does not display the rectangular region while I'm dragging it. As a result, I am unable to determine its size or confirm if it is the desired region until I complete the dragging process. Is this the intended behavior, or have I made a mistake?
That is not expected behavior. There should be a semi-transparent green rectangle as you are dragging to select an area.
I am not sure what might be causing that issue. I would suggest some basic troubleshooting like reboot your computer and run just this script. See if that resolves the issue and then go from there.
FG
If you can offer the error code it can help debug, rather than guess.
Error: Target window not found.
Specifically: ahk_id329502
---- C:\Myscript\Snipper\Snipper.ahk
522: OnMessage(0x6, WM_ACTIVATE)
524: {
▶ 526: If WinGetTitle('ahk_id' hwnd) = 'SnipperWindow'
527: If (Activated := wParam << 48 >> 48)
528: SnipWinBorderColor(guiSnips[hwnd].GuiObj, 'Green')
Call stack:
C:\Myscript\Snipper\Snipper.ahk (526) : [WinGetTitle] If WinGetTitle('ahk_id' hwnd) = 'SnipperWindow'
C:\Myscript\Snipper\Snipper.ahk (526) : [WM_ACTIVATE] If WinGetTitle('ahk_id' hwnd) = 'SnipperWindow'
> OnMessage
C:\Myscript\Snipper\Snipper.ahk (298) : [Gui.Prototype.Show] guiSSR.Show('x' sX ' y' sY ' w1 h1')
C:\Myscript\Snipper\Snipper.ahk (298) : [SelectScreenRegion] guiSSR.Show('x' sX ' y' sY ' w1 h1')
C:\Myscript\Snipper\Snipper.ahk (145) : [<Hotkey>] Area := SelectScreenRegion('LButton')
> #Lbutton