Skip to main content
Inspiring
February 14, 2023
Answered

Adobe AI can't copy/paste using scripts + user interface

  • February 14, 2023
  • 4 replies
  • 2102 views

Hi community, I got a question, I've been struggling with this issue for around three hours already.

I have an user interface, which I don't use it to copy/paste just to gather information, and when I click in "ok" to do it's thing, AI suddently does not paste what I commanded it to copy, using app.executeMenuCommand("paste"); does not work and app.paste(); shows an error message with the code 171 saying that is an internal AI error, funny thing is that when I close/open AI it works again (note that when I re-open AI i did not run the user interface)

 

also, in the same moment I run the UI, and copy another thing by control + C it does not paste either, not until I restart AI.

 

does anybody know what could be happening? please and thanks

This topic has been closed for replies.
Correct answer AntonioPacheco

Hi! In the end, what I did is to make the script wait like a second before execute the command for copy or cut, because I don't knwo why, the script/illustrator runs the code so fast that it just ignores or pass the code line, so I did something like an alert in between or:

 

alert("wait a second");

$.sleep( 999 ); /*Milliseconds*/

alert ("done");

 

and it worked good, hope this helps!

4 replies

AntonioPachecoAuthorCorrect answer
Inspiring
February 23, 2023

Hi! In the end, what I did is to make the script wait like a second before execute the command for copy or cut, because I don't knwo why, the script/illustrator runs the code so fast that it just ignores or pass the code line, so I did something like an alert in between or:

 

alert("wait a second");

$.sleep( 999 ); /*Milliseconds*/

alert ("done");

 

and it worked good, hope this helps!

Inspiring
February 15, 2023

Hi Community,

 

I have no explanation for what's happening about the copy/paste thing here but since I can't be stuck in the same error for a day, what I did is to bring the content I need from that layer directly from abother document, so I did:

 

app.open(File("C:\\AuxFile.ai"));
app.executeMenuCommand("selectall");
app.executeMenuCommand("copy");
app.activeDocument.close(SaveOptions . DONOTSAVECHANGES);
app.executeMenuCommand("pasteInPlace");
app.executeMenuCommand("sendToBack");
app.executeMenuCommand("deselectall");

 

Hopefully I can have the solution for this someday, in the meantime, copy/paste the object from another document works.

CarlosCanto
Braniac
February 15, 2023

it's hard to tell without seeing the script

Inspiring
February 15, 2023

Sorry!  I was trying to figure out what's happening but no results, here is the code:

 

//always add if using autohotkey
// app.preferences.setBooleanPreference("/ShowExternalJSXWarning", false)

//Get document and layers, active artboard to 0.
var doc = app.activeDocument;
var layers = doc.layers;
app.activeDocument.artboards.setActiveArtboardIndex(0);

//String to show or hide the layers accoding selection
var GarmentSide, myFullString = "";

//color quantity
var CantidadDeColors = 0;

//Dialog init
var w = new Window ("dialog", "(ML ScreenPrint)Please select Product, Location, and where to draw the information.");
w.orientation = 'row';
var rdProduct, rdInkType, rdLocation, rdSide , rdTeeStyle = ""; //each variable will store one of the text values in every radio button group.
var valorX = 715, valorY = 42; //where to draw chips
var objeto = doc.selection[0];

//Product Radiobutton group init
var rg_Product = w.add ("panel");
rg_Product.alignChildren = "left";
rg_Product.add ("radiobutton", undefined, "Screen Printing");
rg_Product.add ("radiobutton", undefined, "Embroidery/Patch");
rg_Product.add ("radiobutton", undefined, "DTG");

//Location Radiobutton group init
var rg_Location = w.add ("panel");
rg_Location.alignChildren = "left";
rg_Location.add ("radiobutton", undefined, "FULL FRONT");
rg_Location.add ("radiobutton", undefined, "LEFT CHEST");
rg_Location.add ("radiobutton", undefined, "RIGHT CHEST");
rg_Location.add ("radiobutton", undefined, "FULL BACK");
rg_Location.add ("radiobutton", undefined, "YOKE");
rg_Location.add ("radiobutton", undefined, "CENTER CAP");
rg_Location.add ("radiobutton", undefined, "SLEEVES");
rg_Location.enabled = false;

//Location Radiobutton group init
var rg_TeeStyle = w.add ("panel");
rg_TeeStyle.alignChildren = "left";
rg_TeeStyle.add ("radiobutton", undefined, "TEE");
rg_TeeStyle.add ("radiobutton", undefined, "LONG SLEEVE");
rg_TeeStyle.add ("radiobutton", undefined, "HOODIE");
rg_TeeStyle.enabled = false;

