0

I am trying to create a batch script which puts a screenshot on the clipboard for me to save in another application.

I am using the "^{PRTSC}" and have copied code from another posting here (I would ask/comment there but the listing is closed and I do not have enough points to post there.)

When I run the following line line I get no errors:

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys("%{PRTSC}")

But when I create a new document in Photoshop and select Paste ctrl+v, nothing is pasted from the clipboard.

Clearly the code is not putting a screenshot in the clipboard.

( I do not want to use Navcmd )

Lee_Dailey
  • 6,339
  • 2
  • 17
  • 22
  • 2
    Does this answer your question? [How can I do a screen capture in Windows PowerShell?](https://stackoverflow.com/questions/2969321/how-can-i-do-a-screen-capture-in-windows-powershell) – ack Mar 07 '21 at 13:34
  • 1
    Why are you using batch? Just use PowerShell directly. There are many prebuilt, [sample/example scripts all over the web for your use case](https://duckduckgo.com/?q=%27powershell+take+screen+shot%27&t=h_&ia=web). There are even modules in the MS powershellgallery.com for this sort of thing. For example as search for Find-Module -Name '*capture*'. You can call PowerShell scripts from a batch file. Lastly, doing this will capture the console window you ran this from unless you run this minimized or hidden via a shortcut and you must immediately select your target window after the double click. – postanote Mar 08 '21 at 01:01
  • or rather that do this copy/prtsc thing, I'd bet PhotoShopas an object module that would allow you further automation to capture stuff from the screen and insert it into your current PhotoShop window via scripting. Again, I never needed PhotoShop for any reason. So, no experience automating it. – postanote Mar 08 '21 at 01:15
  • A quick web serach: ['photoshop object model'](https://www.bing.com/search?q=%27photoshop%20object%20model%27&qs=n&form=QBRE&sp=-1&pq=%27photoshop%20object%20model%27&sc=1-24&sk=&cvid=0C78E72F72E2447881F8B8E12AECEAB7). The first hit: [Adobe Photoshop CC 2015 Scripting Guide](http://wwwimages.adobe.com/www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-cc-scripting-guide-2015.pdf#:~:text=Photoshop%20Object%20Model.%20A%20document%20object%20model%20%28DOM%29,defined%20for%20that%20application%29%20through%20a%20scripting%20language) – postanote Mar 08 '21 at 01:15
  • The documentation says you can't send prtsc. – user14797724 Mar 09 '21 at 01:28
  • See https://winsourcecode.blogspot.com/2019/06/simulates-printscreen-key-sendkeys.html?m=1 – user14797724 Mar 09 '21 at 01:33

2 Answers2

0

Here is an old batch script that use a module in VB.net and save a screenshot as jpeg image with system date:


/*
@echo off & cls & color FC
Mode 30,3
::Autor Delmar Grande
::http://bbat.forumeiro.com/t248-bat-exe-printscreen-usando-vb-net
::Data  Qui 11 Jul 2013
:: Modified by Hackoo on 09/09/2016 to save image with system date
Title PrintScreen by Delmar Grande and modified by Hackoo
Rem Just adding a little timeout to organise our screenshot
Timeout /T 4 /Nobreak>nul
findstr "'%skip%VB" "%~f0">"%tmp%\%~n0.vb"
for /F %%i in ('dir /B /S ^"%WinDir%\Microsoft.NET\Framework\vbc.exe^"') do set "vbc=%%i"
if /i "%vbc%"=="" cls & color 1c & echo This script needs the framework & pause & Exit
cls
%vbc% /nologo /out:"%tmp%\%~n0.exe" "%tmp%\%~n0.vb"
"%tmp%\%~n0.exe"
del "%tmp%\%~n0.vb" >NUL 2>&1
del "%tmp%\%~n0.exe" >NUL 2>&1
exit
*/
Imports System.Windows.Forms 'VB
Module ModulePrintscreen 'VB
  Sub Main() 'VB
  Dim MaDate As String 'VB
  SendKeys.SendWait("{%}({PRTSC})") 'VB
  If My.Computer.Clipboard.ContainsImage() Then 'VB
  MaDate = Format(Now,"dd-MM-yyyy_hh-mm-ss") 'VB
  My.Computer.Clipboard.GetImage.Save(MaDate & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 'VB
  End If 'VB
  End Sub 'VB
End Module 'VB
Hackoo
  • 15,943
  • 3
  • 28
  • 59
0

As far as I know, windows keyboard supports two shortcuts to capture the screen:

  • PrtScn Captures the entire desktop
  • Alt+PrtScn Captures the active window.

Per the title and your description, ^{PRTSC} should act as if it's pressing Ctrl+PrtScn, which will perform the same action as PrtScn.

Assuming you just want to capture the active window, running the following command from a batch file will work:

powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('%%{PRTSC}')"

There are a few differences:

  • Using the SendWait implementation from .NET. The COM SendKeys implementation has some issues, notably around how it reacts to console windows and special keys.
  • I'm using single quotes to quote the string inside the command, otherwise the double quote will end the quoted string being sent to PowerShell.
  • Using % to simulate the Alt key, and more, since it's in a batch file, escaping the % with %%.
Anon Coward
  • 4,910
  • 2
  • 17
  • 25
  • there's also Win+PrintScreen in newer Windows 10 which saves the screenshot directly as \*.png – phuclv Mar 27 '21 at 13:55