Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Group Selected Items [Javascript / CS3]

New Here ,
Feb 04, 2011 Feb 04, 2011

This seems like it'd be an extremely easy thing to do, but I'm unable to figure it out after searching forums, google, and the scripting documentation. I'm trying to just group whatever items are selected when I run the script. Some of the things I've tried are:

var document = app.activeDocument;

var selection = document.selection;

//this doesn't work, I get 'invalid parameter'

//for the groups.add() function.

var group = document.groups.add(selection);

//same here, I get invalid parameter on groups.add()

var itemArray = new Array;

for (var i in selection) {

    itemArray.push(i);

}

var group = document.groups.add(itemArray);

//also doesn't work. Debugging shows that the itemArray.push()

//is never reached, so the isPrototypeOf isn't working like I expect.

var itemArray = new Array;

for (var i in selection) {

    if (i.prototype.isPrototypeOf(PageItem)) {

        itemArray.push(i);

    }

}

var group = document.groups.add(itemArray);


Do I just have something fundamentally wrong with how I'm trying to approach this? I'm new to Javascript, I've worked a lot with Java (Don't worry, I know they're completely unrelated), but I've got a few other scripts working, this particular issue I can't seem to figure out, though.

Thoughts?

Thanks!

TOPICS
Scripting
2.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Feb 04, 2011 Feb 04, 2011

groups.add indeed only accepts an array of page items.

//same here, I get invalid parameter on groups.add()
var itemArray = new Array;
for (var i in selection) {
    itemArray.push(i);
}
var group = document.groups.add(itemArray);
 
//also doesn't work. Debugging shows that the itemArray.push()

Your "for" loop syntax is wrong. Use this:

for (var i=0; i<selection.length; i++)

   itemArray.push (selection);

That ought to make it work, methinks.

I guess you don't have to explicitly check if your selectio

...
Translate
Advocate ,
Feb 04, 2011 Feb 04, 2011

groups.add indeed only accepts an array of page items.

//same here, I get invalid parameter on groups.add()
var itemArray = new Array;
for (var i in selection) {
    itemArray.push(i);
}
var group = document.groups.add(itemArray);
 
//also doesn't work. Debugging shows that the itemArray.push()

Your "for" loop syntax is wrong. Use this:

for (var i=0; i<selection.length; i++)

   itemArray.push (selection);

That ought to make it work, methinks.

I guess you don't have to explicitly check if your selection are page items -- or perhaps you only have to check the very first item. It's impossible to select both a text range and page items

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 04, 2011 Feb 04, 2011

I could have sworn I'd tried that already to the same effect! Yet, it worked.

Thank you very much, that did the trick. Figures it was something simple!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 04, 2011 Feb 04, 2011
LATEST

Also, you can't make group in "document" you need to target "page" instead.

So this would work:

myObj = app.selection;
app.activeWindow.activePage.groups.add(myObj);

Or this:

myObj = app.selection;
app.activeDocument.pages[0].groups.add(myObj);

Hope that helps.

--

tomaxxi

http://indisnip.wordpress.com/

http://inditip.wordpress.com/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines