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

javascript to aquire clipboard contents

Contributor ,
Jan 21, 2022 Jan 21, 2022

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

 

TOPICS
Scripting
1.2K
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

Community Expert , Jan 21, 2022 Jan 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.
...
Translate
Guide ,
Jan 21, 2022 Jan 21, 2022

Hi @LynxKx 

 

Sorry I misread your question 😞

 

(Message deleted.)

 

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
Community Expert ,
Jan 21, 2022 Jan 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;  
}

 

 

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
Contributor ,
Jan 21, 2022 Jan 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. 

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
Community Expert ,
Jan 21, 2022 Jan 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;
}

 

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
Community Expert ,
Jan 21, 2022 Jan 21, 2022

Hi Brian, your first line is missing the properties object end brace I think it should be this?

 

var temptf = app.activeDocument.textFrames.add({geometricBounds: [0,0,20,50]});

 

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
Community Expert ,
Jan 21, 2022 Jan 21, 2022

Thaks, Rob. Also forced "pts" so we don't blow out the page if we're on inches. Need more coffee.

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
Contributor ,
Jan 21, 2022 Jan 21, 2022

I use the string for a couple different scripts I run in InDesign; 

  1. clipboard contents are used to assign alt text to anchored objects within my textframes
  2. clipboard contents are used to assign descriptive text to form fields 

 

I also assign shortcuts to the scripts for even quick use.

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
Community Expert ,
Jan 21, 2022 Jan 21, 2022

OK, try my updated code above. I guess I don't get, though, how CTRL+V is unsuitable for your needs.

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
Contributor ,
Jan 21, 2022 Jan 21, 2022
LATEST

Works! thank you so much! so far I'm not experiencing the delay I was having before so thats super awesome! 

yes, ctrl+v works fine but the scripts minimize amount of clicks and contain multiple functions:

  • my alt script allows me to assign custom alt text to an image without having to open the export options window > change source to custom and then pasting description
  • my forms script allows me to assign descriptive text as well as format the font, size and multiline at once. 

 

if theres only 1 or 2 instances in a file obviously i'm not saving loads of time, but i'm creating files with 50+ images/graphics and over 100 form fields and have the scripts with multi function so i'm just ctrl+c and then run my script shortcut it saves me LOOOOOADS of time 🙂

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