I am trying to adjust layer order to the logical read friendly top left page items at the beginning to bottom right page items at the end of layer order. I wrote this script but its not producing the desired results. Any ideas welcome?
idealistically I would want the script to read & match layer order like a newspaper, left to right but with columns of page items.
tell application "Adobe Illustrator"
activate
tell document 1
set allpositions to position of group items ------ of group item "group art var"
set neworderlist to my simple_sort(allpositions)
repeat with x in neworderlist
--move (every group item of group item "group art var" whose position = x) to beginning
move (every group item whose position = x) to beginning
end repeat
end tell
end tell
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
Hi. Your sort method doesn’t work on lists of lists and incorrectly flips to the lesser value; e.g. sorting ({1, 2}, {-2, 4}, {1, 1}) yields {“-2”, “1”}. I think you want {x, y} planar coordinates sorted on the first item.
tell application "Adobe Illustrator"'s document 1
set neworderlist to my rectify(my sort((my entangle(get group items's position))))
--return result --this is meant to actually be done once, to confirm proper order
repeat with x in neworderlist
move (group items whose position = x) to beginning
end repeat
end tell
-----------------------------------------------------------------------------------------------------------------------
on entangle(whatever)
set text item delimiters to ","
set newlist to {}
repeat with x in whatever
set newlist's end to (get {x's item 1, x's item 2}) as text
end repeat
newlist
end entangle
on sort(whatever)
set AppleScript's text item delimiters to linefeed
(do shell script "echo " & (whatever as text)'s quoted form & " | sort -g")'s paragraphs
end sort
on rectify(whatever)
set text item delimiters to ","
set newlist to {}
repeat with x in whatever
set newlist's end to {x's text item 1 as number, x's text item 2 as number} --changed from "x's text items" to coerce to real
end repeat
newlist
end rectify
tell application "Adobe Illustrator"'s document 1
set allGroupItems to every group item
set z to 0
set table to {}
repeat with x in allGroupItems
set z to z + 1
set label to "groupitem" & z as string
set name of x to label
set {c, y} to position of x
set table to table & y & "," & label & return as string
end repeat
set AppleScript's text item delimiters to linefeed
tell me to set table to (do shell script "echo " & (table as text)'s quoted form & " | sort")'s paragraphs
set AppleScript's text item delimiters to ","
repeat with x in table
move (group item (last text item of x)) to beginning ---end
end repeat
redraw
end tell
I provided a way to deal with a binary sort; all three handlers were part of one method that arranges paired lists of numbers contained in the variable allpositions from their leftmost to rightmost. You are now attempting to sort multiple classes of unpaired objects. I’m at a loss to account for the unexplained changes in the question.
the original script was not functioning properly where result of (every group item of document 1 whose position = X) produced nil {} & I couldn’t figure out where the syntax error was.
So basically I only the later order in a loosely set column of group items to be reordered.
A null value might possibly happen if there are multiple objects at the exact same location, resulting in duplicate list values. You can verify this by enabling the commented out command in post #2. The easiest solution is to wrap your move statement in a try/end try block, so that impossible moves aren’t attempted. Alternatively, the sort can use another shell command to filter out duplicates.
set sorted to (do shell script "echo " & (newlist as text)'s quoted form & " | sort -n | uniq")'s paragraphs
I had no luck with the sample scripts on my mac, mainly where the x in the sorted doesn’t get recognized as a comparable position for any AI object. Using your shell sort script, I wrote this for now until I can think of something better.
tell application "Adobe Illustrator"'s document 1
set gi to group items
set al to {}
repeat with x in gi
set y to item 2 of (x's position) as string
set x's name to y
set al to al & y
end repeat
set AppleScript's text item delimiters to linefeed
repeat with x in ((do shell script "echo " & (al as text)'s quoted form & " | sort -g")'s paragraphs)
move (group item x) to beginning
end repeat
end tell
The AI portion of post #2 starts by obtaining positions; there should always be comparable matches, when there are groups in the document. Perhaps the reference failed to be realized and requires an explicit get. Post #2 has been updated. Test on a dummy document with just a few objects and post the event log, if the outcome is other than expected.