Sorting layers in illustrator

Anyone have a quick recipe for sorting layers by name in an illustrator document via applescript? I need to arrange layers by name to perform different handlers based on such names.

The layers will be group items named from the number 4 to 1, with the larger names being acted upon first, then 3, and so on down to 1. I’ll want to perform handlers on each layer, one by one via loop. The loop will repeat a max of 16 times so the efficiency isn’t as important as getting them in the correct order.

Thanks in advance!

So, are we talking about layers or groupitems that sit inside layers? And if it’s groupitems, will they all sit inside the same layer? Or are we talking about layers inside layers (sublayers)?

They will all be sublayers under “layer 1”

Thanks again!

Cool. Do you want to re-order the layers just to run the script in the right order? If that’s the case, it’s easier to keep the layers in the order they are and adjust the way the script cycles through the layers.

I need the largest “ie #4” at the top because the scripts that will follow will arrange the art in the layers on the document to specific coordinates on the page.

The page will consist of a grid of 16 squares, 4x4. The number 1 art will take up one square, the 2 art 2 squares and so on. I want to arrange the largest art first, keeping track of the remaining empty squares, then filling in the remaining empty squares with the smaller art layers down the line until the grid is full.

So, 4’s (if any) will completely fill a layer, 3’s will leave one remainder and so on. By looping through the layers, I can work my way down to the singles, placing them where needed (that’s the logic, anyway).

Here’s what I started to do. I figure I can change the whose loops to add the 3’s, 2’s and 1’s. I’m new to this, so I’m winging it as I go…

I think the logic is sound, but if it isn’t, or if there’s a better way to accomplish what I’m trying to do, I’M MORE THAN WILLING TO LEARN! Thank You!

set slot_1 to {x:0, y:808.5} – Coordinates of the 16 available die slots on the template.
set slot_2 to {x:180, y:808.5}
set slot_3 to {x:360, y:808.5}
set slot_4 to {x:540, y:808.5}

set slot_5 to {x:0, y:606.5}
set slot_6 to {x:180, y:606.5}
set slot_7 to {x:360, y:606.5}
set slot_8 to {x:540, y:606.5}

set slot_9 to {x:0, y:404.25}
set slot_10 to {x:180, y:404.25}
set slot_11 to {x:360, y:404.25}
set slot_12 to {x:540, y:404.25}

set slot_13 to {x:0, y:202}
set slot_14 to {x:180, y:202}
set slot_15 to {x:360, y:202}
set slot_16 to {x:540, y:202}

set Slots_left to 16

set layer_one_remainder to 4 --These are used to hold the remaining number of open slots should a 3, 2 or 1 die occupy a layer
set layer_two_remainder to 4
set layer_three_remainder to 4
set layer_four_remainder to 4

global template_exists
set template_exists to {}
set template_is_full to false

tell application “Adobe Illustrator”
activate
–my draw_template(template_exists)
copy
open file (“macintosh hd:cetip”)
paste

set total to (count of group items of document 1) --Counts the number of dies currently on the template.	

tell application "Adobe Illustrator"
	
	repeat with i from 1 to total
		(every group item of document 1 whose name = "4")
					
		if Slots_left = 16 and layer_four_remainder = 4 then
			set selection of document 1 to ¬
				{group item i of document 1}
			set position of selection to {x of slot_1, y of slot_1}
			set Slots_left to 12
			set layer_four_remainder to 0
			
		else
			if Slots_left = 12 and layer_three_remainder = 4 then
				set selection of document 1 to ¬
					{group item i of document 1}
				set position of selection to {x of slot_5, y of slot_5}
				set Slots_left to 8
				set layer_three_remainder to 0
				
				
			else
				if Slots_left = 8 and layer_two_remainder = 4 then
					set selection of document 1 to ¬
						{group item i of document 1}
					set position of selection to {x of slot_9, y of slot_9}
					set Slots_left to 4
					set layer_two_remainder to 0
					
					
					
				else
					if Slots_left = 4 and layer_one_remainder = 4 then
						set selection of document 1 to ¬
							{group item i of document 1}
						set position of selection to {x of slot_13, y of slot_13}
						set layer_one_remainder to 0
						set Slots_left to 0
						
						set template_is_full to true
					end if
				end if
			end if
		end if
	end repeat

I forgot to add that several people on the network will add art, one layer at a time, until the grid is full. So, this layout needs to be redrawn every time a new layer is added. The script is run to reflect the new layer added, then saved for the next person.

I think I sort of see what you’re trying to do. All I would say is that, as long as the artwork doesn’t overlap, there really isn’t any reason to move the layers up and down.
I may of course be misunderstanding the logic. If you want to re-order the layers alphabetically, I would:

set layerNameList to name of every layer of layer 1
--order layerNameList alphabetically [url=http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html](here's a sorting routine)[/url]
repeat with currentLayerName in layerNameList
tell layer 1
move layer currentLayerName to end
end
end

I haven’t tried this out as I’m at work and really shouldn’t be doing this :slight_smile: , so beware

You may have to replace

move layer currentLayerName to end

with

move layer currentLayerName to beginning

in case it comes out the wrong way

I’m at work as well! I’ll try this as soon as I can and get back to you! Thanks for all your help!

The one thing that occurred to me is that in my scrappy bit of scripting, I’m addressing the layers by their name which could potentially be a problem if there are two or more layers with the same name.

Indeed it does.

It returns the list, in order, and includes only one copy of each number. Not sure if I did something wrong, but it threw a flag when trying to move the layer around. Hmmmmm.

I found what I was missing to make this work. I knew about the phrases: Move to end, move to beginning, which is what I was using in my IF/THEN statements. It was driving me crazy that I couldn’t find out if there were others, which would allow me to move in between layers. The ones I was looking for are: Move to BEFORE, Move to AFTER, which until now, I didn’t know existed. Now, I can move groups where needed, making short work of my max 16 slots sort.

Now, my sort can be much more effective. Thanks for all your help!!!

Glad to hear you figured it out.