need to write a script to count objects/brush-objects on a layer
Copy link to clipboard
Copied
I've forgotten how to script AI and all my old scripts are not retrievable ATM.
in Pseudo code what I need is:
determine current active AI layer
reference all the objects in that layer as stuff
iterate stuff as item
if item is a brushed path then count++
return count
bonus points for returning info on all visible layers and totalling the layer counts
I'd settle for even just counting selected objects whatever they are. thx.
if I can find out the references for layers and layer objects and selected objects I could get started. User Guide to Illustrator CC 2015 Scripting is not great, I need a proper manual.
my script so far. I'm not referencing the objects on the target layer correctly to count them so I get undefined as the value.
var msg = "List of visbile layers\r"
// If a docuement is open
if ( app.documents.length > 0 ) {
sourceDoc = activeDocument;
if(sourceDoc.pageItems.length > 0 ) {
var layerCount = sourceDoc.layers.length;
for (var i = 0; i < layerCount; i++ ) {
targetLayer = sourceDoc.layers ;
if (targetLayer.visible ) {
msg += targetLayer.name + ' ';
}
msg += targetLayer.length + '\r';
}
alert (msg +"\r i =" + i, -1)
}
}
Message was edited by: Alastair Leith script so far. just need to do counting and do filtering for brushes
Explore related tutorials & articles
Copy link to clipboard
Copied
so I guess question is how do i access Arttree for a particular layer. The introduction to crippling says I can access the ratter for a particular layer but there's no example scripts that i can find. I could iterate the entire documents path items which I found a direct reference for but then I'd have to iterate through them all and test for layer visibility and keep an array of item counts. not a clean way to do it.
Copy link to clipboard
Copied
I don't believe we have access to what brush is applied to a stroke, or even if it has one...
for access to a layers items, something like this will get you started
var doc = app.activeDocument;
var lay = doc.activeLayer;
for(var i=0; i<lay.pageItems.length; i++){
var item = lay.pageItems;
//here you can try to do the impossible... 🙂
alert(item.name);
}
Copy link to clipboard
Copied
Unfortunately, it looks like you're right, Qwerty. We don't have access to any 'brushed' property. We can simply tell whether an object is stroked or not, it's stroke width, stroke dash properties, stroke cap, stroke color, miter limit, stroke join, and stroke overprint.
Sorry wideEyed.. I'm not sure this one is really 'do-able'. Although i hope i'm wrong and someone else has a solution, as this would be good to know.
Copy link to clipboard
Copied
Well if you were really adamant and had CS6+ and had a limited amount of known brushes to use with no crazy nested group/appearances, you can actually use Select Same Appearance to select brushed items by creating testing temps.
Check out my example which works in CC2015 on a document with simple paths.
#target illustrator
function test(){
function checkForBrushedPaths(brushObj){
doc.selection = null;
var s = doc.pathItems.add();
s.setEntirePath([[0,0],[100,-100]]);
s.stroked = true;
s.filled = false;
brushObj.applyTo(s);
app.executeMenuCommand("Find Appearance menu item");
s.remove();
return {brushName : brushObj.name, pathsAmount : doc.selection.length};
};
var doc = app.activeDocument;
var brushedPathsReport = "Brush Name\tPaths Detected\r";
for(var i=0; i<doc.brushes.length; i++){
var checkObj = checkForBrushedPaths(doc.brushes);
if(checkObj.pathsAmount > 0){
brushedPathsReport += (checkObj.brushName + "\t" + checkObj.pathsAmount + "\r");
}
};
var msg = (brushedPathsReport.split("\r").length > 2) ? brushedPathsReport : "No brushed paths were found here at all!";
alert(msg);
}
test();
I noticed that here Re: [JS] CS6+ executeMenuCommand , the Select Same Appearance command is not listed, but luckily my 1st guess interpolation guess proved correct.
Copy link to clipboard
Copied
Love this Idea Silly.
you know I have a soft spot for really Hack approaches to things that should be really simple.
maybe I should add this to a little Selector Add-in I have been making.
Copy link to clipboard
Copied
Yea I think we're alike in this penchant for "rube-goldbergish" workarounds. (which is all Actions are, imho)
I have a fascination with the "emergent" logic that is magical and yet ubiquitous in the universe. The amazing principle is how each of the components really does not care about anything else, not the end purpose or the preceding chain of events, and yet collectively do resemble a creature of sorts.
See this ridiculous experiment for a fun example:
Illustrator Variable Data Exploratory Techniques: Using Actions to Style Overset Text | Vasily Hall ...
Copy link to clipboard
Copied
I love this type of useless machine.
who remembers "Mouse Trap"?
the mythbusters guys started a series along those lines,
I thought it would be better then it was but hey(shrug) I still watched it...
Copy link to clipboard
Copied
I was so excited about this.
silly your script example works, but not if the stroke size changes or the colour or the fill.
now i'm back scratching my head.
I was so excited, now...
now I need a glass of red.
Copy link to clipboard
Copied
Okay, that's not great, is it.
However, since the OP request doesn't actually require numbers of specific brushes, we could just simply use the Select > Object > Brush Strokes command which will select all brush strokes.
app.executeMenuCommand("Brush Strokes menu item");
Copy link to clipboard
Copied
I have gone beyond the OP's topic and am working on something for me now...
selfish I know... LOL
the brushStrokes menu item may be part of the key though.
bit more time spent thinking and I may see some light...
Copy link to clipboard
Copied
I can see light...
will take some work, and will be as hack as they come.
but I think I have an idea that will achieve what I want.
I'll post back when I have it working.
Copy link to clipboard
Copied
This mystery intrigues me and causes my life to have meaning the next morning, keep it up plz
Copy link to clipboard
Copied
its working,
it involves a new working document, some executeMenuCommands, and an action.
it flickers a bit when run, but does work.
I'll post the code once I clean it up and get the action embedded.
I've also done most of the work in slotting it in to an extension so it all works from a dockable panel.
Copy link to clipboard
Copied
Cool, but as it's different from the original purpose here, what is it going to do?
Copy link to clipboard
Copied
sorry, I guess that would have been a good piece of info to share...
the addin I have so far is more to do with learning how to build simple extensions then the need to have any real world application.
but it may as well be useful.
you may remember the select blend post from a few weeks back, I believe you chimed it at one point.
I mad a HTML panel with a button that selects all blends in active document.
now I am expanding that so the panel builds a list of all brushes in the active document, so when you press one, it selects all objects with that brush applied.
so in essence it's an extension of the select options already available in illustrator.
I have done it as a panel rather then adding to the pre-existing menus due to my understanding of the more traditional extensions being crud.
it's still very ugly but here is a current screen shot.
Copy link to clipboard
Copied
are those buttons in 'select brush strokes' automatically generated by what's in the document? or are those hard coded?
Copy link to clipboard
Copied
they are generated whenever a document becomes active,
still trying to work out how to get them to update if a brush is added or removed.
and it does double them up at times and I am not sure why, so I need to sort that out.
but if you click the group heading it will refresh.
Copy link to clipboard
Copied
OK, here is the first iteration of the jsx code that does the work.
as I said its quite Hack and I am sure it has bugs (let me know if anyone finds any).
the part testing brush.length and and existence of the Touch Brush will probably be the weakest point,
this brush does not exist in CC, and I'm not sure about CC2014 I have not looked yet, I have not had a chance to test in anything but CC2015.
as the GUI for this is in the HTML of the Extension, the brush name to find is hard coded at the bottom of the script.
I'll post the extension when I have that to a beta stage.
let me know if you come up with any way to improve this.
function select_same_brush(BrushName){
// CS6+ compatable (I think)
//---------------------------------------------------------------------
// code written by Qwertyfly
// contact tristan@qwertyfly.com
//
// version 0.1 Beta
//
//---------------------------------------------------------------------
//Select all brushed items
app.executeMenuCommand("Brush Strokes menu item");
var doc = app.activeDocument;
var sel = doc.selection;
doc.selection = null;
//Create a temp document for working in
var tempDoc = documents.add();
//copy each selected item to the temp document and get associated Brush
var qty = 0;
for(var i = 0; i < sel.length; i++){
if(determine_brush(sel) == BrushName){
sel.selected = true;
qty++;
}
}
//finalise by returning focus to activeDocument and removing the temp working document
doc.activate();
tempDoc.close(SaveOptions.DONOTSAVECHANGES);
// the info in this alert to be shown in the HTML panel
alert("found " + qty + " brushed items matching the name '" + BrushName + "'.");
function determine_brush(item){
testItem = item.duplicate(tempDoc, ElementPlacement.PLACEATEND);
do_script();
if(tempDoc.brushes.length == 2 && tempDoc.brushes[0].name == "Touch Calligraphic Brush"){
testItem.remove();
return tempDoc.brushes[1].name;
}else if(tempDoc.brushes.length == 2 && tempDoc.brushes[1].name == "Touch Calligraphic Brush"){
testItem.remove();
return tempDoc.brushes[0].name;
}else if(tempDoc.brushes.length == 1){
testItem.remove();
return tempDoc.brushes[0].name;
}else{
alert("issue"); // this needs to be more than a basic alert, add a proper error catch.
}
testItem.remove();
}
function do_script(){
// Set your action name and the set name that it belongs to here
var myAction = "Remove_Unused_Brushes";
var mySet = "Scripted_Actions";
//---------------------------------------------------------------------------------
var currentInteractionLevel = app.userInteractionLevel;
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
try {
app.doScript(myAction, mySet, false);
if(tempDoc.brushes.length != 2){ // this test is required to get the catch to run, a failed doScript will not trigger it
throw new Error("Looks like the Action does not exist. Error catch will now try to create it.");
}
} catch(e) {
var actionStr = [
'/version 3',
'/name [ 16',
'53637269707465645f416374696f6e73',
']',
'/isOpen 0',
'/actionCount 1',
'/action-1 {',
'/name [ 21',
'52656d6f76655f556e757365645f42727573686573',
']',
'/keyIndex 0',
'/colorIndex 0',
'/isOpen 0',
'/eventCount 2',
'/event-1 {',
'/useRulersIn1stQuadrant 0',
'/internalName (ai_plugin_brush)',
'/localizedName [ 5',
'4272757368',
']',
'/isOpen 0',
'/isOn 1',
'/hasDialog 0',
'/parameterCount 1',
'/parameter-1 {',
'/key 1835363957',
'/showInPalette -1',
'/type (enumerated)',
'/name [ 17',
'53656c65637420416c6c20556e75736564',
']',
'/value 8',
'}',
'}',
'/event-2 {',
'/useRulersIn1stQuadrant 0',
'/internalName (ai_plugin_brush)',
'/localizedName [ 5',
'4272757368',
']',
'/isOpen 0',
'/isOn 1',
'/hasDialog 1',
'/showDialog 0',
'/parameterCount 1',
'/parameter-1 {',
'/key 1835363957',
'/showInPalette -1',
'/type (enumerated)',
'/name [ 12',
'44656c657465204272757368',
']',
'/value 3',
'}',
'}',
'}'
].join('\n');
createAction(actionStr);
app.doScript(myAction, mySet, false);
actionStr = null;
}
app.userInteractionLevel = currentInteractionLevel;
}
function createAction (str) {
var f = new File('~/ScriptAction.aia');
f.open('w');
f.write(str);
f.close();
app.loadAction(f);
f.remove();
}
}
//var brushName = "Charcoal - Feather";
var brushName = "5 pt. Flat";
select_same_brush(brushName);
Copy link to clipboard
Copied
Qwertyfly,
Did you get to a more finished state? This sounded so promising!
Copy link to clipboard
Copied
I have some messy code, but I got side tracked with a different script I actually Need for work.
I'll try to put some time in to it soon.
Copy link to clipboard
Copied
yes I asked on the Apple Applescript list too and Shane Stanley who does a lot of Adobe CC scripting professionally said good luck determining brush application to paths.
Copy link to clipboard
Copied
so what I did was just selected all the brushes and made sure they were on discrete layers of their own and counted sub totals and grand total. got myself there in the end, sometimes writing the question in public prompts the brain into gear.
Copy link to clipboard
Copied
Usually Illustrator actions are next to useless, however in this case they can record selecting objects with applied brushes, and they could be used to create a new layer and cut/paste in front into the new targeted layer, or one could simple use Document Info (Selection Only)/Objects to list the selected objects or brushes.
Copy link to clipboard
Copied
i made an action 10 years ago to create half-step patterns for use in fashion. you could move one of the objects and then it would replicate throughout the whole repeated pattern whatever size it was. last time I used them I think


-
- 1
- 2