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

getSelected() returns pageItem's strokeColor or fillColor but not both

New Here ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

Hi All,

I'm having trouble writing a script to tell me which swatches are used in the active document and I think the problem comes down to the Swatches method getSelected(), see code below. I'm storing a list of all the pageItems and the swatches in the activeDocument. Then I select each pageItem and use getSelected which should return an array of swatches for my selection. However I've noticed it only returns the strokeColor or fillColor of the pathItems for example. Is there a way to fix this? Any help would be greatly appreciated

var _pageItems = app.activeDocument.pageItems;

var _pageItemsLength = _pageItems.length;

if (_pageItemsLength > 0)

{

  var _placedItemFileSwatches = app.activeDocument.swatches;

  for (var i = 0; i < _pageItemsLength; i++)

  {

    _pageItems.layer.visible = true;

    if (_pageItems.name != "")

    {

      app.activeDocument.selection = [_pageItems];

      var _selectedSwatches = _placedItemFileSwatches.getSelected();

      var _selectedSwatchesLength = _selectedSwatches.length;

      for (var j = 0; j < _selectedSwatchesLength; j++)

      {

        if (_selectedSwatches != "")

        {

          alert("Swatch '{0}' is assigned to pageItem '{1}'".fmt(_selectedSwatches.name, _pageItems.name) );

        }

      }

    }

  }

}

TOPICS
Scripting

Views

758

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 , Mar 29, 2018 Mar 29, 2018

hard to say exactly what the problem is without being able to test it, but my guess is that the problem lies with the way the UI displays any given swatch. For example, you always have either "fillColor" or "strokeColor" at the forefront of your swatches panel. And if you select an object that has a "Black" fill and a "White" stroke, then the result of the getSelected() method would presumably be tied to the active color/swatch type (for lack of better way to say that). So if your swatches panel

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

hard to say exactly what the problem is without being able to test it, but my guess is that the problem lies with the way the UI displays any given swatch. For example, you always have either "fillColor" or "strokeColor" at the forefront of your swatches panel. And if you select an object that has a "Black" fill and a "White" stroke, then the result of the getSelected() method would presumably be tied to the active color/swatch type (for lack of better way to say that). So if your swatches panel has the "stroke" in front, then your swatches panel should have "White" as the selected swatch in your swatches panel.

If you're already looping the pageItems in the document, you can ignore the swatches panel and access the fillColor and strokeColor properties of that object directly.

Beware, however, this will require additional logic in order to handle groupItems or  compoundPathItems (since fillColor and strokeColor are not properties of those objects).

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
New Here ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

You are correct It is returning whatever is at the forefront of the swatches panel. If I can switch that through scripting then that would solve my problem. I'm going to investigate further and see if there's a way...

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 ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

If that's the only hangup, you should be able to create an action that swaps fill/stroke (but be conscious of the fact that you can't explicitly say "set the active color option to stroke" or something. you can just toggle the setting).

check Silly-V​'s post about creating dynamic actions here:

Creating a dynamic action to use with app.doScript() method.

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
Valorous Hero ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

Actually I think it has the 'set fill/set stroke' commands. But I'm not too sure if it always works.

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

you can check whether the fill or stroke is active

alert(app.isFillActive());

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

is it settable though? OP was saying that they were unable to read both values using the getSelected() method.

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

no, it's not settable, it will just assist setting the proper action to toggle fill/stroke

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 ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

LATEST

ah yes. good point.

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