Skip to main content
Inspiring
March 1, 2023
解決済み

• Copy a string to clipboard (at launch) via javascript

  • March 1, 2023
  • 返信数 3.
  • 2532 ビュー

Hello -

 

I would like to copy a string to the clipboard (on Mac) when Illustrator is launch.

 

So I copy-paste the below script into a new .jsx file (Startup Scripts folder), but I get an error…

Any idea why I get this error?

 

var SWTY = "bGE-PJk";

// var x = app.documents[0].selections.toString();
// This should be escaped OK

var sh = app.system("osascript -e 'set the clipboard to \"" + SWTY + "\"'");
  sh ? alert('Failed?') : alert('Done…');

 

 

このトピックへの返信は締め切られました。
解決に役立った回答 jduncan

If you need the onClick functionality for your button in a palette it's messy and requires using BridgeTalk (docs) but doable.

 

The first example below sends all the code you need to execute as a string through BridgeTalk to Illustrator allowing you to use a palette.

 

Credit to @Davide_Barranca12040269 for the idea.

 

 

/*
ButtonOnClickFromPalette.jsx for Adobe Illustrator
--------------------------------------------------

Execute a palette button onClick function via BridgeTalk.

Created in response to this question on the Adobe forum:
https://community.adobe.com/t5/illustrator-discussions/copy-a-string-to-clipboard-at-launch-via-javascript/td-p/13616550

This script is distributed under the MIT License.
See the LICENSE file for details.

Versions:
0.1.0 initial release
*/

var SWD = "SOME TEXT TO COPY";
var URL = "https://adobe.com/";

// the BridgeTalk Object
var bt = new BridgeTalk();

// the communication target
bt.target = "illustrator";

// The script to be executed as a String
var codeAsString =
  "app.activeDocument.selection = null;" +
  "\n" +
  "var tempObj = app.activeDocument.pathItems.add();" +
  "\n" +
  "var myText = app.activeDocument.textFrames.add();" +
  "\n" +
  'myText.contents = "' +
  SWD +
  '";' +
  "\n" +
  "tempObj.selected = true;" +
  "\n" +
  "myText.selected = true;" +
  "\n" +
  "app.copy();" +
  "\n" +
  "tempObj.remove();" +
  "\n" +
  "myText.remove();" +
  "\n" +
  'var html = new File(Folder.temp.absoluteURI + "/aisLink.html");' +
  "\n" +
  'html.open("w");' +
  "\n" +
  "var htmlBody = " +
  "\"<html><head><META HTTP-EQUIV=Refresh CONTENT='0; URL=" +
  URL +
  "'></head><body><p></body></html>\";" +
  "\n" +
  "html.write(htmlBody);" +
  "\n" +
  "html.close();" +
  "\n" +
  "html.execute();";

// assign to the object's body the message
bt.body = codeAsString;

// make your dialog (not palette)
var win = new Window("palette");
win.text = "DC TOOLS";
// make your button
var button = win.add("button", undefined, "PACK");
// add all the things you want done when button is clicked
button.onClick = function () {
  // send the message to the target app
  bt.send();
};
// show the dialog
win.show();

 

 

If you don't need the functionality of a palette you can do the same things much cleaner/easier just using a dialog like below.

 

 

var SWD = "SOME TEXT TO COPY";

// make your dialog (not palette)
var win = new Window("dialog");
win.text = "DC TOOLS";
// make your button
var button = win.add("button", undefined, "PACK");
// add all the things you want done when button is clicked
button.onClick = function () {
  // clear the current selection
  app.activeDocument.selection = null;
  // add temp objects to hold text for copying
  var tempObj = app.activeDocument.pathItems.add();
  var myText = app.activeDocument.textFrames.add();
  myText.contents = SWD;
  tempObj.selected = true;
  myText.selected = true;
  // copy the text
  app.copy();
  // remove the two temp object made for copying
  tempObj.remove();
  myText.remove();
  openURL("https://adobe.com/");
  win.close();
};
// show the dialog
win.show();

function openURL(url) {
  var html = new File(Folder.temp.absoluteURI + "/aisLink.html");
  html.open("w");
  var htmlBody =
    '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' +
    url +
    '"></head><body> <p></body></html>';
  html.write(htmlBody);
  html.close();
  html.execute();
}

 

 

