Counting Path Items of a selection in Illustrator

I am trying to figure out a way to count the number of path objects that are within an active selection. I need it to then stop and warn the operator if more than 1 path items exist within the selection. Can anyone explain why I cannot count the path items of a selection in such a way?

tell application "Illustrator CS"
	tell document 1
		set sel to selection
		if (count path items of sel) > 1 or class of sel is group item then
			
			display dialog "This script will not work on a grouped objects or multi-selected objects" buttons "Cancel"
			if button returned of userResponse is "Cancel" then return
		end if
	end tell
end tell

Jeff,

I’m not sure exactly why it doesn’t work this way, but it will work like this:

tell application "Illustrator CS"
	tell document 1
		set sel to selection	
		if (count sel) > 1 or class of sel is group item then
			
			display dialog "This script will not work on a grouped objects or multi-selected objects" buttons "Cancel"
			if button returned of userResponse is "Cancel" then return
		end if
	end tell
end tell

PreTech

Thank you PreTech,
This is working great. This is a good thing for me to know, since I will use this over and over again I am sure :slight_smile:

-Jeff

The default for ‘Cancel’ is to quit the script, so you really don’t need a line to deal with it, so although I don’t have Illustrator, this should work (if you bother to try, let me know if it does):


tell application "Illustrator CS" to tell document 1 to tell selection to if count > 1 or class is group item then display dialog "This script will not work on a grouped objects or multi-selected objects" buttons "Cancel"

It’s kind of a hobby of mine to attempt one-liners :smiley:

Adam,
Your one line script is getting held up on “(count)”. I wouldn’t be able to tell you why, but I wanted to let you know because you were nice enough to look into this. Script editor says: Illustrator CS got an error: Internal Error

tell application "Illustrator CS" to tell document 1 to tell selection to if (count) > 1 or class is group item then display dialog "This script will not work on a grouped objects or multi-selected objects" buttons "Cancel"

I’m a bit late to this, but this information might help you in the future.

The reason that this does not work is that the result of the selection is a list of object references to the object or objects selected. You cannot get a count of path items of this list:

{compound path item 2 of layer 1 of document 1, group item 1 of layer 1 of document 1, compound path item 3 of layer 1 of document 1, path item 1 of layer 1 of document 1}

You would need to get the path items of each item of the list or you could count the items in the list as PreTech did.

The reason it doesn’t work is that a selection is a list of items, not path (group,etc.) items.

path items of selection is invalid…

items of selection returns a list of selected items: {path item 1 of layer 1 of document 1 of application “Illustrator CS”, path item 2 of layer 1 of document 1 of application “Illustrator CS”, path item 3 of layer 1 of document 1 of application “Illustrator CS”, path item 4 of layer 1 of document 1 of application “Illustrator CS”, path item 5 of layer 1 of document 1 of application “Illustrator CS”}

every item of selection whose class is path item: returns null.
Oddly, this “appears” not to work but really does… If you ask for explicit properties of the items you’ll see that it is working. You just didn’t ask it for anything it could display.

class of every item of selection whose class is path item: returns {path item, path item, path item, path item, path item}

Enjoy.
Jim Neumann
BLUEFROG

Needs some “it” in the right places, but don’t trouble yourself, I won’t fix it - I was just curious.

Thanks Jim.

Adam, I’m curious to see if the one liner can work just for the fun of it. I played around with this some but cannot get it to work. The reason seems to be centered around the “count” command. Illustrator gives “Internal error” when this is used to “count selection”. I set up the code with the usual tell blocks and told it to “count selection” and received the same error. However, when I set the variable “sel” to selection, it works. Anyone have any idea why this is? Is this only in Illustrator?

PreTech

Yup; I think it needs to be count (get selection) or perhaps count (get selection of it)

I shouldn’t have stuck my oar in - these one-liners always take some testing and tweaking, but I too do it just for the fun of it.

Adam,

That was it!

tell application "Illustrator CS" to tell document 1 to if (count (get selection)) > 0 or class is group item then display dialog "This script will not work on a grouped objects or multi-selected objects" buttons "Cancel"

Now, the question is what is different about get selection and just selection?

PreTech

When you set selection to a variable, AppleScript ‘does’ it. When you ‘get selection’ AppleScript does it. When you count AppleScript does it usually. When ‘selection’ is used in a tell statement of an app that has that as a key word, there seems to be confusion about which comes first - selection or counting. ‘get selection’ separates those. Kai wrote something about it at one point, but I can’t find it now; I just remembered that it helps when something involving ‘selection’ errors.

I am sorry to take so long to get back to this post. Sorry, I have nothing more to add but a simple “thank you” to everyone that helped explain these selection items or lists, you know what I mean :slight_smile: This was all extremely usefully information, and a post I will refer back to quite often.

I hope everyone has a good weekend.

-Jeff