Skip to main content
Participating Frequently
January 19, 2011
Question

Document width/height in ActionScript

  • January 19, 2011
  • 1 reply
  • 2739 views

Hello,

I'm in the (sometimes frustrating) middle of the "porting scripts from JS to AS3" process. A couple of little findings to share.

I've found that what is a simple division of numbers in JS:

var myHeight = app.activeDocument.height;

var myWidth = app.activeDocument.width;

var ratio = myWidth/myHeight;

needs to be written in a slight different way in ActionScript3:

var myHeight:Number =  parseFloat(app.activeDocument.width.valueOf())

var myWidth:Number = parseFloat(app.activeDocument.height.valueOf());

var ratio:Number = myHeight/myWidth;

That's because, for some reason, it seems that app.activeDocument.width is not a String, like the API reference says.

Not a Number as I would have supposed, but an Object - at least this is what comes from the ExtensionBuilder code hinting:

and from the Problems tab in Flash Builder.

Is that... correct?!

So, in order to divide height and width, I have to parseFloat() their valueOf().

Now, since in one of the next steps I need to fill an app.activeDocument.resizeImage() method, the required parameters are:

width:Object

height:Object

resolution:Number

plus a resampleMethod

In my script I have to divide width and height by a scaling factor, then use them in the resizeImage() as Objects.

I can do this in two ways:

var scalingFactor:uint = 4:

var finalHeight:Object = myHeight/scalingFactor;         // myHeight and ratio

var finalWidth:Object = finalHeight.valueOf() / ratio;   // as calculated before

So the result of a division of a Number (myHeight) and a Uint (scalingFactor) can be automatically casted as an Object.

While an Object (finalHeight) can't be directly divided by a Uint, but its valueOf() yes, and the result is finally casted as an Object.

Having spent some extra time scratching my head with this, apparently, trivial porting of three JS code lines, I hope to be of some help to those approaching a similar problem.

Regards,

Davide

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
January 20, 2011

Try asking your question  in the actionscript forum http://forums.adobe.com/community/flash/flash_actionscript3

JJMack