• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• get text frame's angle of rotation

# get text frame's angle of rotation

New Here ,
Nov 06, 2008 Nov 06, 2008

Copied

Hello. Does anyone know if there's a way to get/return a text frame's angle of rotation? I've searched through the CS4 AppleScript documentation and the closest I found was a text frame's "matrix" property which seems to be used to rotate an object but not return an object's current angle of rotation. (There is a character rotation property but this seems to return the rotation of each character in the text frame, not the text frame as a whole.)

Ben
TOPICS
Scripting

Views

5.0K

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
10 Replies 10
New Here ,
Nov 06, 2008 Nov 06, 2008

Copied

After a bit more reading I learned that the matrix property of a text frame seems to return a 2x2 rotation matrix (http://en.wikipedia.org/wiki/Rotation_matrix) and that you can input the first two values of the matrix (in reverse order) into a two-argument arctangent function (http://en.wikipedia.org/wiki/Atan2) to calculate the angle of rotation. I don't know if AppleScript has a two-argument arctangent function, but I ran a few calculations with Ruby's Math.atan2 function and it worked great. If anyone knows of a better/easier way, please let me know, but for now this way should do the trick.

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 Beginner ,
Nov 07, 2008 Nov 07, 2008

Copied

Hi Ben,

I posted a similar request on Apple's AppleScript Users Forum. I can't find the thread at the moment, but here's the code that others helped me to develop.

I'd be interested to see your code.

Simon

-- SCRIPT --

set {theta_deg, hscale, vscale, skew} to my decodeMatrix(matrix of text frame 1 of document 1)
end tell

on decodeMatrix(mat) -- requires Satimage.osax
set _a to mvalue_a of mat
set _b to mvalue_b of mat
set _c to mvalue_c of mat
set _d to mvalue_d of mat
end tell
set theta to atan2 {_c, _d}
set cos_theta to cos (theta)
set hscale to roundThis(_a / cos_theta, 2)
set vscale to roundThis(_d / cos_theta, 2)
set skew to roundThis(_c / cos_theta, 2)
set theta_deg to roundThis(theta / pi * 180, 2)
return {theta_deg, hscale, vscale, skew}
end decodeMatrix

on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
tell (n * x) to return (it div 0.5 - it div 1) / x
end roundThis

-- END SCRIPT --

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 07, 2008 Nov 07, 2008

Copied

Thanks Simon! That's sounds like a great solution. Here's what I had done using Ruby:

set m to matrix of text frame 1 of current document
return do shell script "ruby -e 'puts Math.atan2(" & mvalue_b of m & "," & mvalue_a of m & ") / Math::PI * 180'"
end tell

(Note that counter-clockwise rotations will be returned as positive and clockwise will be negative.)

Report

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

Copied

Be aware that if the rotated text has ever had other disproportionate transformations applied, the values returned by the matrix manipulations may not be what you want.

For example, if your intention is to straighen rotated text, and the text frame has been disproportionately scaled, or if the text characters have a width setting other than 100%, the script may not work to realign the textframe to horiz/vert.

JET

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 Beginner ,
Nov 07, 2008 Nov 07, 2008

Copied

Yeah, and if the object has been skewed too, the values are different.

I wish there was just a range of properties for all transformation values that we could use instead. I think InDesign supports something like this.

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 Beginner ,
Nov 07, 2008 Nov 07, 2008

Copied

The other thing I don't fully understand and haven't explored is that when an object has been rotated it gets a new tag called "BBAccumRotation".

If I rotate a text frame 45Â°, the value of the BBAccumRotation tag is 0.785398.

Anyone any idea what this evaluates too? There's nothing in the scripting docs.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Nov 09, 2008 Nov 09, 2008

Copied

Report

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

Copied

I don't think you need to go to Ruby or satimage for this. Neat though it is to figure out the math of the matrix and implement this yourselves, Illustrator's scripting dictionary has the commands to make Illustrator translate an angle to a matrix value and vice versa:

see

concatenate rotation matrix

and

get rotation matrix

in the language dictionary.

There are the equivalent commands for scale and translation matrices too.

In the Illustrator CS6 Applescript Dictionary, I'm looking at Page 99.

Report

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

Copied

Hey, nevermind. Looks like you can convert an angle to a matrix, and transform a matrix by adding an angle, but not get the angle from an existing matrix.

Nice work with the Ruby code.

- Tom.

Report

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

Copied

LATEST

Here is a method that avoids working with the transform matrix in case that's desired.

1. Define x1 and y1 from the geometricBounds.

2. Change the textFrame.textRange.baselineShift by any positive amount (small is fine).

3. Define x2 and y2 from the geometricBounds (which are now shifted).

4. Define the angle using the math below.

5. Revert the change to baselineShift.

``0 - Math.atan( ( x1 - x2 ) / ( y1 - y2 ) ) * 180.0 / Math.PI``

This will return a value of 0 to 359 degrees, where 0 degrees is unrotated text.

It might be affected by the origin quadrant preference (whether y goes up/down as you move up/down the page). I haven't tested that interaction.