Ungroup and then Regroup in Illustrator

Is this possible?
Using Illustrator CS:
I want to try and ungroup an active selection. But sometimes an active selection can contain many groups of groups of groups, etc.
So I wanted the script to trace the amount and way it “ungroups” until it can’t do this any more.

Once all objects are ungrouped I could insert a much larger script.

But here is even a harder part. How can the objects be regrouped in reverse manner as how they were ungrouped?

I don’t mind if this can’t be done. I just wanted to ask just in case.

The system events for group is:

tell application "System Events"
				keystroke "g" using command down
			end tell

And the system events for ungroup is:

tell application "System Events"
				keystroke "g" using command down & shift down
			end tell

Even if I can only regroup once at the end of the script, it would still be helpful.

Jeffkr,

I don’t know if this is what you’re looking for, but try this.

tell application "Illustrator CS"
	activate
	set thisDoc to current document
	tell thisDoc
		if class of selection is group item then
			set layName to name of layer of selection
			move every path item of selection to beginning of layer layName
			
			-- do other actions
			get properties of selection
			set thePaths to selection
			set groupRef to make new group item at end of layer layName
			move thePaths to end of groupRef
		end if
	end tell
end tell

PreTech

Jeffkr,

I did not look closely enough to what you were doing. Here is another script. See if this works for you.

set groupList to {}
tell application "Illustrator CS"
	activate
	set thisDoc to current document
	set n to 1
	tell thisDoc
		set theItems to selection
		repeat with x in theItems
			if class of x is group item then
				set properties of x to {name:"group" & n}
				set groupList's end to name of x
			end if
		end repeat
		repeat with i from 1 to (count groupList)
			set selection to group item (item i of groupList)
			set anItem to selection
			try
				set thePaths to every path item of anItem
				set theTexts to every text frame of anItem
				set allItems to thePaths & theTexts
			end try
			tell application "System Events" to keystroke "g" using command down & shift down
			display dialog "items ungrouped" -- do what you want here. If you make another selection here then you will have to select all items in the original group for the next line to work. This assumes that the selection is still the same.
			tell application "System Events" to keystroke "g" using command down
		end repeat
	end tell
end tell

PreTech

Pretech,
This is what I was looking for.
Very nice work.
Thanks again :slight_smile: