Need an Illustrator JavaScript script to toggle three settings at once and keep them synchronized
Asked Answered
S

1

9

I often work with icons and need to quickly check how they look with pixel preview. When I'm building them I have the grid enabled, and artboards visible.

I really want a script that will do the following in one click:

  1. Hide Artboards

  2. Hide all guides

  3. Hide the grid

  4. Toggle Pixel Preview to ON.

And then another script that will exactly reverse the above toggles.

Unfortunately I can't find a way to query the state of the application to determine if artboard borders are already hidden, or if the grid is already hidden, etc.

This is the simple script right now:

//@target illustrator
preferences.setBooleanPreference("ShowExternalJSXWarning", false); // Fix drag and drop a .jsx file

function main() {
   var SCRIPT = {
      name: "Toggle Preview Mode",
      version: "v.0.1"
   };

   if (!documents.length) {
      alert("Error\nOpen a document and try again");
      return;
   }
   app.executeMenuCommand("showgrid");
   app.executeMenuCommand("showguide");
   app.executeMenuCommand("raster");
   app.executeMenuCommand("artboard");
}

try {
   main();
} catch (e) {}

But the problem is if the grid is already hidden, the script will toggle it back on when I want to hide it. Likewise with guides / pixel preview / artboard visibility. Is there a way to query the state of each property?

Basically I want to do this: (excuse the psuedo code)

if(guidesAreHidden=true){ //do nothing }
if(gridIsHidden=true){ //do nothing }
if(pixelPreviewIsOn=true){ //do nothing }
if(artboardsAreHidden=true){//do nothing }

Or a more complex example (Only the grid is hidden):

if(guidesAreHidden=false) { app.executeMenuCommand("showguide"); }
if(gridIsHidden=true){ //do nothing }
if(pixelPreviewIsOn=false){ app.executeMenuCommand("raster"); }
if(artboardsAreHidden=false){ app.executeMenuCommand("artboard"); }

Essentially I need a way to retrieve whether guides/grid/pixel preview/artboard visibility is currently enabled or disabled so I can set the appropriate state of the application.

Any help greatly appreciated.

Edit1:


I just want to add that if this can be done with a custom CEP or UXP plugin rather than a script, I would love to hear how. I am not adverse to programming my own plugin to accomplish this feature. I've been meaning to dive into CEP/UXP plugin authoring for awhile now anyway.

Schist answered 25/8, 2022 at 6:0 Comment(5)
Good question. I'd want to know the answer, too.Constructionist
As far as I know CEP has even less access to the Illustrator's intestines than Extendscript. Not sure about UXP plugins but I suspect they're alike CEP in this regard. But I'm sure the full-fledged native Illustrator plugins (SDK, C++, etc) have full access to everything.Constructionist
Any luck? Don't want that bounty to go to waste, lol.Magnification
On a serious note, I'm following this question and am here to help if you have any further issues in the future. Just ask and I'll help out :)Magnification
@cybernetic.nomadMagnification
M
1

Illustrator has a way to do this using a document in the app:

var items = app.activeDocument.pageItems
for (var i = 0; i < items.length; i++) {
    var item = items[i]
    //check here for:
    // item.hidden
    // item.visible
}

The hidden property will tell you if things like layers and artboards are showing.

The visible property is for placed items like images for example.

Magnification answered 5/9, 2022 at 3:19 Comment(6)
Thanks so much, going to try this out in a little bit here. Do you know what valid values are for the hidden property? What should I be testing for if I want to check guides, grid, pixel preview, and artboard visibility?Schist
hidden and visible are just booleans so only true or false.Magnification
Sorry, I meant what item values exist for the above? Is it just guides.hidden for instance if testing for guides visibility? Can you update your post to include code that corresponds to the correct item values?Schist
I’ll post an updateMagnification
Looking forward to it! Many thanks.Schist
I’m going to get to this later tonight. Just letting you know.Magnification

© 2022 - 2024 — McMap. All rights reserved.