返信数 3

dimitri_cas作成者
Inspiring
March 2, 2023

Ohhh and by the way…

that doesn't ssems to work neiter  😛

button2.graphics.font = ScriptUI.newFont ("Helvetica", "Bold", 11);
// button2.pointSize = 15;
// button2.text.font.size = "12pt";

 

 

jduncan
Community Expert
jduncanCommunity Expert解決!
Community Expert
March 2, 2023

If you need the onClick functionality for your button in a palette it's messy and requires using BridgeTalk (docs) but doable.

 

The first example below sends all the code you need to execute as a string through BridgeTalk to Illustrator allowing you to use a palette.

 

Credit to @Davide_Barranca12040269 for the idea.

 

 

/*
ButtonOnClickFromPalette.jsx for Adobe Illustrator
--------------------------------------------------

Execute a palette button onClick function via BridgeTalk.

Created in response to this question on the Adobe forum:
https://community.adobe.com/t5/illustrator-discussions/copy-a-string-to-clipboard-at-launch-via-javascript/td-p/13616550

This script is distributed under the MIT License.
See the LICENSE file for details.

Versions:
0.1.0 initial release
*/

var SWD = "SOME TEXT TO COPY";
var URL = "https://adobe.com/";

// the BridgeTalk Object
var bt = new BridgeTalk();

// the communication target
bt.target = "illustrator";

// The script to be executed as a String
var codeAsString =
  "app.activeDocument.selection = null;" +
  "\n" +
  "var tempObj = app.activeDocument.pathItems.add();" +
  "\n" +
  "var myText = app.activeDocument.textFrames.add();" +
  "\n" +
  'myText.contents = "' +
  SWD +
  '";' +
  "\n" +
  "tempObj.selected = true;" +
  "\n" +
  "myText.selected = true;" +
  "\n" +
  "app.copy();" +
  "\n" +
  "tempObj.remove();" +
  "\n" +
  "myText.remove();" +
  "\n" +
  'var html = new File(Folder.temp.absoluteURI + "/aisLink.html");' +
  "\n" +
  'html.open("w");' +
  "\n" +
  "var htmlBody = " +
  "\"<html><head><META HTTP-EQUIV=Refresh CONTENT='0; URL=" +
  URL +
  "'></head><body><p></body></html>\";" +
  "\n" +
  "html.write(htmlBody);" +
  "\n" +
  "html.close();" +
  "\n" +
  "html.execute();";

// assign to the object's body the message
bt.body = codeAsString;

// make your dialog (not palette)
var win = new Window("palette");
win.text = "DC TOOLS";
// make your button
var button = win.add("button", undefined, "PACK");
// add all the things you want done when button is clicked
button.onClick = function () {
  // send the message to the target app
  bt.send();
};
// show the dialog
win.show();

 

 

If you don't need the functionality of a palette you can do the same things much cleaner/easier just using a dialog like below.

 

 

var SWD = "SOME TEXT TO COPY";

// make your dialog (not palette)
var win = new Window("dialog");
win.text = "DC TOOLS";
// make your button
var button = win.add("button", undefined, "PACK");
// add all the things you want done when button is clicked
button.onClick = function () {
  // clear the current selection
  app.activeDocument.selection = null;
  // add temp objects to hold text for copying
  var tempObj = app.activeDocument.pathItems.add();
  var myText = app.activeDocument.textFrames.add();
  myText.contents = SWD;
  tempObj.selected = true;
  myText.selected = true;
  // copy the text
  app.copy();
  // remove the two temp object made for copying
  tempObj.remove();
  myText.remove();
  openURL("https://adobe.com/");
  win.close();
};
// show the dialog
win.show();

function openURL(url) {
  var html = new File(Folder.temp.absoluteURI + "/aisLink.html");
  html.open("w");
  var htmlBody =
    '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' +
    url +
    '"></head><body> <p></body></html>';
  html.write(htmlBody);
  html.close();
  html.execute();
}

 

 

dimitri_cas作成者
Inspiring
March 3, 2023

Hey jduncan  🙂

 

Many thx for your reply, your time and your talent…

Will definitively give it a try ASAP.

 

