Skip to content

Instantly share code, notes, and snippets.

@peterthehan
Last active November 13, 2022 21:42
Show Gist options
  • Save peterthehan/56814d5d6dd254d952ca02c18263d415 to your computer and use it in GitHub Desktop.
Save peterthehan/56814d5d6dd254d952ca02c18263d415 to your computer and use it in GitHub Desktop.
Paste into Explorer to save your clipboard as a webp, jpeg, png, or txt file.
#NoEnv
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%
return
getActiveExplorerPath()
{
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd == explorerHwnd)
{
return window.Document.Folder.Self.Path
}
}
}
}
mapClipboardFormatToFileType()
{
; https://docs.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats#constants
if DllCall("IsClipboardFormatAvailable", "uint", 1)
return "text"
if DllCall("IsClipboardFormatAvailable", "uint", 2)
return "image"
return "none"
}
getFileExtension(fileType, alt)
{
return fileType == "text" ? "txt" : alt
}
writeFile(filePath)
{
fileExtension := SubStr(filePath, -2)
if % fileExtension == "txt" {
FileAppend, %Clipboard%, %filePath%
return
}
Run, i_view64.exe /clippaste /convert=%filePath% /silent
}
getFilePath(alt)
{
if !FileExist("i_view64.exe") {
TrayTip, Missing IrfanView, Download the portable version of IrfanView (i_view64.exe) at https://www.irfanview.com/ to use this script.
return
}
folderPath := getActiveExplorerPath()
if !FileExist(folderPath) {
TrayTip, Invalid Destination Folder, Destination "%folderPath%" is invalid.
return
}
fileType := mapClipboardFormatToFileType()
if % fileType == "none" {
TrayTip, Invalid Clipboard Format, Clipboard must contain either text or bitmap data to save.
return
}
fileExtension := getFileExtension(fileType, alt)
return % folderPath . "\" . A_Now . "." . fileExtension
}
#IfWinActive, ahk_exe Explorer.EXE
^1::
filePath := getFilePath("webp")
writeFile(filePath)
return
^2::
filePath := getFilePath("jpg")
writeFile(filePath)
return
^3::
filePath := getFilePath("png")
writeFile(filePath)
return
#IfWinActive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment