{"id":1324,"date":"2010-11-02T14:58:52","date_gmt":"2010-11-02T14:58:52","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=1324"},"modified":"2010-11-02T14:58:52","modified_gmt":"2010-11-02T14:58:52","slug":"waiting-for-the-clipboard","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2010\/11\/02\/waiting-for-the-clipboard\/","title":{"rendered":"Waiting for the Clipboard"},"content":{"rendered":"<p>Today I was helping someone who was wanting to write a script to take screen-shots from one application and then paste those screen-shots into Microsoft Excel.  <\/p>\n<p>Initially things weren&#8217;t working reliably because the script didn&#8217;t factor in the time taken for the large bitmap of a screen-shot to exist in the clipboard after pressing the print screen button, before attempting to paste into Excel.  E.g. consider:<\/p>\n<pre name=\"code\" class=\"macroscript\">Press Print Screen\r\nSetFocus>Microsoft - Excel*\r\nPress CTRL\r\nSend>v\r\nRelease CTRL<\/pre>\n<p>The above is probably going to fail most of the time because a screen shot is a large bitmap and is going to take some time to arrive on the clipboard, but the script above performs a paste in Excel immediately after pressing print screen.  The print screen key being depressed and the clipboard containing the bitmap are not the same thing.<\/p>\n<p>While we could have just said &#8220;wait 5 seconds&#8221; and that would probably have been fine for evermore, it isn&#8217;t very sensitive and wouldn&#8217;t be ideal for a script that needs to run as fast as possible.  Ideally we only want to wait until we know the bitmap is in the clipboard.<\/p>\n<p>Text is usually smaller than a bitmap, but for large text items one way to make things bulletproof is to do something like this:<\/p>\n<pre name=\"code\" class=\"macroscript\">PutClipBoard>dummy\r\n\r\nSetFocus>source_app_title\r\nPress CTRL\r\nSend>c\r\nRelease CTRL\r\n\r\nLabel>wait_for_data\r\nWait>0.2\r\nGetClipBoard>clipdata\r\nIf>clipdata=dummy\r\n  Goto>wait_for_data\r\nEndif<\/pre>\n<p>By putting a known value onto the clipboard in the first place we can then have a little loop which keeps checking the clipboard until the returned value is not our known value.  We then know our CTRL-V has worked and that we can safely paste to the target application.<\/p>\n<p>But how can we do the same thing when the clipboard data is an image?  The above won&#8217;t work because GetClipBoard won&#8217;t return anything for non-textual data.  <\/p>\n<p>Well, Windows has a function called <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms649047(VS.85).aspx\">IsClipboardFormatAvailable<\/a> which will allow us to determine what kind of data is on the clipboard.  So we could use this in a similar way to above to see if the clipboard contains a bitmap or not.  Like this:<\/p>\n<pre name=\"code\" class=\"macroscript\">Let>CF_BITMAP=2\r\n\r\nLet>haveBMP=0\r\nPutClipBoard>dummy\r\n\r\nPress Print Screen\r\n\r\nWhile>haveBMP=0\r\n  LibFunc>user32,IsClipboardFormatAvailable,haveBMP,CF_BITMAP\r\n  Wait>0.2\r\nEndWhile<\/pre>\n<p>We could then paste it somewhere:<\/p>\n<pre name=\"code\" class=\"macroscript\">SetFocus>Document - WordPad\r\nPress CTRL\r\nSend>v\r\nRelease CTRL<\/pre>\n<p>It&#8217;s always nice to wait only as long as we have to and it makes the script more reliable and portable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was helping someone who was wanting to write a script to take screen-shots from one application and then paste those screen-shots into Microsoft Excel. Initially things weren&#8217;t working reliably because the script didn&#8217;t factor in the time taken for the large bitmap of a screen-shot to exist in the clipboard after pressing the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1324"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":3,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1327,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1324\/revisions\/1327"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}