//Side Radiobutton group init
var rg_Side = w.add ("panel");
rg_Side.alignChildren = "left";
rg_Side.add ("radiobutton", undefined, "LEFT SIDE");
rg_Side.add ("radiobutton", undefined, "RIGHT SIDE");
rg_Side.enabled = false;

//Ink Type group init
var rg_InkType = w.add ("panel");
rg_InkType.alignChildren = "left";
rg_InkType.add ("radiobutton", undefined, "PLASTISOL");
rg_InkType.add ("radiobutton", undefined, "DISCHARGE");
rg_InkType.enabled = false;

//Disable radiobutton default selected option
rg_Product.children[0].value = false;
rg_Location.children[0].value = false;
rg_TeeStyle.children[0].valaue = false;
rg_Side.children[0].value = false;
rg_InkType.children[0].value = false;

/*/////////////////////////*/
/*///Variable Gathering////*/
/*/For all the Radio Groups/*/
/*/////////////////////////*/
for (var i = 0; i < rg_Product.children.length; i++) {
/** @TyPe {RadioButton} */(rg_Product.children[i]).onClick = function(){
rg_Location.enabled = true;
rdProduct = this.text;
// alert(this.text);
}
}

for (var i = 0; i < rg_Location.children.length; i++) {
/** @TyPe {RadioButton} */(rg_Location.children[i]).onClick = function(){
rg_TeeStyle.enabled = true;
rdLocation = this.text;
// alert(this.text);
}
}

for (var i = 0; i < rg_TeeStyle.children.length; i++) {
/** @TyPe {RadioButton} */(rg_TeeStyle.children[i]).onClick = function(){
rg_Side.enabled = true;
rdTeeStyle = this.text;
// alert(this.text);
}
}

for (var i = 0; i < rg_Side.children.length; i++) {
/** @TyPe {RadioButton} */(rg_Side.children[i]).onClick = function(){
rg_InkType.enabled = true;
rdSide = this.text;
// alert(this.text);
}
}

for (var i = 0; i < rg_InkType.children.length; i++) {
/** @TyPe {RadioButton} */(rg_InkType.children[i]).onClick = function(){
rdInkType = this.text;
// alert(this.text);
}
}
/*/////////////////////////*/
/*////End Of Gathering/////*/
/*/////////////////////////*/

//Create button.
var botones = w.add("group");
// botones.orientation = "row";
botones.add ("button", undefined, "OK");

