I’m stumped as to how delete a page item. It’s an embedded jpeg in an AI doc. Basically, I can delete it by selecting it and running a simple script like this:
tell application “Adobe Illustrator”
tell current document
delete selection
end tell
end tell
However, if I try to find it and delete it as part of a larger script, it won’t delete. No error is thrown and everything else in the script works, but the item does not get deleted. Here’s a chunk of the script that I hoped would find and delete the item. Note that the script can find the item fine because I’m able to copy its position property and use it to position another item I’m pasting in.
set theList to every page item of document 1
repeat with e from 1 to count of theList
set theObject to item e of theList
if width of theObject = 26 then
set theBounds to position of theObject
delete theObject
paste
set Paster to item 1 of selection
set position of Paster to theBounds
end if
end repeat
Your problem comes about because references to items in AI are based on their index, or position front-to-back. So you get a list OK, but as soon as you start deleting and/or pasting, your list of references is out-of-date. You can probably work around the problem by loop from back to front, and moving the pasted items to the back after pasting them.
I agree with Shane, but I would add that the width statement may also be at issue. How and when did you obtain the width? You can’t trust that the visual measurement from dialogs in AI are wholly accurate. Those numbers are approximations and prone to rounding errors. Let’s say that your photo actually measures 26 points but you move it a few inches into a new position; it may now be 25.99999 points (or some similar change). Coercing the width to a real”or choosing some other property altogether”may prove less problematic.
Thanks guys. I’ve encountered both the problems you note, the backwards indexing of page items and the way that the specifications for object measurements in properties can run to a dozen digits after the decimal place, even when the GUI is showing an integer!
In this case, most of my problem came about by not referencing the the item I’ve found as a raster item. For some reason, if I loop through the document’s raster items rather than looping through all the page items. This syntax let’s me delete the items:
set theRasters to every raster item of document 1
repeat with h from 1 to count of theRasters
try
if width of item h of theRasters = 26 then
set theBounds to position of item h of theRasters
delete item h of theRasters
paste
set Paster to item 1 of selection
set position of Paster to theBounds
end if
end try
end repeat
(Thanks to Larry G. Schneider from Adobe’s AI Scripting forum for that one!)
This script is a bit of a kludge, especially where I paste in the replacement image for the one I’m deleting. I wish there was a script equivalent to the “relink” function in the GUI.
Although it’s nice to narrow down the search to only the intended items, none of your problem actually came from the reference. A raster item is one type of page item. The reason that code sample works is because the try block is preventing the (display of) errors.
I thought you were pasting text, for some reason. Changing the file path to a new image is the same as “relink”.
The odd thing is that I wasn’t getting any errors. Without the try block and looping through a list of page items (rather than raster items) the script would run fine. The delete line would neither throw an error nor actually delete the item.
Re: the “relink”
I had to find some embedded jpegs and replace them with vector art. I had thought that I could relink the embedded jpegs with a directory reference to a new AI file that I’d use as a replacement for the jpegs. That doesn’t work thought because the embedded jpeg no longer retains the file path in its properties.
I read somewhere that you could write a script to parse the postscript and look for the the original file reference and then replace that but that seemed daunting.
If you do a lot of this, you might consider working in CS. It looks like Adobe changed the raster items’ file path property to read-only in later versions”CS3 or possibly CS2”but, it initially worked that even embedded files have document-relative paths that could be relinked.