Author Topic: Capture "Print Screen" with a Script file  (Read 1250 times)

0 Members and 1 Guest are viewing this topic.

Padanges

  • Newbie
  • *
  • Posts: 179
Capture "Print Screen" with a Script file
« on: September 09, 2016, 09:09:28 AM »
Hi,
is it possible for a script to create a clipboard content file? For example, after pressing "Print Screen" I get the screen captured into memory, but can I create an object which has that memory (paste) content? If we have a text and an image copied simultaneously - can we access both of them separately? Can we verify whether it's a proper txt/bmp format? I would intend then to save the captured clipboard as a *.JPG/*.TXT file with some index number in the file name.


Regards

RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #1 on: September 10, 2016, 12:20:31 AM »
Clipboard access from active scripting, using standard available objects, is very limited. Only with specific ActiveX objects this would be feasible.
Even to just copy/paste text there is the need to use tricks like this:
Code: [Select]
function CopyTextToClipboard(sTxt){
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.value = sTxt;
    oTb.select();
    oTb = null;
    oIe.ExecWB(12,0);
    oIe.Quit();
    oIe = null;
}                 

function GetTextFromClipboard(){
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.focus();
    oIe.ExecWB(13,0);
    var s = oTb.value;
    oIe.Quit();
    oIe = null;
    return s;
}
CopyTextToClipboard("Hello, World!");
pdfe.echo(GetTextFromClipboard());

The usual method to copy both images and text to the clipboard is by using the RTF format. Is this the format you are tying to deal with? Don't know any ready available trick to access this type of data from active scripting. Probably with MS Word automation this may be possible.

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #2 on: September 10, 2016, 07:33:07 AM »
Thanks now we can capture clipboard text. But how do we GetImageFromClipboard()?
sLister can copy both text and image - I was wondering whether I capture both using scripts.

RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #3 on: September 13, 2016, 01:30:16 AM »
But how do we GetImageFromClipboard()?
Not possible without a dedicated ActiveX control or a trick with MS Word, Libre/OpenOffice, or another similar app, automation.
Or executing a PowerShell script, like this ones.
Quote
sLister can copy both text and image - I was wondering whether I capture both using scripts.
Not the sLister, but the Sumatra PDF. Indeed it puts in the clipboard both formats, when the selection includes multiple content. Unfortunately doesn't provide a mixed content format, like RTF.

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #4 on: September 13, 2016, 09:02:18 AM »
That's quite off-putting. I think binding scripts with windows powershell is not a right way to go.
OK. So could you give some hints on how to go about with ActiveX? Perhaps these links could be useful:
http://www.codeproject.com/Articles/25967/Clipboard-ActiveX-for-Image-Copy-Paste-into-Web-Fo
http://alvinalexander.com/java/java-clipboard-image-copy-paste

RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #5 on: September 14, 2016, 02:32:18 PM »
If you are OK installing a third-party tool, then install a 32-bit version of ImageMagick, making sure you selected the "Install ImageMagickObject OLE Control..." additional tasks option, and then you can save the image in the clipboard, and process it, like this:
Code: [Select]
var img = new ActiveXObject("ImageMagickObject.MagickImage.1");
//save clipboard to file
msgs = img.Convert("clipboard:", "c:\\temp\\image.bmp");

//create thumbnail
msgs=img.Convert("c:\\temp\\image.bmp","-thumbnail","244x244>","-background","#ffffffffffff0000","-gravity","center","-extent","244x244",
"(","+clone","-background","black","-shadow","80x3+4+4",")","+swap","-background","none","-layers","merge","+repage",
"-background","#ffffffffffff0000","-gravity","center","-extent","256x256","c:\\temp\\image.png");

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #6 on: September 15, 2016, 08:05:56 PM »
Quote
If you are OK installing a third-party tool, then install a 32-bit version of ImageMagick
If there's no better way to capture image clipboard data - ImageMagick is fine. It's a great tool - it is even allowed to be used freely for personal or commercial purposes. Thank you for the solution - the code works fine now!

I have a question about your CopyTextToClipboard/GetTextFromClipboard methods. They seem to work "a bit slower" than the usual code and perhaps that is because of the While cycles with the Sleep() method:
Code: [Select]
while(oIe.ReadyState!=4) WScript.Sleep(20);
while(oIe.document.readyState!='complete') WSript.Sleep(20);
but removing these lines gives no problem to the methods. And trying to paste ~200kb of clipboard text data raises no issues (other than the waiting time). Are they really necessary? What should they help with?

RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #7 on: September 16, 2016, 04:17:52 PM »
I have a question about your CopyTextToClipboard/GetTextFromClipboard methods. They seem to work "a bit slower" than the usual code and perhaps that is because of the While cycles with the Sleep() method:
Code: [Select]
while(oIe.ReadyState!=4) WScript.Sleep(20);
while(oIe.document.readyState!='complete') WSript.Sleep(20);
but removing these lines gives no problem to the methods. And trying to paste ~200kb of clipboard text data raises no issues (other than the waiting time). Are they really necessary? What should they help with?
This is not my code, but these sleep values are indeed too high. A sleep(1) is enough, and the while(oIe.document.readyState!='complete') line is not really needed in this case. Checking if the browser is ready to accept interaction with the loaded document is really needed, or the script will fail if a command is sent before, but with modern fast PCs it gets ready soon than when this code got write. The amount of what is copied/pasted is not the issue here.

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #8 on: September 16, 2016, 08:46:55 PM »
Thanks for the reply. I've modified the code lines accordingly.
Lastly, I would like to ask, whether we can escape the need to install ImageMagick at all. I found c++ code sample where you can register a *.dll for creating ImageMagickObject:
https://www.autoitscript.com/forum/topic/117914-imagemagickobject-object/?page=2
Do you think it's possible to get our clipboard image capture function using only "\ImageMagickObject.dll" with JS instead?

RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #9 on: September 17, 2016, 04:33:04 PM »
Do you think it's possible to get our clipboard image capture function using only "\ImageMagickObject.dll" with JS instead?
At least the ImageMagickObject.dll I have, with just 100KB, for sure just implements the COM handling thing. It needs to have many of the other DLL's present, to do the work.

A nice opportunity for you to learn how to code a COM server. ;)

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #10 on: September 17, 2016, 06:04:08 PM »
COM-what?  ;D  it's a bit over the top for me. but with right the code samples and a large manual - miracles do happen 8)
but I guess you say that we can't get away with only one dll.

Padanges

  • Newbie
  • *
  • Posts: 179
Re: Capture "Print Screen" with a Script file
« Reply #11 on: September 23, 2016, 09:35:54 PM »
Have you ever tried working with WshExtra.dll ? It has Clipboard object.
http://www.script-coding.com/WshExtra.html


RTT

  • Administrator
  • *****
  • Posts: 775
Re: Capture "Print Screen" with a Script file
« Reply #12 on: September 23, 2016, 11:44:07 PM »
Have you ever tried working with WshExtra.dll ? It has Clipboard object.
http://www.script-coding.com/WshExtra.html
First time I see this one. But it seems it is limited to text only, so the IE trick does the same without the need to install and register anything else.
I haven't needed clipboard access from scripts yet, and I can always add this functionality to the scripts API.