if(w.show() == 1){
if(rdLocation == "FULL FRONT" || rdLocation == "LEFT CHEST" || rdLocation == "RIGHT CHEST"){
GarmentSide = "FRONT";
}
else{
GarmentSide = "BACK"
}
myFullString = rdTeeStyle + " " + GarmentSide + " " + rdSide;
//Just to check variable gather
// alert(rdProduct + " " + rdLocation + " " + " " + rdTeeStyle + " " + rdSide);
// alert(myFullString);
app.executeMenuCommand("cut");
var myNewLayer = doc.layers.add();
myNewLayer.name = rdLocation;
doc.activeLayer = doc.layers[rdLocation];
app.executeMenuCommand("pasteFront");
WritePlacement();
if(rdSide == "LEFT SIDE"){
switch(rdLocation){
case "FULL FRONT":
app.doScript ("FullFrontLeftSide", "MLScreenPrintUI");
break;
default:
break;
}
}
else{
switch(rdLocation){
case "FULL FRONT":
app.doScript ("FullFrontRightSide", "MLScreenPrintUI");
break;
default:
break;
}
}
var doc = app.activeDocument;
var myLayers = doc.layers;
// alert(myLayers["Garments"].layers["InfoBreakdownShapes"].name);
myLayers["Garments"].layers["InfoBreakdownShapes"].visible = true;
myLayers["Garments"].layers["InfoBreakdownShapes"].hasSelectedArtwork = true;
app.executeMenuCommand("copy");
myLayers["Garments"].layers["InfoBreakdownShapes"].visible = false;
doc.activeLayer = myLayers[0];
app.executeMenuCommand("pasteFront");
app.executeMenuCommand("sendToBack");
app.executeMenuCommand("deselectall");
}
function WritePlacement(){

//if object selected
try {
var ancho = 0, alto = 0, anchopx = 0, altopx = 0, anchomm = 0, altomm = 0, anchoin = 0, altoin = 0;
ancho = objeto.width;
alto = objeto.height;
anchopx = ancho/72;
altopx = alto/72;
anchomm = anchopx*25.4;
altomm = altopx*25.4;
anchoin = anchomm*0.0393701;
altoin = altomm*0.0393701;
var textblack = new CMYKColor();
textblack.cyan = 0;
textblack.magenta = 0;
textblack.yellow = 0;
textblack.black = 100;
// Math.floor(anchoin);
// Math.floor(altoin);
// alert(anchoin + " x " + altoin);
var fontsize = 8;
var space = 5; // horizontal space between textPaths
var font = "ProximaNova-Light"; // font
var y = 0;

//Writes the location

var w = fontsize * 5, h = fontsize, x = -910, y = h + space + 128;

// var color = swatches["TextColour"].color; // color
var group = doc.groupItems.add();
var rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
var text = /** @TyPe {typeof TextFrameItems} */(/** @TyPe {unknown} */ (doc.textFrames)).areaText(rect);
text.contents = rdLocation;
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.characterAttributes.size = fontsize;
text.textRange.characterAttributes.textFont = (app.textFonts.getByName(font));
text.textRange.characterAttributes.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90"); // rotate text
text.move(group, ElementPlacement.PLACEATEND);

//Writes Process

var w = fontsize * 5, h = fontsize, x = -910, y = h + space + 138;

group = doc.groupItems.add();
rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
text = doc.textFrames.areaText(rect);
text.contents = rdProduct; //Math.round(anchoin * 100) / 100 + "''W x " +
// text.contents = Math.round(altoin * 100) / 100 + "'' TALL"; //Math.round(anchoin * 100) / 100 + "''W x " +
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
text.textRange.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90");
text.move(group, ElementPlacement.PLACEATEND);

//Writes the inktype

w = fontsize * 5, h = fontsize, x = -910, y = h + space + 148;

group = doc.groupItems.add();
rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
text = doc.textFrames.areaText(rect);
text.contents = rdInkType;
// text.contents = Math.round(anchoin * 100) / 100 + "''WIDE ";
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
text.textRange.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90");
text.move(group, ElementPlacement.PLACEATEND);


//Writes the width

w = fontsize * 5, h = fontsize, x = -910, y = h + space + 158;

group = doc.groupItems.add();
rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
text = doc.textFrames.areaText(rect);
text.contents = Math.round(anchoin * 100) / 100 + "'' W";
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
text.textRange.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90");
text.move(group, ElementPlacement.PLACEATEND);

//Writes the height

w = fontsize * 5, h = fontsize, x = -910, y = h + space + 168;

group = doc.groupItems.add();
rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
text = doc.textFrames.areaText(rect);
text.contents = Math.round(altoin * 100) / 100 + "'' H";
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
text.textRange.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90");
text.move(group, ElementPlacement.PLACEATEND);

//Writes the amount of colors

w = fontsize * 5, h = fontsize, x = -909, y = h + space + 176.5;

group = doc.groupItems.add();
rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
text = doc.textFrames.areaText(rect);
text.contents = CantidadDeColors;
// text.textRange.paragraphAttributes.justification = Justification.CENTER;//Center text
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
text.textRange.fillColor = textblack;
while (text.lines[0].characters.length < text.characters.length) {
text.textPath.width += fontsize;
}
// text.rotate("90");
text.move(group, ElementPlacement.PLACEATEND);

} catch (e) { //if object selected
alert("No object was selected!");
}
}

 

the exact part not working is:

 

var doc = app.activeDocument;
var myLayers = doc.layers;
// alert(myLayers["Garments"].layers["InfoBreakdownShapes"].name);
myLayers["Garments"].layers["InfoBreakdownShapes"].visible = true;
myLayers["Garments"].layers["InfoBreakdownShapes"].hasSelectedArtwork = true;
app.executeMenuCommand("copy");
myLayers["Garments"].layers["InfoBreakdownShapes"].visible = false;
doc.activeLayer = myLayers[0];
app.executeMenuCommand("pasteFront");
app.executeMenuCommand("sendToBack");
app.executeMenuCommand("deselectall");

 

What I am trying to do is, select all the objects in this layer InfoBreakdownShapes (whether is visible or not) to the new layer the code generates at the top , image attached:

 

I am attaching the file I am using, as per the community rule it has no private information.

 

In my opinion... the code gets adobe stuck or something like that and I must figure out how to:

 

unhide layer

select all in that layer infobreakdownshapes

copy

paste in the new generated layer

send it to back so the information is visible

hide again the layer (this hide/unhide is because that layer called breakdownshapes is needed just to duplicate its contents everytime the code is run, but I am thinking now in just create the shapes via code.)

Inspiring
February 14, 2023

I just discovered that when I move that specific part of the code which selects all in a layer (hasSelectedArtwork) and paste that code in a new VS code doc, works good. It's like something is messing up while executing the UI.

Inspiring
February 15, 2023

I just provided the code, one more thing... if you run the code block where I have the error, it will run perfectly (without being in a UI)