iseahound / ImagePut

A core library for images in AutoHotkey. Supports AutoHotkey v1 and v2.
https://www.autohotkey.com/boards/viewtopic.php?f=83&t=76633
MIT License
116 stars 24 forks source link

Add support for Windows 11 Explorer tabs #47

Closed TheCrether closed 4 months ago

TheCrether commented 4 months ago

Hi! This time the commits didn't get mangled for some reason

I came across your repo yesterday since I was searching pasting clipboard images into the explorer. Great work!

On Windows 10 it works great but when I tried it on a Windows 11 PC with an Explorer with multiple tabs, the wrong "tab" was selected.

Here is a fix where the "active" Tab of the most recent explorer gets chosen (works for Windows 10 too since the explorer is built similarly 😂)

iseahound commented 4 months ago

Appreciated.

  1. Can you describe your use case for the ImagePutExplorer function? Personally Figma doesn't seem to allow me to save assets directly to a file but would rather place them on the clipboard instead.
  2. Is the default behavior of throwing it onto the desktop acceptable?
TheCrether commented 4 months ago

I wanted to have the possibility to take a screenshot with the standard Win+Shift+S shortcut and then paste it into the explorer. It looks like this:

#HotIf WinActive('ahk_exe explorer.exe')
$^v:: {
  clipboard := ClipboardAll()
  try {
    t := ImagePut.ImageType(clipboard) ; this throws an error if it isn't an image
    if InStr(t, "Clipboard") {
      ImagePutExplorer(clipboard)
    } else {
      Send("^v")
    }
  } catch {
    Send("^v")
  }
}
#HotIf
TheCrether commented 4 months ago

As for the standard action of putting on the Desktop, I think that it is okay but another good place would be the Pictures folder of the user.

iseahound commented 4 months ago

Thanks for satiating my curiosity. ImagePutExplorer is one of my personal functions, I wasn't sure if anyone else would need such a thing. I've also updated some of the clipboard functions and I've propagated your changes to BitmapToExplorer as well. (StreamToExplorer only detects PNGs on the clipboard). See: https://github.com/iseahound/ImagePut/commit/6f840ef43e40e40cd0caef4206ab3f61ec608441

Also I deleted the logic for putting it onto the desktop by default, you'll need to specify a default directory as in ImagePutExplorer(image, A_Desktop)

TheCrether commented 3 months ago

I wanted to have the possibility to take a screenshot with the standard Win+Shift+S shortcut and then paste it into the explorer. It looks like this:

#HotIf WinActive('ahk_exe explorer.exe')
$^v:: {
  clipboard := ClipboardAll()
  try {
    t := ImagePut.ImageType(clipboard) ; this throws an error if it isn't an image
    if InStr(t, "Clipboard") {
      ImagePutExplorer(clipboard)
    } else {
      Send("^v")
    }
  } catch {
    Send("^v")
  }
}
#HotIf

so, after using this a bit, it seems as though checking the ImageType before just using ImagePutExplorer, throws a critical memory error when copying files (which makes the script actually crash).

So I've resorted to using this:

$^v:: {
  clipboard := ClipboardAll()
  try {
    ImagePutExplorer(clipboard)
  } catch {
    Send("^v")
  }
}
iseahound commented 3 months ago

There's a possibility I didn't account for the null case when nothing is on the clipboard. I'll look into it later today, any error messages or steps would be helpful (esp the state of the clipboard before crashing)

iseahound commented 3 months ago

Fixed by b288770a726e28d5c154567d56952e20e8d7e6c1.

Note that you don't need to pass a binary buffer object ClipboardAll() but can also just pass the prototype constructor ImagePutWindow(ClipboardAll) instead. The reason for this new check is because certain AutoHotkey programmers are abusing the ClipboardAll(data, size) constructor to create arbitrary buffer objects.

TheCrether commented 3 months ago

Ohh okay, I see. For some reason the error does not appear when passing the prototype constructor. Here a screenshot of when it happens: image

This only happens when copying files but for some reason it does not always appear. After a bit of random testing I found that it only appeared combined with TeraCopy so I don't think it's an actual bug from your side. I'll reply with a comment if it happens again and I can reproduce it

iseahound commented 3 months ago

Thanks for looking into this. It would help if you'd check for the ClipboardAll() ptr and size value when the error occurs on your end.

To give additional clarity, let's say someone uses the ClipboardAll(data, size) constructor to directly load a GIF into the buffer. I'd like to process this as a normal buffer object and not check the clipboard. ImagePut offers a variety of ways to skip the uncertain ImageType function such as explicitly declaring your input type: ImagePutWindow({clipboard: true}) which might not be what you'd want in this case. If you'd like some help structuring your code, I'd use a producer-consumer format:

https://github.com/telppa/PaddleOCR-AutoHotkey/blob/main/12.%20OCR%20using%20Snipping%20Tool.ahk

PaddleOCR is one of the tools that uses ImagePut. Specifically, you'd want to check for ClipboardType equals to 2 to indicate an image.

TheCrether commented 3 months ago

Thank you! I didn't implement a full producer consumer format but this is enough for my purposes (at the moment):

isImageInClipboard := false
ClipboardChanged(ct) {
  global isImageInClipboard
  isImageInClipboard := ct == 2
}
OnClipboardChange(ClipboardChanged)

#HotIf WinActive('ahk_exe explorer.exe')
$^v:: {
  if not isImageInClipboard {
    return
  }
  try {
    ImagePutExplorer(ClipboardAll)
  } catch {
    Send("^v")
  }
}
#HotIf