Re: Adobe Photoshop Bounds of a layer?
Re: Adobe Photoshop Bounds of a layer?
- Subject: Re: Adobe Photoshop Bounds of a layer?
- From: Alex Zavatone <email@hidden>
- Date: Thu, 04 Sep 2014 16:28:40 -0400
Well, not sure, since I'm not sure if the .js would behave identically to the AS or not.
Sent from my iPad
On Sep 4, 2014, at 4:02 PM, Dave <email@hidden> wrote:
> Hi Alex,
>
> Yes that would probably work too and worth me thinking about.
>
> It’s to do with the way in which layers “float” until they are flattened. So, a whole PSD have it’s original set somewhere in a infinite space, but the extent, width and height are constant.
>
> I solved it but adding a fill layer the whole size of the document and give it a “Magic” name. This layer, will have a result that is offset by the same amount as the rest of the layers, BUT since you know that this is the base for all enclosing layers then you can use it’s X, Y as an offset from where you want it to originate.
>
> Look at this JS function:
>
> //*****************************************************************************************
> //
> // normalizeBoundRect
> //
> //****************************************************************************************
> function normalizeBoundRect(theBoundsRect,theXOffset,theYOffset)
> {
> var myTopPosition;
> var myLeftPosition;
> var myBottomPosition;
> var myRightPosition;
> var myBoundsRect;
> var myXStart;
> var myXEnd;
> var myYStart;
> var myYEnd;
> var myWidth;
> var myHeight;
> var myXDifference;
> var myYDifference;
>
> // Y X yEnd XEnd
> // 0 1 2 3
> // theBoundsRect is [left, top, right, bottom].
>
> myLeftPosition = theBoundsRect[0].value;
> myTopPosition = theBoundsRect[1].value;
> myRightPosition = theBoundsRect[2].value;
> myBottomPosition = theBoundsRect[3].value;
>
> myXStart = myLeftPosition - theXOffset;
> myYStart = myTopPosition - theYOffset
> myXEnd = myRightPosition + theXOffset;
> myYEnd = myBottomPosition + theYOffset;
> myWidth = myXEnd - myXStart;
> myHeight = myYEnd - myYStart;
>
> myBoundsRect = {};
> myBoundsRect[0] = myXStart;
> myBoundsRect[1] = myYStart;
> myBoundsRect[2] = myWidth;
> myBoundsRect[3] = myHeight;
>
>
> $.writeln("-----");
> $.writeln("PhotoShop - Rect: " + "[" + theBoundsRect[0] + "," + theBoundsRect[1] + "," + theBoundsRect[2] + "," + theBoundsRect[3] + "]");
> $.writeln("Cocoa - Rect: " + "[" + myBoundsRect[0] + "," + myBoundsRect[1] + "," + myBoundsRect[2] + "," + myBoundsRect[3] + "]");
> $.writeln("Using Offset X: " + theXOffset + " Offset Y: " + theYOffset);
> $.writeln("-----");
>
> return myBoundsRect;
> }
>
> You call it once it initialise the offset as so:
>
> //**
> //** This gets the “Magic” Layer offset by 0,0 e.g. nothing so it’s still minus based, e.g.. -1-1 = -1 -1
> //**
> myBounds = normalizeBoundRect(theBoundsOfMagicLayer,0,0);
> myOffsetX = myBounds[0]; //-1
> myOffsetY = myBounds[1]; //-1
>
> //**
> //** Now to get the Cocoa Rect of a Layer do this:
> //**
> myLayerCocoaBounds = normalizeBoundRect(theBoundsOfMagicLayer, myOffsetX, myOffsetY);
> myNewX = myLayerCocoaBounds[0]; //0 - As we want it!
> myNewY = myLayerCocoaBounds[1]; //0
>
>
> What do you think?
>
> All the Best
> Dave
>
>
> On 4 Sep 2014, at 20:24, Alex Zavatone <email@hidden> wrote:
>
>> How are you getting the bounds? My olden scripts used to select all, create a new layer, fill it with a color and then get the bounds for that.
>>
>> It was my old solution to get the bounding rect for a document, thought there must be a better way. It worked though.
>>
>> Sent from my iPad
>>
>> On Sep 4, 2014, at 2:27 PM, Dave <email@hidden> wrote:
>>
>>> Hi,
>>>
>>> Thanks, I just needed verification, I have been searching for info and the 0 relative Array came form looking at some JavaScript and forgetting they were different in AS.
>>>
>>> I think I’ve sorted it out now, but if anyone knows a better solution I’d be grateful if you’d share!
>>>
>>> I have a PSD that is 40,40 Pixels
>>>
>>> I then have 4 Fill Rects at, X,Y with a Width, Height of 20,20
>>>
>>> Rect1: X,Y: 0,0
>>> Rect2: X,Y 20,0
>>> Rect3: X,Y 0,20
>>> Rect4: X,Y 20,20
>>>
>>> Now when I read bounds in my Script I get everything offset by -1 or sometimes -2. From experimenting with other PSD this negative offset various.
>>>
>>> Why is this?
>>>
>>> I’ve fixed it by calculating the difference in X,Y and then adding/subtracting from each layers X,Y.
>>>
>>>
>>> Basically I want the coordinate Starting at 0,0, how come PS is giving me negative numbers?
>>>
>>> Bye the way, this same format as the pre Mac OS X, Mac Toolbox and Carbon, I need the values in Cocoa style, e.g.
>>>
>>> X,Y W,H
>>>
>>> Thanks for your help, I really appreciate it.
>>>
>>> All the Best
>>> Dave
>>>
>>>
>>> On 4 Sep 2014, at 13:01, Alex Zavatone <email@hidden> wrote:
>>>
>>>> This might help.
>>>>
>>>> set artboardRect to the bounds of layer 1 of myDoc
>>>> set myLeft to item 1 of artboardRect
>>>> set myTop to item 2 of artboardRect
>>>> set myRight to item 3 of artboardRect
>>>> set myBottom to item 4 of artboardRect
>>>>
>>>> I've been using it since 2008.
>>>>
>>>> On Sep 4, 2014, at 6:05 AM, Dave wrote:
>>>>
>>>>>
>>>>> On 3 Sep 2014, at 00:02, Martin Orpen <email@hidden> wrote:
>>>>>
>>>>>> On 2 Sep 2014, at 08:05, Dave <email@hidden> wrote:
>>>>>>
>>>>>>> It seems that bounds is specified to be Top, Left, Bottom,Right (old pre MacOS X style Rects), my question is, to correctly calculate the Width and Height, should the code be:
>>>>>>
>>>>>>
>>>>>> Photoshop’s layer bounds are Left, Top, Right, Bottom
>>>>>>
>>>>>> So
>>>>>>
>>>>>> width = bounds[3] - bounds[1]
>>>>>> height = bounds[4] - bounds[2]
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> Thanks for taking the time to answer.
>>>>>
>>>>>
>>>>> I don’t understand this, surely it’s:
>>>>>
>>>>> bounds[0] = Left (or top?)
>>>>> bounds[1] = Top (or left?)
>>>>> bounds[2] = Right (or bottom?)
>>>>> bounds[3] = Bottom (or Right?)
>>>>>
>>>>> Does the array start at 0 or 1?
>>>>>
>>>>> I can’t seen to find it written in stone anywhere - if you know where Bounds is defined I’d be grateful if you could point me to it.
>>>>>
>>>>> I just want to be 100% certain I’ve got the right coordinates because I’m seeing some strange behaviour.
>>>>>
>>>>> Thanks a lot for your help,
>>>>>
>>>>> Dave
>>>
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> AppleScript-Users mailing list (email@hidden)
>>> Help/Unsubscribe/Update your Subscription:
>>> Archives: http://lists.apple.com/archives/applescript-users
>>>
>>> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden