Highlighted

code to move selected text box to new layer

Explorer ,
Aug 29, 2020

Copy link to clipboard

Copied

I'm trying to create a script that creates a new named layer and Paragraph style, then takes the selected text box and moves it to that layer. I've got the new layer and style but I'm having trouble moving that selection. Could someone give me a example of how to do that?

 

var myDoc = app.activeDocument;  
var Sel = app.selection;


//Create INFO layer
if(!myDoc.layers.itemByName("INFO").isValid){myDoc.layers.add({name:"INFO"})}; 


//Create Notes Paragraph Style
if (!myDoc.paragraphStyles.item("INFO").isValid) {
    var pStyle = myDoc.paragraphStyles.add({name: "INFO"});
    with (pStyle) {
       appliedFont = "Helvetica Neue\tBold";
        pointSize = 14;
        fillColor = "Black";
    }

}
Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Ahh, sorry it was a typo I replied to you just getting out of bed and missed writing the property, as Crazy panda mentioned it has to be the following

Sel[0].itemLayer = myDoc.layers.itemByName("INFO");

Sorry for the confusion, I will edit the previous answer as well

-Manan

TOPICS
Scripting

Views

119

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

code to move selected text box to new layer

Explorer ,
Aug 29, 2020

Copy link to clipboard

Copied

I'm trying to create a script that creates a new named layer and Paragraph style, then takes the selected text box and moves it to that layer. I've got the new layer and style but I'm having trouble moving that selection. Could someone give me a example of how to do that?

 

var myDoc = app.activeDocument;  
var Sel = app.selection;


//Create INFO layer
if(!myDoc.layers.itemByName("INFO").isValid){myDoc.layers.add({name:"INFO"})}; 


//Create Notes Paragraph Style
if (!myDoc.paragraphStyles.item("INFO").isValid) {
    var pStyle = myDoc.paragraphStyles.add({name: "INFO"});
    with (pStyle) {
       appliedFont = "Helvetica Neue\tBold";
        pointSize = 14;
        fillColor = "Black";
    }

}
Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Ahh, sorry it was a typo I replied to you just getting out of bed and missed writing the property, as Crazy panda mentioned it has to be the following

Sel[0].itemLayer = myDoc.layers.itemByName("INFO");

Sorry for the confusion, I will edit the previous answer as well

-Manan

TOPICS
Scripting

Views

120

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 29, 2020 0
Adobe Community Professional ,
Aug 29, 2020

Copy link to clipboard

Copied

Try the following

 

Sel[0].itemLayer = myDoc.layers.itemByName("INFO")

 

 This will move the first element of the selection to the layer INFO, if you want to move the other elements of the selection as well then run a loop from zero to Sel.length. Sel is an array of all the elements that are selected

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 29, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

It doesn't seem to work. Have you tried it? Did it work for you?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Participant ,
Aug 30, 2020

Copy link to clipboard

Copied

try

 

Sel[0].itemLayer = "INFO";

 

or

 

Sel[0].itemLayer = myDoc.layers.itemByName("INFO");

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

Yes, this code should be placed at the end of your code. If it does not work still. Show the whole code and let us know the error you get

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

Ok move to the bottom and still not working. Here is the code, the only things not working are attaching the new paragraph style to the object style and moving it to the new layer. Thank you for your help!

var myDoc = app.activeDocument;  
var Sel = app.selection;
var os = app.documents[0].objectStyles.itemByName("INFO")


//create color swatch
if(!myDoc.colors.item("INFO").isValid){
myDoc.colors.add({  
  colorValue: [0,50,0,0],  
  name: "INFO"  
  }); 
}

//Create INFO layer
if(!myDoc.layers.itemByName("INFO").isValid){myDoc.layers.add({name:"INFO"})}; 


//Create INFO Paragraph Style
if (!myDoc.paragraphStyles.item("INFO").isValid) {
    var pStyle = myDoc.paragraphStyles.add({name: "INFO"});
    with (pStyle) {
       appliedFont = "Helvetica Neue\tBold";
        pointSize = 10;
        fillColor = "Black";
    }
}

//Create object style
if (!myDoc.objectStyles.item("INFO").isValid) {
    var oStyle = myDoc.objectStyles.add({name: "INFO"});
    with (oStyle) {
        fillColor = "INFO";
        strokeColor = "INFO";
        strokeWeight = 5;
        paragraphStyle = "INFO"; //This isn't working
    }
}


//Apply object style
if(os.isValid){
Sel[0].applyObjectStyle(os)
}
	
//Move to layer
Sel[0] = myDoc.layers.itemByName("INFO");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Participant ,
Aug 30, 2020

Copy link to clipboard

Copied

it's

Sel[0].itemLayer = "INFO";

or

Sel[0].itemLayer = myDoc.layers.itemByName("INFO");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 1
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

Ahh, sorry it was a typo I replied to you just getting out of bed and missed writing the property, as Crazy panda mentioned it has to be the following

Sel[0].itemLayer = myDoc.layers.itemByName("INFO");

Sorry for the confusion, I will edit the previous answer as well

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

No problem at all, and thank you for your help. Since you can see the whole script do you happen to know why I can't set the Object styles paragraph style? You can see where I try to set it when I create the objectstyle.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

For object style issue use the property appliedParagraphStyle instead of paragraphStyle

-Manan

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

You beat me to it.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

So would that be inline like I have it, because that isn't working.

//Create object style
if (!myDoc.objectStyles.item("NOTES").isValid) {
    var oStyle = myDoc.objectStyles.add({name: "NOTES"});
    with (oStyle) {
        fillColor = "NOTES";
        strokeColor = "NOTES";
        strokeWeight = 5;
        appliedParagraphStyle = "NOTES"; //This isn't working
    }
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

Instead of 

appliedParagraphStyle = "NOTES"

 

use

appliedParagraphStyle = myDoc.paragraphStyles.item ("NOTES");

Referring to objects by using just a string usually but not always works. Always refer to objects by setting an object.

P.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 2
Explorer ,
Aug 30, 2020

Copy link to clipboard

Copied

Thank you, this does set the paragraph style but it doesn't apply it because the checkbox for "Paragraph Styles" isn't checked in the object style. Would that be a Boolean?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 31, 2020

Copy link to clipboard

Copied

You need to set the following property as well to apply the paragraph style

enableParagraphStyle = true

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 31, 2020 1