occasionally you’re going to try to develop a script that just isn’t going to work (at least, without a large portion of it devoted to a workaround to deal with applescript’s shortcomings). but even in trying, you still learn new stuff. this is one of those scripts.
the plan was to make this one part of a script for those anal types who like they’re files to be neat, neat, neat. cleaning out unused swatches and layers should be easy as :
tell application "Adobe InDesign CS4" tell active document delete unused swatches delete unused layers end tell end tell
ok, the first part works — no worries. but check the dictionary and you’ll find there’s no such thing as unused layers. why? who the hell knows.
anyway, don’t let that stop us. it should still be easy — you just need to identify which layers do not have any page items, and delete them. getting page items will result in a list and an empty list looks like this — {} :
tell application "Adobe InDesign CS4" tell active document repeat with x from 1 to (count layers) get all page items of layer x if result is {} then delete layer x end if end repeat end tell end tell
now, the first reason this fails should be obvious to anyone who has tried deleting things with a repeat loop before. here’s the explanation : imagine a file with three layers — layer one has page items, so it gets skipped — layer two is empty, so it gets deleted — that just leaves layer three — but there IS no layer three to check, because there are only two layers in the file now.
what to do? well it’s actually quite simple — just go through the layers in reverse order — 3 to 1. see the difference in the repeat line :
tell application "Adobe InDesign CS4" tell active document repeat with x from (count layers) to 1 by -1 get all page items of layer x if result is {} then delete layer x end if end repeat end tell end tell
rock on. works like a treat. and it might be perfect for your purposes — depending on how you work.
BUT, the reason the script is a dud is that it will also delete layers whose page items only exist on a master page. that is, imagine you have a layer which is specifically for holding master page items and you don’t put anything on that layer in the document pages themselves — well, that layer gets deleted by this script. bugger!
so, your homework for today is to come up with the shortest fool-proof workaround for this problem. have fun and keep grunting.
Have you ever managed to find a answer for this as I am struggling to get it to check master items?
I’ve now managed it. Not sure how it compares to yours though.
Cheers
Andy
Good on you Andy
I never bothered pursuing the answer.
I probably got distracted by a shiny something.
m.
Can you update this lesson with the answer? I don’t have time for homework!
No. I don’t have time for lazy students. :-)+)
m.