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

code to move selected text box to new layer

Engaged ,
Aug 29, 2020 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";
    }

}
TOPICS
Scripting

Views

529

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
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 30, 2020 Aug 30, 2020

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

Votes

Translate

Translate
Community Expert ,
Aug 29, 2020 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

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

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

 

Votes

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
community guidelines
Contributor ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

try

 

Sel[0].itemLayer = "INFO";

 

or

 

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

 

 

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 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

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 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");

Votes

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
community guidelines
Contributor ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

it's

Sel[0].itemLayer = "INFO";

or

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

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 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

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 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.

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

For object style issue use the property appliedParagraphStyle instead of paragraphStyle

-Manan

 

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 Aug 30, 2020

Copy link to clipboard

Copied

You beat me to it.

 

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 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
    }
}

Votes

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
community guidelines
Community Expert ,
Aug 30, 2020 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.

Votes

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
community guidelines
Engaged ,
Aug 30, 2020 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?

Votes

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
community guidelines
Community Expert ,
Aug 31, 2020 Aug 31, 2020

Copy link to clipboard

Copied

LATEST

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

enableParagraphStyle = true

-Manan

Votes

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
community guidelines