Hold Right-Click to Save Image As? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Hold Right-Click to Save Image As?

Post by feeko » 04 Feb 2023, 13:00

Can I please get some help on creating a simple script for Firefox that saves an image to the desktop automatically after holding right-click on the image?

This is instead of having to right-click > save image as > name file > save.

So far I found these things:

Code: Select all

Rbutton::
;
;it gives you half a second to either complete the double click
;or to complete the press-and-hold cycle. If neither happens
;you get a normal single click response.
;
keywait, rbutton, t0.5
if errorlevel = 1
...
https://www.autohotkey.com/board/topic/7648-right-mouse-button-added-functionality-doubleclick-it/

Code: Select all

#RButton::
   MouseGetPos,,,,Control
   IfInString Control, Internet Explorer
      Send {RButton}s{Enter}
Return
https://www.autohotkey.com/board/topic/10127-ie-right-click-save-picture-as/

User avatar
mikeyww
Posts: 26656
Joined: 09 Sep 2014, 18:38

Re: Hold Right-Click to Save Image As?  Topic is solved

Post by mikeyww » 04 Feb 2023, 17:19

Code: Select all

; This script saves a Firefox image to the Windows desktop
#Requires AutoHotkey v1.1.33
firefox := "ahk_class MozillaWindowClass ahk_exe firefox.exe"
#If WinActive(firefox)
~RButton::
KeyWait RButton, T.3
If ErrorLevel {        ; Held
 SoundBeep 1500
 KeyWait RButton
 Sleep 100
 Send v                ; Select menu option to save the image
 WinWaitActive Save Image ahk_class #32770 ahk_exe firefox.exe,, 3
 If !ErrorLevel {
  ControlGetText fn, Edit1
  FileRecycle % filePath := A_Desktop "\" fn
  ControlSetText Edit1, % filePath
  Sleep 200
  Send `n              ; Save the image
  Sleep 800
  Run % filePath       ; Display the image
 } Else MsgBox 48, Error, Window was not found.
}
Return
#If

Code: Select all

; This script saves a Firefox image to the Windows desktop
#Requires AutoHotkey v2.0
firefox := "ahk_class MozillaWindowClass ahk_exe firefox.exe"
#HotIf WinActive(firefox)
~RButton:: {
 If !KeyWait('RButton', 'T.3') {  ; Held
  SoundBeep 1500
  KeyWait 'RButton'
  Sleep 100
  Send 'v'                        ; Select menu option to save the image
  If WinWaitActive('Save Image ahk_class #32770 ahk_exe firefox.exe',, 3) {
   Try FileRecycle filePath := A_Desktop "\" ControlGetText('Edit1')
   ControlSetText filePath, 'Edit1'
   Sleep 250
   Send '`n'                      ; Save the image
   Sleep 800
   Run filePath                   ; Display the image
  } Else MsgBox 'Window was not found.', 'Error', 48
 }
}
#HotIf

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Hold Right-Click to Save Image As?

Post by feeko » 05 Feb 2023, 00:42

@mikeyww thank you! This is going to save me a lot of time for my work over the long-run.

Code: Select all

 WinWaitActive Save Image ahk_class #32770 ahk_exe firefox.exe,, 3
 If !ErrorLevel {
  ControlGetText fn, Edit1
  FileRecycle % filePath := A_Desktop "\" fn
  ControlSetText Edit1, % filePath
  Sleep 200
  Send `n              ; Save the image
  Sleep 800
  Run % filePath       ; Display the image
 } Else MsgBox 48, Error, Window was not found.
}
Return
#If
What does this part of the script do?

If there's an error it recycles the saved image?

-

I used the 1.1.33 version and this is the error that popped up. The image does save to the desktop still though.
Attachments
Screenshot 2023-02-04 213612.jpg
Screenshot 2023-02-04 213612.jpg (30.42 KiB) Viewed 614 times

User avatar
mikeyww
Posts: 26656
Joined: 09 Sep 2014, 18:38

Re: Hold Right-Click to Save Image As?

Post by mikeyww » 05 Feb 2023, 07:23

Hello,

Here is how the script works. It waits for the button to be released. It then sends "v" to the context menu, triggering the "Save Image As..." menu option. It then gets the text from the edit control, which contains the file name. It prepends the user's desktop path. If that file already exists, the existing file is recycled. The edit control is updated to include the full path with the desktop. A line feed or Enter is sent to submit the dialog and save the file. The file is then opened by the default image viewer. It may be possible to use ControlClick instead of Enter, but my success with ControlClick varies.

In testing the script, I found that the sleeps are important, because some of the steps, such as setting the control and saving the file, take significant time. If you increase the sleeps, the script should be OK. The script contains three sleeps. If you receive the error when the dialog window is visible, increase the second sleep. Otherwise, increase the third sleep. I experienced the same error in my testing, which is why I increased the second sleep, but I showed it only in my v2 script as posted.

There are undoubtedly some other ways to save images on Web pages, too. This is a relatively simple approach that would typically work.

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Hold Right-Click to Save Image As?

Post by feeko » 05 Feb 2023, 21:21

Thank you for the explanation.

Do you know why the error window comes up?

User avatar
mikeyww
Posts: 26656
Joined: 09 Sep 2014, 18:38

Re: Hold Right-Click to Save Image As?

Post by mikeyww » 05 Feb 2023, 21:28

Yes. It comes up because the sleeps are too short. Either the dialog window has not been resolved, or the file has not been created yet. Creating the file can take a second or so, and this is the purpose of the sleep. That explains why I wrote what I wrote in my previous post. An alternative would be a loop that iterates until the file exists.

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Hold Right-Click to Save Image As?

Post by feeko » 05 Feb 2023, 21:54

I couldn't get rid of the error window by tinkering with the sleep times, but I removed these 2 lines (below) and the error window stopped coming up.

Code: Select all

  Sleep 800
  Run % filePath       ; Display the image

Is it bad to remove those 2 lines for the script?

What was the purpose of the "display the image" with the "Run" code?

User avatar
mikeyww
Posts: 26656
Joined: 09 Sep 2014, 18:38

Re: Hold Right-Click to Save Image As?

Post by mikeyww » 05 Feb 2023, 22:00

The Run was a way to confirm that the script worked. You would otherwise never know whether the script worked, without manually checking the output file. Run saves time by doing that work for you. It leverages the power of the script to take away some of your manual labor, and this is one goal of computer programs, from my perspective. At least during the testing process, this type of confirmation can be useful.

As long as the Run is not needed, then the final sleep is also not needed. Enjoy!

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Hold Right-Click to Save Image As?

Post by feeko » 05 Feb 2023, 22:08

Got it, thanks again!

Post Reply

Return to “Ask for Help (v1)”