Skip to main content
LynxKx
Inspiring
January 21, 2022
Answered

javascript to aquire clipboard contents

  • January 21, 2022
  • 2 replies
  • 1737 views

I recieved this code from an awesome community member ... but its for a mac user. I'm trying to show a friend with a PC how to use it... is there another way to code 'getting clipboard contents' without it being system specific?

 

Would calling out Windows instead of Macintosh and Finder work? I'm also not familiar with applescript - does that make a difference with PCs?

 

I'm still searching around to try and find java for clipboard contents ... but wanted to see if someone here had a solution.

 

function Main() {

  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;  
} 

 

This topic has been closed for replies.
Correct answer brian_p_dts

If clipboard contents is always a string, you can make a temp text frame in your active document, read the contents, and delete the frame. Not sure that would be any faster. What's the use of the clipboard contents? That might help us steer you in better direction. 

 

var getClipboard = function() {
try { var temptf = app.activeDocument.textFrames.add({geometricBounds: ["0pt","0pt","20pt","50pt"]}); } catch(e) { return ""; }
temptf.insertionPoints[0].select();
app.paste();
var contents = temptf.parentStory.contents;
temptf.remove();
return contents;
}

 

2 replies

rob day
Community Expert
January 21, 2022

I'm still searching around to try and find java for clipboard contents

 

Do you mean Java or JavaScript?

 

The JavaScript code you posted checks for the system name via File.fs and if it is Mac OS uses AppleScript to get the clipboard and if it’s not uses VisualBasic. This sends an alert with the result:

 

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;  
}

 

 

LynxKx
LynxKxAuthor
Inspiring
January 21, 2022

awesome, so the visual_basic langauge is for a PC ... I've been having some issue with the original code lagging quite a bit, sometimes if i toggle between files it will reset and work quickly, other times it is delayed almost 30 seconds. so i wasnt sure if there was simpler code to aquire clipboard contents. 

brian_p_dts
brian_p_dtsCorrect answer
Community Expert
January 21, 2022

If clipboard contents is always a string, you can make a temp text frame in your active document, read the contents, and delete the frame. Not sure that would be any faster. What's the use of the clipboard contents? That might help us steer you in better direction. 

 

var getClipboard = function() {
try { var temptf = app.activeDocument.textFrames.add({geometricBounds: ["0pt","0pt","20pt","50pt"]}); } catch(e) { return ""; }
temptf.insertionPoints[0].select();
app.paste();
var contents = temptf.parentStory.contents;
temptf.remove();
return contents;
}

 

Marc Autret
Brainiac
January 21, 2022

Hi @LynxKx 

 

Sorry I misread your question 😞

 

(Message deleted.)