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

Script to unlock objects and change stroke attributes

Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Good day everyone,

Im trying to have a script where It can;
- unlock all objects on "Design" layer
Then a separate script to;

- change stroke size and color of selected object

- then move it from "art" layer to "Design" layer.

 

Thank you in advance

TOPICS
Scripting , SDK

Views

223

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 19, 2021 Aug 19, 2021

The color needs to be named in your Swatch panel, and that name needs to be referenced in 'Color Swatch Name' this line: 

var col = doc.swatches.itemByName("Color Swatch Name");

Also, I thought you wanted to change the object color, not stroke color. Fixed below: 

 

var doc = app.activeDocument;
var al = doc.layers.itemByName("art");
var dl = doc.layers.itemByName("Design");
var col = doc.swatches.itemByName("Color Swatch Name");
var apis = al.allPageItems;
for (var i = 0; i < apis.length; i++)
...

Votes

Translate

Translate
Community Expert ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

#1

var dl = app.activeDocument.layers.itemByName("Design").allPageItems;
for (var i = 0; i < dl.length; i++) { 
   try { dl[i].locked = false; } catch(e) {}
}

 #2

var doc = app.activeDocument;
var al = doc.layers.itemByName("art");
var dl = doc.layers.itemByName("Design");
var col = doc.swatches.itemByName("Color Swatch Name");
var apis = al.allPageItems;
for (var i = 0; i < apis.length; i++) {
   try {
      apis[i].fillColor = col;
      apis[i].strokeWeight = "1 pt";
      apis[i].itemLayer = dl;
   } catch(e) {}
}

 

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

thank you so much @brianp311 , though im still getting this pop up error.

- how do i change the stroke color to a C0M0Y0K0 value (current object selected)
Screen Shot 2021-08-19 at 2.18.22 PM.png

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 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

The color needs to be named in your Swatch panel, and that name needs to be referenced in 'Color Swatch Name' this line: 

var col = doc.swatches.itemByName("Color Swatch Name");

Also, I thought you wanted to change the object color, not stroke color. Fixed below: 

 

var doc = app.activeDocument;
var al = doc.layers.itemByName("art");
var dl = doc.layers.itemByName("Design");
var col = doc.swatches.itemByName("Color Swatch Name");
var apis = al.allPageItems;
for (var i = 0; i < apis.length; i++) {
   try {
      apis[i].strokeColor = col;
      apis[i].strokeWeight = "1 pt";
      apis[i].itemLayer = dl;
   } catch(e) {}
}

 

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

thanks @brianp311 , what if I dont have a swatches and I want to set it up here on the script is that possible? or should I add a line to create a swatch then call it on the "var col" line?

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

also, how can i change the var apis = al.allPageItems; to current selected object?

thank 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
Community Expert ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Here you go. 

var doc = app.activeDocument;
var al = doc.layers.itemByName("art");
var dl = doc.layers.itemByName("Design");
var col = doc.swatches.itemByName("myWhite");
if (!col.isValid) {
    col = doc.colors.add({
        colorValue: [0,0,0,0],
        space: ColorSpace.CMYK,
        name: "myWhite",
    });
}
try {
    var sel = app.selection[0];
    sel.strokeColor = col;
    sel.strokeWeight = "1 pt";
    sel.itemLayer = dl;
} catch(e) { }

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

LATEST

Thank you so much! This is prefect

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