Copy link to clipboard
Copied
Hi,
I'm currently trying to do the same copy to clipboard trick Muppet Mark-QAl63s showed me in this thread: http://forums.adobe.com/thread/840440?tstart=0
Unfortunately with my current document I get an error when trying to execute this:
var lineRef = app.activeDocument.pathItems.add();
and there error is: "Target layer cannot be modified"
....which is a bit cryptic to me. Why can't the layer be modified ?
I've printed to the console app.activeDocument.activeLayer.locked and it prints false.
What does that error mean and how can I get past it ?
Thanks in advance,
George
Why not just add a new layer and finally remove it?
function copyToClipboard (text, doc){doc.layers.add().textFrames.add().contents = text;doc.layers[0].hasSelectedArtwork = true;app.copy();doc.layers[0].remove();}copyToClipboard("Text for copy to Clipboard", activeDocument);
Copy link to clipboard
Copied
Check to see if the layer that you are trying to add the line to is visible. I gave it a quick try, and Illustrator wouldn't let me add an item when the layer was not visible.
Copy link to clipboard
Copied
That seems to replicate the issue quite well,
done a basic test with a new document and an invisible layer:
var wasVisible = app.activeDocument.activeLayer.visible; if(!wasVisible) app.activeDocument.activeLayer.visible = true; var lineRef = app.activeDocument.pathItems.add(); if(!wasVisible) app.activeDocument.activeLayer.visible = false;
and that worked.
I don't have the document I had the issue with on this machine, but I'll check again tomorrow.
Thanks!
Copy link to clipboard
Copied
AI doesn't allow you to work with a layer that isn't visible. This includes stuff like removing it as well as modifying it.
Copy link to clipboard
Copied
So what's the rest of the script. Kind of hard to tell from the snippet.
Copy link to clipboard
Copied
Ok, it wasn't as simple as I expected.
The document I had issues with is from a designer and contains a lot of layers, some locked, some invisible, although at the moment, I'm concerned with just one layer.
I might be wrong, but it turns out that I need unlock/set visible true for ALL the layers, even though I'm not using them...the activeLayer is visible and unlocked. I actually hope I'm wrong, because at the moment I'm storing the state of all layers, making them ALL visible/unlocked, doing the clipboard hack, then restoring them. This is a bit much just for the clipboard functionality to be honest and I suspect it can crash Illustrator for complex documents.
Here's my current function:
function copyToClipboard(text,doc){
var lvs = [];
var numLayer = doc.layers.length;
//unlock/make all visible
for(var i = 0 ; i < numLayer ; i++){
lvs = [doc.layers.locked,doc.layers.visible];
if(lvs[0]) doc.layers.locked = false;
if(!lvs[1]) doc.layers.visible = true;
}
try{
var lineRef = app.activeDocument.pathItems.add();
lineRef.setEntirePath( Array(Array(0, 0), Array(500, 0) ) );
var pathTextRef = doc.textFrames.pathText(lineRef);
pathTextRef.contents = text;
pathTextRef.selected = true;
doc.selection = [pathTextRef];
app.cut();
}catch(error){ alert(error); }
//restore
for(var i = 0 ; i < numLayer ; i++){
if(lvs[0]) doc.layers.locked = true;
if(!lvs[1]) doc.layers.visible = false;
}
}
Is this the only way to achieve this ? Feels very hacky/clunky/innefficient not to mention the previous selection is lost.
Copy link to clipboard
Copied
Any tips ? Anyone ?
Copy link to clipboard
Copied
Why not just add a new layer and finally remove it?
function copyToClipboard (text, doc){doc.layers.add().textFrames.add().contents = text;doc.layers[0].hasSelectedArtwork = true;app.copy();doc.layers[0].remove();}copyToClipboard("Text for copy to Clipboard", activeDocument);
Copy link to clipboard
Copied
) yes, that is a lot simpler...why did I miss that
Thanks!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now