By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Selecting a set of colors, making a new layer, and then sending to that layer using Illustrator script
JavaScript

Javascript – Selecting a set of colors, making a new layer, and then sending to that layer using Illustrator script

Admin
Last updated: 2023/12/13 at 5:45 PM
Admin
Share
3 Min Read
Selecting a set of colors, making a new layer, and then sending to that layer using Illustrator script

Problem:

I am trying to create an illustrator script that will select a a group of dots that all have the same color and then create a new layer and send it to that layer. The script works when doing one layer, but not with both.

Contents
Problem:Solution:

I don’t understand what I am doing wrong with my code. When I run the code as is, I get an error of “The operation cannot complete because there is no selection.” However, the code still runs, but it moves the yellow dots to the red dots layer and thats it. But, when I comment out either of the selectColor lines with the arguments filled in, it runs correctly with no error and puts the correct dots on the correct new layer.

Any suggestions?

var doc = app.activeDocument;//Gets the active document
var layerName;
var lay = doc.layers;
var myLayer = lay.add();

var yellow = new RGBColor();
yellow.red = 252;
yellow.green = 253;
yellow.blue = 1;

var red = new RGBColor();
red.red = 252;
red.green = 0;
red.blue = 0;




function selectColor(swatch, layerName) {

        var temp = app.documents[0].pathItems.rectangle(10, 10, 150, 150);
        temp.fillColor = swatch;

        // To select object that have CMYK Red swatch applied to fill.
        app.executeMenuCommand('Find Fill Color menu item');

    temp.remove();


    myLayer.name = layerName;
    app.executeMenuCommand('Selection Hat 2');
}

selectColor(yellow, "Yellow Dots");

selectColor(red, "Red Dots");

Solution:

Steven! As far as I can tell you want this:

var yellow = new RGBColor();
yellow.red = 252;
yellow.green = 253;
yellow.blue = 1;

var red = new RGBColor();
red.red = 252;
red.green = 0;
red.blue = 0;

function selectColor(swatch, layerName) {
    app.selection = null;

    var doc = app.activeDocument;
    var temp = doc.pathItems.rectangle(10, 10, 150, 150);
    temp.fillColor = swatch;
    temp.selected = true;
    app.executeMenuCommand('Find Fill Color menu item');
    temp.remove();

    try {
        var myLayer = doc.layers.getByName(layerName);
    } catch (e) {
        var myLayer = doc.layers.add();
        myLayer.name = layerName;
    }

    doc.activeLayer = myLayer;
    app.executeMenuCommand('Selection Hat 2'); // send the selection to current layer

    app.selection = null;
}

selectColor(yellow, "Yellow Dots");
selectColor(red, "Red Dots");

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Handle multiple Firebase Projects in the BackEnd Javascript Javascript – Handle multiple Firebase Projects in the BackEnd Javascript
Next Article App component keeps calling the axios.get many times over in a react function Javascript – App component keeps calling the axios.get many times over in a react function
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?