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

Code to be evaluated! [007] // Resizing and distributing ! …

LEGEND ,
Nov 14, 2016 Nov 14, 2016

Copy link to clipboard

Copied

Hi Scripters,

I would like to apply the same width/height to a selection of frames and distribute them in the original area!

To be a little clearer:

I select the four frames (above) and I would like to have the result below!

Capture d’écran 2016-11-14 à 23.38.57.png

… I get this:

Capture d’écran 2016-11-15 à 00.12.46.png

Yes!  It seems to be good! Aha! … but not really!! I've an error message!

Capture d’écran 2016-11-15 à 00.23.13.png

The code I've written is:

var myDoc = app.activeDocument;

var mySel = app.selection; 

var myGroup = myDoc.groups.add(mySel);  

var myGB = myGroup.geometricBounds;

myGroup.ungroup();

var myNewWidth = UnitValue('25mm').as('pt');  

var myNewHeight = UnitValue('10mm').as('pt');

for (i = 0; i < mySel.length; i++) {

      if (mySel.geometricBounds[1] == myGB[1])  mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

      if (mySel.geometricBounds[3] == myGB[3])  mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_RIGHT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

      mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.CENTER_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

}

myDoc.distribute(mySel, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS);

Thanks in advance! 

(^/)

TOPICS
Scripting

Views

541

Translate

Translate

Report

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
People's Champ ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

The problem doesn't seem to be linked to your code specifically but from the Rorohiko API implementation which weirdly you doesn't seem to call here.

Do you include it somewhere in the code ?

Plus, if you really want to be effective and error free, you should secure your code by avoiding reaching objects that may not be avalaile at some point.

var myDoc = app.activeDocument;

That relies on a existing document but you may run your script when there aren't any.

So you may prefer

function main() {

     var doc;

     if ( app.documents.length) return;

     //Now you can go safe

     doc = app.activeDocument;

     //do something with doc

}

HTH

Votes

Translate

Translate

Report

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
LEGEND ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Hi Loic,

No real reason to run this script without open file! 

… but you're right: no reason to run it without a selection!! … So [including an "undo!"]:

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Resize and Distribute! …");

function main()

{

   

    var myDoc = app.activeDocument;

    var mySel = app.selection;

    if (mySel.length == 0)  exit();

    else {

        var myGroup = myDoc.groups.add(mySel);  

        var myGB = myGroup.geometricBounds;

        myGroup.ungroup();

        var myNewWidth = UnitValue('25mm').as('pt');  

        var myNewHeight = UnitValue('10mm').as('pt');

       

        for (i = 0; i < mySel.length; i++) {

            if (mySel.geometricBounds[1] == myGB[1])  mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

            if (mySel.geometricBounds[3] == myGB[3])  mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_RIGHT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

            mySel.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.CENTER_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [myNewWidth, myNewHeight]);

        }

        myDoc.distribute(mySel, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS);

    }

}

However, I've the same error message! 

(^/)

Votes

Translate

Translate

Report

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
Mentor ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

@@Hi,

Notice an error message:

Source: if(theNewLabel == "subscribe") ....

I cant see this line in provided code so make it clear first.

Jarek

Votes

Translate

Translate

Report

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
LEGEND ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Hi Jarek and Loic!

I've just discussed with Kris from Rorohiko!

The problem came from "PlaceAutoSynchronizer" plugin installed in the ID "Plug-Ins" folder, sub-folder "Rorohiko".

Kris told me to remove this plugin and the problem is fixed now!

Cool! No more error now! … That doesn't mean my script is … perfect! 

But I keep learning! …

(^/)

Votes

Translate

Translate

Report

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
People's Champ ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

No real reason to run this script without open file!

Ha ha ha . Well you may not work much with end users

I mean it's better to take good habits

Votes

Translate

Translate

Report

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
LEGEND ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

LATEST

Well! I know you're right! I read, especially here, so "nice" comments of users who really don't know how to use ID!

… and not only newbies! …    So, I need to have good habits before they apply on a "pretty" button and trigger World War III by inadvertence! Knowledge give us Responsibility, as Uwe Laubender said once in another topic!

(^/) 

Votes

Translate

Translate

Report

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