Enjoy your day.

 

 

 

- Dimitri

 

jduncan
Community Expert
Community Expert
March 1, 2023

So this is a crazy way to do this but it's the only way I can sort out how to run a system command from Ai (requires Photoshop). Since Ai doesn't have the `app.system()` method, we are going to send a Bridge Talk message (docs) to Photoshop to execute the system command for us.

 

Notes:
  1. Photoshop must be installed and open on your system
  2. This file needs to be placed in your Ai start up scripts folder (docs)

 

Before this idea, I tried a few different ways of doing this all within Ai but none of them worked...

 

/**
 * AiCopyToClipboardViaPhotoshop.jsx
 *
 * Run a Mac system command from Illustrator via the `app.system()` Photoshop method
 * using Bridge Talk (https://extendscript.docsforadobe.dev/interapplication-communication/)
 *
 * Notes:
 * 1. Photoshop must be installed and open on your system
 * 2. This file needs to be placed in your Ai start up scripts folder
 * (https://extendscript.docsforadobe.dev/introduction/scripting-for-specific-applications.html?highlight=startup#startup-scripts)
 */

// item to be copied to clipboard
var SWTY = "bGE-PJk";

// use Bridge Talk to run a system command in Photoshop (must be installed)
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = 'app.system("echo ' + SWTY + ' | pbcopy")';

// send the message
bt.send();

 

dimitri_cas作成者
Inspiring
March 2, 2023

Hey jduncan  🙂

 

W0W thx a lot, it works perfectly.

Just a bi "sad" that Photoshop needs to be open.

 

Even I'm absolutely not a programmer…

I was working on a piece of coding but I'm stuck with the part with the part I will highlight.

That highlighted part works "alone", but when I embed it in the below piece of code it doesn't work anymore…

 

Hereunder the basic code. As comment = highlighted part 😉

 

// #targetengine miscellaneous;
#target Illustrator
#targetengine main


var SWD = "bGE-PJk";

// PALETTE
// =======
var myPalette = new Window("palette");  // , {resizeable: true}
    myPalette.text = "DC TOOLS";
    myPalette.orientation = "column";
    myPalette.alignChildren = ["center","top"];
    myPalette.spacing = 5;
    myPalette.margins = 5;

var button2 = myPalette.add("button", undefined, "PACK", {name: "button2"});
    button2.graphics.font = ScriptUI.newFont ("Helvetica", "Bold", 11);   // <— From p78 https://www.daube.ch/docu/fm-documentation/ExtendScript/ScriptUI_for_Dummies-%5BPeterKahrel%5D.pdf   // button2.pointSize = 15;    // button2.text.font.size = "12pt";
    button2.helpTip = "XXX";

myPalette.frameLocation = [ 1850, 70 ];
myPalette.show();

button2.addEventListener('mousedown', function () {

/*
    // Clear the clipboard
    app.activeDocument.selection = null;
    app.copy();

    var tempObj = app.activeDocument.pathItems.add();   // create some temporary object
    var myText = app.activeDocument.textFrames.add();   // create the text frame
        myText.contents = SWD;

        tempObj.selected = true;                        // select the temporary object first -- it is important part!
        myText.selected = true;                         // <--- suprise! now you can select the text frame just like any other object
        tempObj.remove();                               // delete the temporary object

        app.copy();                                     // var sh = app.copy();
*/

   openURL('https://adobe.com/');
})


function openURL(url) {
    var html = new File(Folder.temp.absoluteURI + '/aisLink.html');
        html.open('w');
    var htmlBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
        html.write(htmlBody);
        html.close();
        html.execute();
}

 

CarlosCanto
Community Expert
Community Expert
March 1, 2023

app.system is not part of the Illustrator API, it's on the Photoshop API if that helps

dimitri_cas作成者
Inspiring
March 1, 2023

Hey Carlos  🙂

 

It helps,

but how can I copy-paste that string (SWTY) into the clipboard at launch (without having specifically an document open)?

 

Thanks…

 

 

 

CarlosCanto
Community Expert
Community Expert
March 2, 2023

create a new document, add a text frame, add the contents you want copied to the clipboard, app.copy() then close the document.

 

it's a bit too much but that the only option I can think of right now