Advertisement
Lorenzo501

Snipping Tool.ahk

Oct 23rd, 2024 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     This is not meant to be used together with the Win+S Snip feature of Time Saver (use whichever feels best at a certain computerwork session). Place in %Temp% folder.
  3.     In File Explorer go to: "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories" > "Snipping Tool" > Properties, and then change Target from:
  4.     "%windir%\system32\SnippingTool.exe" to "%Temp%\Snipping Tool.ahk", and "Start in" from blank to "%Temp%" (then find it in the Start menu and pin to Start).
  5.  */
  6. #Requires AutoHotkey 2.0
  7. #SingleInstance Force
  8. #NoTrayIcon
  9. #Include "C:\Users\%A_UserName%\Downloads\AutoHotkey\Lib\winrt.ahk-main\windows.ahk"
  10. Run("ms-screenclip:")
  11. HookEvent(EVENT_OBJECT_NAMECHANGE := 0x800C, HandleNameChangedSnipWinEvent)
  12. SetTimer(DismissSnipNotification, -600000) ; This sets the timer to 10 minutes
  13.  
  14. HandleNameChangedSnipWinEvent(hWinEventHook, event, hWnd, *)
  15. {
  16.     try
  17.         if (WinGetTitle(hWnd) = "Snip & Sketch" && WinGetClass(hWnd) = "ApplicationFrameWindow")
  18.         {
  19.             Sleep(100)
  20.             WinClose(hWnd)
  21.  
  22.             ; Save clipboard image to temp folder to see its width and height (overwrites any that might've been saved in the past)
  23.             RunWait("PowerShell.exe -Command `"$img = get-clipboard -format image; $img.save('" A_Temp "\latestSnip.jpg')`"",, "Hide")
  24.             imageSize := GetImageSize(A_Temp "\latestSnip.jpg")
  25.  
  26.             ; The extra space is for the paint toolset ofcourse
  27.             rawPaintWinWidth := imageSize.Width + 27
  28.             rawPaintWinHeight := imageSize.Height + 192
  29.  
  30.             ; Initializing the paint wnd size (without the minimum, it will hide the paint toolset and without the maximum, it will go beyond the screen boundaries)
  31.             paintWinWidth := (rawPaintWinWidth >= 379 ? (rawPaintWinWidth <= A_ScreenWidth ? rawPaintWinWidth : A_ScreenWidth) : 379)
  32.             paintWinHeight := (rawPaintWinHeight >= 251 ? (rawPaintWinHeight <= A_ScreenHeight ? rawPaintWinHeight : A_ScreenHeight) : 251)
  33.  
  34.             Run("mspaint.exe",, "Min")
  35.             WinWait("ahk_exe mspaint.exe")
  36.             WinSetTransparent(0)
  37.  
  38.             if (paintWinWidth = A_ScreenWidth && paintWinHeight = A_ScreenHeight)
  39.             {
  40.                 WinMaximize()
  41.                 Send("^v^+x") ; Pastes the image from clipboard and removes any white space that might remain
  42.             }
  43.             else
  44.             {
  45.                 WinActivate()
  46.                 Send("^v^+x") ; Pastes the image from clipboard and removes any white space that might remain
  47.                 WinRestore() ; Unmaximize if it's maximized
  48.                 WinMove((A_ScreenWidth / 2) - (paintWinWidth / 2), (A_ScreenHeight / 2) - (paintWinHeight / 2), paintWinWidth, paintWinHeight) ; Moves to center w/ new size
  49.             }
  50.  
  51.             WinSetTransparent("Off")
  52.             ExitApp()
  53.         }
  54. }
  55.  
  56. GetImageSize(imagePath)
  57. {
  58.     static shellObj := ComObject("Shell.Application")
  59.     SplitPath(imagePath, &fileName, &fileDir)
  60.     folderObj := shellObj.Namespace(fileDir)
  61.  
  62.     if (!folderItemObj := folderObj.ParseName(fileName))
  63.         throw ValueError("The image path does not contain a (valid) file", -1, imagePath)
  64.  
  65.     sizeArray := StrSplit(folderItemObj.ExtendedProperty("Dimensions"), " x ")
  66.  
  67.     ; Ad hoc object which allows you to see its properties with IntelliSense, usable w/ dot notation (image size w/o invisible characters)
  68.     return {Width: LTrim(sizeArray[1], Chr(8234)), Height: RTrim(sizeArray[2], Chr(8236))}
  69. }
  70.  
  71. ; This gets rid of the snip notification, if it still exists
  72. DismissSnipNotification(*)
  73. {
  74.     Windows.UI.Notifications.ToastNotificationManager.History.Clear("Microsoft.ScreenSketch_8wekyb3d8bbwe!App")
  75.     ExitApp()
  76. }
  77.  
  78. HookEvent(event, fnObj, pid := 0) => DllCall("SetWinEventHook", "UInt", event, "UInt", event, "Ptr", 0, "Ptr", CallbackCreate(fnObj), "UInt", pid, "UInt", 0, "UInt", 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement