/* credits to Rohwedder for: https://www.autohotkey.com/boards/viewtopic.php?t=57919 "Draw rectangle and get coordinates relative to active windows. Should work with all current AHK versions/builds." */ ; ================================================================================================ ; the macro uses 8 separate parsing loops #Persistent #SingleInstance Force #NoEnv SetBatchLines, -1 ; to have the script run at maximum speed SetWinDelay, 0 ; fast speed of WinMove CoordMode, Mouse, Screen CoordMode, ToolTip, Screen MouseGetPos, StartX, StartY Tooltip, Set Surface Coordinates.`nMake lasso selection.`nwith Shift + LeftButton., StartX, StartY - 150, 1 BW := 1 ; Border width (and height) in pixels BC := "Red" ; Border color FirstCall := True Gui, -Caption +ToolWindow +LastFound +AlwaysOnTop Gui, Color, %BC% Return +LButton:: ; hotkey Shift LButton press and hold MouseGetPos, StartX, StartY WinGetActiveStats, Title, WindowWidth, WindowHeight, WindowX, WindowY SetTimer, DrawRectangle, 10 Return Counter := 0 +LButton Up:: ; hotkey Shift LButton release Counter +=1 ; how many times LButton is released If (Counter = 1) ; the first time Clipboard := "" ; clipboard is made empty SetTimer, DrawRectangle, Off ; stop red line production FirstCall := True Gui, Cancel ToolTip, , , , 2 ; close tooltip (2) X2 := X1 + W1 Y2 := Y1 + H1 ; MsgBox, 0, Coordinates, Area # %Counter%`nX = %X1% - Y = %Y1% - W = %W1% - H = %H1% MsgBox, 4096, Coordinates, ( Area # %Counter% X1 = %X1% - Y1 = %Y1% - X2 = %X2% - Y2 = %Y2% Upto 8 areas and images can be created in one go. If you create only one image, press OK on the first pass followed by CapsLock. This opens the text file Image Coordinates. You can paste the coordinates in this file with Control + V. If you want to create e.g 5 areas and their images press CapsLock after your OK for Area 5. After pasting press Shift + NumpadEnter to create the images. ) ; MsgBox, 0, relative: %Title%, % "X = " X1-WindowX " - Y = " Y1-WindowY If (Counter = 1) ; max 8 areas stored Area1 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 2) Area2 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 3) Area3 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 4) Area4 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 5) Area5 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 6) Area6 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 7) Area7 = %X1%, %Y1%, %X2%, %Y2% If (Counter = 8) Area8 = %X1%, %Y1%, %X2%, %Y2% ClipboardText = Surface # %Counter%%A_Tab%X1 = %X1%%A_Tab%Y1 = %Y1%%A_Tab%%A_Tab%X2 = %X2%%A_Tab%Y2 = %Y2% ClipNew := ClipboardText If (Counter = 1) Clipboard = %ClipNew% If (Counter > 1) Clipboard = %Clipboard%`n`r%ClipNew% Return DrawRectangle: MouseGetPos, X2, Y2 If (XO = X2) And (YO = Y2) ; if the mouse hasn't moved Return Gui, +LastFound XO := X2, YO := Y2 If (X2 < OriginX) ; to allow dragging to the left of the click point X1 := X2, X2 := OriginX Else X1 := StartX If (Y2 < StartY) ; to allow dragging above the click point Y1 := Y2, Y2 := StartY Else Y1 := StartY W1 := X2 - X1, H1 := Y2 - Y1 ; draw the rectangle W2 := W1 - BW, H2 := H1 - BW WinSet, Region, 0-0 %W1%-0 %W1%-%H1% 0-%H1% 0-0 %BW%-%BW% %W2%-%BW% %W2%-%H2% %BW%-%H2% %BW%-%BW% If (FirstCall) { Gui, Show, NA x%X1% y%Y1% w%W1% h%H1% FirstCall := False } WinMove, , , X1, Y1, W1, H1 ; ToolTip, %X1% - %Y1% - %X2% - %Y2%, 2 Return CapsLock:: ; write coordinates to "Image_Coordinates.txt" If (Counter = "") ExitApp Else { Tooltip, , , , 1 ; close tooltip (1) SetTitleMatchMode, 2 If WinExist("Image_Coordinates") Goto, SkipRunIC3 Run, Image_Coordinates.txt, , Min ; start minimized Sleep, 500 SkipRunIC3: WinActivate, Image_Coordinates Sleep, 50 WinGetPos, , , Width, Height, Image_Coordinates WinMove, Image_Coordinates, , (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2), 800, 800 ; 800, 800 size of text window (*change*?) Sleep, 100 } WinWaitNotActive, Image_Coordinates.txt - Notepad MsgBox, 4161, Create and Save Images, To create and save your images`nPress Shift + NumpadEnter. IfMsgBox, Cancel ExitApp Return +NumpadEnter:: ; select image area - for Snipping Tool Tooltip, , , , 1 ; close tooltip (1) if "Image_Coordinates.txt" not used - no CapsLock press Loop, parse, Area1, `, { ; MsgBox, 0, Area-1, Field %A_Index% is %A_LoopField%.`n`n ; IfMsgBox, No ; break If (A_Index = 1) X1 = %A_LoopField% If (A_Index = 2) Y1 = %A_LoopField% If (A_Index = 3) X2 = %A_LoopField% If (A_Index = 4) { Y2 = %A_LoopField% Break } } Loop, parse, Area2, `, { ; MsgBox, 4, Area-2, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X3 = %A_LoopField% If (A_Index = 2) Y3 = %A_LoopField% If (A_Index = 3) X4 = %A_LoopField% If (A_Index = 4) { Y4 = %A_LoopField% Break } } Loop, parse, Area3, `, { ; MsgBox, 4, Area-3, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X5 = %A_LoopField% If (A_Index = 2) Y5 = %A_LoopField% If (A_Index = 3) X6 = %A_LoopField% If (A_Index = 4) { Y6 = %A_LoopField% Break } } Loop, parse, Area4, `, { ; MsgBox, 4, Area-4, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X7 = %A_LoopField% If (A_Index = 2) Y7 = %A_LoopField% If (A_Index = 3) X8 = %A_LoopField% If (A_Index = 4) { Y8 = %A_LoopField% Break } } Loop, parse, Area5, `, { ; MsgBox, 4, Area-5, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X9 = %A_LoopField% If (A_Index = 2) Y9 = %A_LoopField% If (A_Index = 3) X10 = %A_LoopField% If (A_Index = 4) { Y10 = %A_LoopField% Break } } Loop, parse, Area6, `, { ; MsgBox, 4, Area-6, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X11 = %A_LoopField% If (A_Index = 2) Y11 = %A_LoopField% If (A_Index = 3) X12 = %A_LoopField% If (A_Index = 4) { Y12 = %A_LoopField% Break } } Loop, parse, Area7, `, { ; MsgBox, 4, Area-7, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X13 = %A_LoopField% If (A_Index = 2) Y13 = %A_LoopField% If (A_Index = 3) X14 = %A_LoopField% If (A_Index = 4) { Y14 = %A_LoopField% Break } } Loop, parse, Area8, `, { ; MsgBox, 4, Area-8, Field %A_Index% is %A_LoopField%.`n`nContinue? ; IfMsgBox, No ; break If (A_Index = 1) X15 = %A_LoopField% If (A_Index = 2) Y15 = %A_LoopField% If (A_Index = 3) X16 = %A_LoopField% If (A_index = 4) { Y16 = %A_LoopField% Break } } MsgBox, 4096, Coordinates surfaces, ( #1 X1 = %X1% Y1 = %Y1% X2 = %X2% Y2 = %Y2% #2 X3 = %X3% Y3 = %Y3% X4 = %X4% Y4 = %Y4% #3 X5 = %X5% Y5 = %Y5% X6 = %X6% Y6 = %Y6% #4 X7 = %X7% Y7 = %Y7% X8 = %X8% Y8 = %Y8% #5 X9 = %X9% Y9 = %Y9% X10 = %X10% Y10 = %Y10% #6 X11 = %X11% Y11 = %Y11% X12 = %X12% Y12 = %Y12% #7 X13 = %X13% Y13 = %Y13% X14 = %X14% Y14 = %Y14% #8 X15 = %X15% Y15 = %Y15% X16 = %X16% Y16 = %Y16% ) Run, C:\windows\system32\SnippingTool.exe ; Run, "C:\Windows\Sysnative\SnippingTool.exe" ; this path is needed for some Win systems Sleep, 1000 WinWait, Snipping Tool, , 0.5 If ErrorLevel MsgBox, TimeOut WinWait at start of loop ; for testing IfWinNotActive, Snipping Tool WinActivate, Snipping Tool WinWaitActive, Snipping Tool, , 0.5 If ErrorLevel MsgBox, TimeOut WinWaitActive at start of loop ; for testing WinMove, Snipping Tool, , 350, 125 ; position window x = 350 y = 125 (*change*?) Sleep, 50 Send, ^n ; create new image Sleep, 1000 Loop, %Counter% { If (A_Index = 1) { MouseMove, X1, Y1 Send {Shift Down}{Click, %X1%, %Y1% down} MouseMove, X2, Y2 Goto, EndSelSurface } If (A_Index = 2) { MouseMove, X3, Y3 Send {Shift Down}{Click, %X3%, %Y3% down} MouseMove, X4, Y4 Goto, EndSelSurface } If (A_Index = 3) { MouseMove, X5, Y5 Send {Shift Down}{Click, %X5%, %Y5% down} MouseMove, X6, Y6 Goto, EndSelSurface } If (A_Index = 4) { MouseMove, X7, Y7 Send {Shift Down}{Click, %X7%, %Y7% down} MouseMove, X8, Y8 Goto, EndSelSurface } If (A_Index = 5) { MouseMove, X9, Y9 Send {Shift Down}{Click, %X9%, %Y9% down} MouseMove, X10, Y10 Goto, EndSelSurface } If (A_Index = 6) { MouseMove, X11, Y11 Send {Shift Down}{Click, %X11%, %Y11% down} MouseMove, X12, Y12 Goto, EndSelSurface } If (A_Index = 7) { MouseMove, X13, Y13 Send {Shift Down}{Click, %X13%, %Y13% down} MouseMove, X14, Y14 Goto, EndSelSurface } If (A_Index = 8) { MouseMove, X15, Y15 Send {Shift Down}{Click, %X15%, %Y15% down} MouseMove, X16, Y16 Goto, EndSelSurface } EndSelSurface: Sleep, 250 Send {Lbutton up}{Shift up} Sleep, 150 IfWinNotActive, Snipping Tool WinActivate, Snipping Tool WinGetPos, , , Width_ST, Height, Snipping Tool CoordMode, ToolTip, Window Tooltip, Save the image.`nPress Z when ready., Width_ST / 2, 0, 3 Keywait, z, d ToolTip, , , , 3 ; close tooltip (3) If (A_Index = Counter) Break IfWinNotActive, Snipping Tool WinActivate, Snipping Tool Else { Run, C:\windows\system32\SnippingTool.exe ; Run, "C:\Windows\Sysnative\SnippingTool.exe" ; this path is needed for some Win systems Sleep, 1000 WinWait, Snipping Tool, , 0.5 If ErrorLevel MsgBox, TimeOut WinWait at end of loop ; for testing IfWinNotActive, Snipping Tool WinActivate, Snipping Tool WinWaitActive, Snipping Tool, , 0.5 If ErrorLevel MsgBox, TimeOut WinWaitActive at end of loop ; for testing WinMove, Snipping Tool, , 350, 125 ; (*change*?) Sleep, 50 Sleep, 100 Send, ^n ; create new image Sleep, 1100 } } IfWinExist, Snipping Tool WinClose, Snipping Tool ExitApp Return ; ==========================================================================================