Illustrator Scripting :: Random Delete A Selection Of Items
Aug 25, 2013
I am trying to create a script to specify a percentage of the selected items you want to remove.Here is what I have so far.
I still need to work out on the alert prompt and the if statement, but for now, I have the feeling that the selection.length keep changing each time Illustrator delete an object, and I don't know how to make it stick.
var selection = app.activeDocument.selection;
//alert prompt dialog for a percentage of deletion
for (var i = 0; i < selection.length; i++) {
if (Math.random < 0.5) { //need to work out the percentage
alert("yes")
selection[i].remove();
}
}
First of all, some details as to what we are working on:
Mac OSX 10.6.8 Illustrator CS6 Accessing files on a shared network drive
I have been working on a file for a number of months now with no problems at all. Right at the point of it being signed off today I opened it to prep for printing (outline fonts, check colours etc.). When I opened it I noticed a number of the graphics (embedded graphics, not linked files) and areas of text were missing. Completely missing. No text boxes, no sign of any images having been there, just GONE. I closed the file without saving and opened it in CS5 - neither the graphics or the text appeared in CS5 either.
Nobody except myself and the other guy has access to these files (nor the softare to open them). The file has not been opened since June 6th when I last made a change (and everything was fine!).
This is not the only file we have discovered this problem with - there was another occurence of this with a completely different file yesterday which we put down to a small glitch, but this is now getting worrying. Yesterday it was graph items, so we thought it was something to do with those. Today it is bog standard graphics and text (no font issues either).
Now, I have also noticed something else with this particular file - it is acting more like an imported PDF than an original Illy file in that items are linked together (un-linked by right-clicking and selecting 'release clipping mask'). I don't know if this could be related? It was definitely created from scratch in Illustrator, and none of the items were linked in such way, or imported from another file.
I'm using a mac running illustrator cs5 and have about 4 thousand eps files (all vector paths filled with black) that need to be assigned colors randomly from a defined color palette (25 custom swatches). The first 2 swatches should be omitted- they aren't relevant to this task.
The script should open the file select all vector elements and assign a random color form the defined custom swatch palette then save the file and move on to the next.
I'm trying to find a way to loop thru all items in an Illustrator document and get their stroke and fill color. Sometimes items are spots, sometimes documents are set to RGB or CMYK, etc, so swatches can be a mixture of things. Is there an easy way to get the swatch name for each item?
For example, I can check if an item is a spot, and get it's color like so:
var c = app.activeDocument.pageItems[0]; if (c.strokeColor.typename === 'SpotColor') { var s = c.strokeColor.spot.name; }
But if I try to get the name from an item colored 'Black' (which is set to Process Color, CMYK), then I'll need to do something else like:
var c = app.activeDocument.pageItems[0]; if (c.strokeColor.typename === 'CMYKColor') { var s = c.strokeColor.name; // This returns undefined - not sure what I'm doing!!! }
Is there an easier way to achieve this, other than getting the typename of each item, and then trying to figure out it's color name? I was hoping something like this would work:
var c = app.activeDocument.pageItems[0].strokeColor.name;
I just got CS6, and I'm wondering how I access menu items using Javascript?Also, I'm looking at the CS6 JS reference, and it doesn't say whether app.activeDocument.selection is Read-Only. I would need this for my purposes in accessing the menu.The item I wanna access is Select > Same > Stroke Color. Seems simple enough.
I have a collection of circles and rectangles in my document. Is there any way to sort out circles from rectagles using JavaScript? Say i want to make my circles red and the rectangles blue.
I have a layer that contains two clipping groups (groups with a clip mask and other art). When I call layer.groupItems it returns an empty array. When I call layer.layers it returns the two clipping groups. I need to know if these layers are clipping groups, but if they are coming back as layers how can I figure that out?
I'm trying to remove all hidden page items regardless of how many sublayers they are burried in. I wrote a snippet here that seems to only partly work. It removes all hidden page items unless two hidden items exist right after each other in the stacking order.
for (i=0; i<doc.pageItems.length; i++){ if (doc.pageItems[i].hidden == true){ doc.pageItems[i].remove(); } }
How can I get width that multiple selected items? (Not grouped.)
//---------- source code ---------- var mm = 2.83464566929134 //unit conversion. (point->milimeter) var W = docRef.selection[0].width/mm; alert(W); //---------- source code ----------
I have script where I select items on multiple artboard but I can't seem to get get active art board of the selected item. Is this possible?
each artboard has a text item and I would like them in the same position on each of their own artboards that they are already one. right now it adjusts them all to one artboard. here is what I have so far
doc = app.activeDocument; for(i = 0; i < selection.length; i++){ var firstItemPosition = doc.selection[0].position; doc.selection[i].position = firstItemPosition; var activeArtboardIndex = doc.artboards.getActiveArtboardIndex(); alert(activeArtboardIndex); }
I have a very simple javascript for Illustrator that is intended to render all filled objects semi-transparent. It works nicely but for large documents with many objects this takes ages, even beyond the time it would take to do it manually.
I'm wondering if there is any way to improve this script so it works in a reasonable time for large files? I'm running Illustrator CS5 on a Windows 7 64-bit Intel Quadcore i5-2400 @ 3.10 GHz System with 12 GB of RAM and an AMD Radeon HD 6350 graphics. Would upgrading to CS6 significantly improve the speed? CPU load is only at 25%, but I guess javascript cannot be run in multiple threads with the Illustrator engine? Is there another scripting language that would allow me faster processing of that task in illustrator or is my code just poorly optimized?
with (app.activeDocument) { if (pathItems.length > 0) { for (var g = 0 ; g < pathItems.length; g++) { if (pathItems[g].filled == true) { pathItems[g].opacity = 50; } } } }
The first loop grabs the geometries drawn in the current document and rotates it around itself creating duplicates. The second loop is supposed to circumvent more geometries around the newly created geometries.
But instead of creating a radiant pattern, this code builds a tower: for the second loop everytime it grabs the last drawn geometry instead of what is in the "Original"
for NumPaths = 1 to frontDocument.PathItems.count set myPath = frontDocument.PathItems.item(NumPaths) For index = 0 To 30 set newPath = myPath.Duplicate call newPath.translate(100*sin(6.14* index/30),100*cos(6.14* index/30))
I'm trying to select every object with the color "CutContour" So far I've been trying:
myDoc.selection = myDoc.spots.getByName("CutContour"); I guess this obviouslly wouldn't work though because myDoc.selection is looking for an array of objects. However, I can't figure out how to loop through all the objects in the document and check to see if they contain the SpotColor.
I want to use the Select > Same > Stroke Color , but I heard that using that in Javascript isn't available unless you have CS6 (which I currently don't). Is it true that accessing menu items through scripting is a feature in CS6?
I'm trying to hide all items in a document (and store each items original visibility state), iterate user pre-selected items and perform an export for each item in selection (I've got this part covered), and finally restore the visibility state of each item again (here's where the storing of original visibility state is needed).
I've found that I can iterate all items in the document using app.activeDocument.pageItems[i] where i is the page item identified by the i-th index in the pageItems collection.
What is not clear, however, is how to get the visibility state of this page item. Indeed, there is a property visbilityVariable associated with page items, but when I try to "inspect" its content using an alert message, I get "undefined".
Are there better ways to iterate all items in a document? Ideally, I'd like to access all items regardless of wether they're path items, group items, text frame items or what have you. I need this collection to read/write the visibility state of each individual item.
I have developed a script to perform some Custom action in Illustrator. I want to make this script run from a a File Menu Item so it can be invoked easily. Is there a way to customize Illustrator to add a new menu item or modify the existing menu tems functions. For example if the script needs to be invoked when the user click "Save" is there a way that could be done.
What is the Javascript equivalent to this AppleScript?
set xxx to every page item whose note = "John"
After looking at some of the tutorials, I pieced this together. Is there a better way of doing this? With 30,000 path items it took over a minute to execute...
docRef = app.activeDocument; var pathLength = docRef.pathItems.length; docRef.pathItems[0].note;
I have Illusrator CS6, and it ask when I close, if I want to save to clipboard. Now the program runs slow. Is there any way to view clipboard and edit and/or delete items that are on it?
Im struggling with the correct use of symbol / symbol items in applescript. I can successfully list the symbols used on the page, but I cannot select them. How I can select all the items on the page that are Symbol Items of symbols named "Apples" but not oranges? After that I hope to replace the instances matching the height or width of the items whichever is greater with a 3rd item name. Javascript is always an option as well but I had the same difficulty.
Is it possible to script the menu item Object/Path/Outline Stroke?I need to cycle through all the Path items in the document and convert them to Outlines.
I'm trying to create a script in CS5 mac os10.6.8 that will change the fill color of selected pathItems. I need a simple script that will change a selected pathItem to cmyk values 2,3,15,0. I will then select this script and implement the action in a batch of 600 files.
I wish this process could be recorded as an action but when I record the action "add new swatch" I have to manually input the cmyk values, which will take forever for a batch of over 600 files.
I have been trying to mess with and target "tints" via javascript but either I am not understanding something or it cant be targeted as I had hoped, so I am looking for direction and guidance. I have a spot color assigned in the color swatches palette, and have drawn a few boxes on the page using this spot swatch. I then try to alter the tint using JS code.
sel[i].tint = // whatever number alert(sel[i].tint) // This alerts fine
The alert shows a change taking place but it never gets applied to the item, the actual item never gets changed. I know things are properly targeted as I can change other attributes fine. When I look in the JS documentation it just shows for when creating a new spot, not for ones that already exist on items, I cant seem to find an example otherwise.
Can tint be adjusted for items on the page that have a spot color applied?
I'm trying to write a script that applies a single swatch to a single path item and repeat for every swatch that is currently contained in the swatch library. This script that I have written is only applying the last swatch in the swatch library to every path item on the page. Currently I'm more concered with getting each swatch that is currently in the swatch library to be applied at least once before worrying about applying the same patch to any extra number of patch items compared to the swatches.length.
// Apply every swatch library color to path items on a page if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0 ) { doc = app.activeDocument;
I have one user who cannot plot correctly from his workstation. All of his drawings will be missing information when they are plotted although they look perfect in the print preview. For example, at the bottom of every drawiing, he puts in a single text block: "Drawn by: SLM" and sometimes the 'S' in his initials won't get plotted, etc. He can save any of those drawings to a shared drive and plot them from a different workstation and there is no issue. We are using AutoCAD Map 3D 2010 on Windows 7 32bit.
I'm new to illustrator scripting (but not to javascript or programming. I have an AI file that has multiple layers. each layer has several items that are supposed to be of same color. I have an array of HEX values.
I would like to loop over all items in a single layer, select them and change their color to some given HEX. Once that works, I will run on all HEX entries in array and save each variation in a separate PNG file.
For the first part i wrote some script (see below), but i do not see any change in the file after i run the script. No errors and no change. I run the script once i select all items in a specific layer.
My code:
function hexToRgb(hex) { var result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex); return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16)