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

Tiny issue with a layer

Community Beginner ,
Aug 09, 2011 Aug 09, 2011

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

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

Enthusiast , Aug 18, 2011 Aug 18, 2011

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);
Translate
Adobe
Community Beginner ,
Aug 09, 2011 Aug 09, 2011

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.

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 Beginner ,
Aug 09, 2011 Aug 09, 2011

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!

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 ,
Aug 09, 2011 Aug 09, 2011

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.

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 ,
Aug 09, 2011 Aug 09, 2011

So what's the rest of the script. Kind of hard to tell from the snippet.

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 Beginner ,
Aug 10, 2011 Aug 10, 2011

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.

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 Beginner ,
Aug 18, 2011 Aug 18, 2011

Any tips ? Anyone ?

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
Enthusiast ,
Aug 18, 2011 Aug 18, 2011

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);
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 Beginner ,
Aug 18, 2011 Aug 18, 2011
LATEST

) yes, that is a lot simpler...why did I miss that

Thanks!

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