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.

fmotion1
  • 237
  • 4
  • 12

1 Answers1

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.

Dan Mullin
  • 4,285
  • 2
  • 18
  • 34
  • 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`? – fmotion1 Sep 05 '22 at 21:43
  • 1
    hidden and visible are just booleans so only true or false. – Dan Mullin Sep 05 '22 at 22:02
  • 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? – fmotion1 Sep 07 '22 at 22:51
  • I’ll post an update – Dan Mullin Sep 08 '22 at 00:06
  • Looking forward to it! Many thanks. – fmotion1 Sep 08 '22 at 12:47
  • I’m going to get to this later tonight. Just letting you know. – Dan Mullin Sep 08 '22 at 14:44