9 Replies Latest reply on Jan 10, 2016 1:53 PM by RobertKyle

    Script to examine clipboard before pasting

    RobertKyle Level 2

      I've had some success using a beforePaste() event-listener to help users avoid pasting formatted text in places where they shouldn't. This script could be a lot smarter if it could look at the contents of the clipboard first. Even a simple count of the paragraphs would be a big help.

       

      From browsing this forum, it appears that the most common idea for checking clipboard contents in JS is to create a temporary text frame, paste into it and then get the contents of the temporary frame. But I seem to recall that AppleScript had direct access to the contents of the clipboard and have read that VBscript might as well.

       

      So: does anyone have a snippet that shows how to use VBscript to get the contents of the clipboard and pass it to javascript?

        • 2. Re: Script to examine clipboard before pasting
          Marc Autret Level 4

          I'm very interested in a robust solution too, involving either VB or AS depending on the OS.

          A good strategy could be to use the app.paste() method as a last resort, only when the OS-based scripted routine does not work or is not available. So we need a way to check that.

           

          Thank you Vamitul for the links.

           

          @+

          Marc

          • 3. Re: Script to examine clipboard before pasting
            RobertKyle Level 2

            Clipboard,vbs kinda sorta looks promising but I'm wondering about the references to IE. Does this script need IE to be running and/or available?

            • 4. Re: Script to examine clipboard before pasting
              Vamitul Level 4

              Available: yes.

              Running, i guess not.

              The point is you don't have access straight to the clipboard via vbs, but you have access to IE, and IE has access to the clipboard. Overall, i think the complications are not worth it.. just paste on a temp frame and do the checks, or, better yet, do the paste in the target frame, and if the pasted text does not fit the requirements do an app.undo()

              • 5. Re: Script to examine clipboard before pasting
                Harbs. Level 6

                The following works on Mac, and theoretically should work on Windows, but I don't have an easy way to test at the moment.

                 

                alert(GetClipboard());
                function GetClipboard(){
                    var clipboard;
                    if(File.fs == "Macintosh"){
                        var script = 'tell application "Finder"\nset clip to the clipboard\nend tell\nreturn clip';
                        clipboard = app.doScript (script,ScriptLanguage.APPLESCRIPT_LANGUAGE);
                    } else {
                        var script = 'Set objHTML = CreateObject("htmlfile")\r'+
                        'returnValue = objHTML.ParentWindow.ClipboardData.GetData("text")';
                        clipboard = app.doScript(script,ScriptLanguage.VISUAL_BASIC);
                    }
                    return clipboard;
                }
                
                • 6. Re: Script to examine clipboard before pasting
                  RobertKyle Level 2

                  On My (Windows) Machine, I get an alert, containing the entire text of your script...

                   

                  But when I change the first line to something like:

                   

                  var myClipboard = GetClipboard();

                   

                  the new variable contains the text of the clipboard. Genius!

                   

                  I did this much of a test: Selected a picture on an Indesign page and copied it. Now the myClipboard variable is null. Which is good, I think.

                   

                  Wondering what other ClipboardData.GetData options are available besides "text". My original idea is to get a paragraph count, and that certainly seems do-able with the result in string form. But if there's a way to get the applied styles for text on the clipboard, my beforePaste() listener could get much smarter.

                  • 7. Re: Script to examine clipboard before pasting
                    RobertKyle Level 2

                    I know I marked it as answered, but if someone can help with the next bit, I'll try to mark it as "answered better"

                     

                    The more I work on my event-listener, the more I realize that I really need to know if the clipboard contains Rich Text.

                     

                    I see snippets on various VB websites that refer to TextDataFormats, one of which being Rtf. I also see a method Clipboard.ContainsText(TextDataFormat format) that appears to be exactly what I need.

                     

                    But my VB skills are very weak and I cannot figure out how to plug a reference to Rtf or ContainsText into Harbs' excellent snippet.

                    • 8. Re: Script to examine clipboard before pasting
                      Kasyan Servetsky Level 5

                      Here's an interesting script for Mac only which uses clipboard. Can't test it because I'm on PC at the moment.

                      • 9. Re: Script to examine clipboard before pasting
                        RobertKyle Level 2

                        Well, I wish the Mac were an option but that's not in the cards. I miss the good old days of Applescript.

                         

                        My understanding of the situation is that there is no Clipboard object in javascript. There is one in some implementations of Visual Basic but not VBscript. Where the Clipboard object exists, it appears that there's a TextDataFormat.rtf which might be the info that I need. But I have no idea how to get it.

                         

                        This is for an event listener attached to a menu event. It's looking for a couple of problematic situations, such as the when the user has selected an entire story and is pasting multiple paragraphs to replace the selected text. If the clipboard contains plain text it will inherit the style of the last selected insertion point, which is usually not what we want. So in that case, I want o give the user a chance to cancel the paste and select a better insertion point. But if the user has copied styled text from another Adobe document, the paste should proceed normally. But since the script runs every time anyone pastes anything, there can't be any visible overheard.

                         

                        Best idea I have is to throw up a confirm() message asking the user if the clipboard text came from an Adobe file (formatted text) or not (plain). But s lot of users will find that confusing.