Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to examine clipboard before pasting

Guest
Dec 02, 2015 Dec 02, 2015

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?

TOPICS
Scripting
3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 02, 2015 Dec 02, 2015

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 =

...
Translate
Advisor ,
Dec 02, 2015 Dec 02, 2015
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 02, 2015 Dec 02, 2015

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 02, 2015 Dec 02, 2015

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()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 02, 2015 Dec 02, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 02, 2015 Dec 02, 2015

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 02, 2015 Dec 02, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 21, 2015 Dec 21, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 10, 2016 Jan 10, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 10, 2016 Jan 10, 2016
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines