NSShadow Part 2

By now reading and writing RTF with shadow styles works fine.
But building a “standard input” in Xcode/IB is complicated for the needed values Shadow Angle, (Shadow Distance) to/from width and height.
Since there a no trigometric functions in AppleScript I have to use something else.
Any ideas how to do that rather than searching a list of pre-computed values?

You can use the JavaScriptCore framework and the Math library:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions
property theMathObject : missing value

doJsMath("cos", 1)
doJsMath("hypot", {3, 4, 5})

on doJsMath(theMethod, argsList)
	if class of argsList is not list then set argsList to {argsList}
	return (mathObject()'s invokeMethod:theMethod withArguments:argsList)'s toDouble()
end doJsMath

on mathObject()
	if theMathObject = missing value then
		set theContext to current application's JSContext's new()
		log theContext's |name|() as string
		set theMathObject to theContext's objectForKeyedSubscript:"Math"
	end if
	return theMathObject
end mathObject

Many thanks Shane.

Works perfect though I don’t understand the “hypot” arguments - maybe was a typing error.

Below the completed script in case somebody is faced the same problem.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions

property theMathObject : missing value
property radM : pi / 180 -- the trigonometric functions use Radians not Degrees

# measuring of angles thru apps are not consistent
# in regards where on a circle the angle 0 is
# I prefer to set it to 12 o'clock and measure clockwise
# (even though it might make more sense to set it to 3 o'clock in regards of shadows)
# starting at 12 also matches better this really ugly circular slider which nowhere works as usable
# even in Apple's flagship app FCP its unusable.
# since angle functions are finally only defined for 0 - 90 or 90-(-90)
# the full circle needs to be segmented into 4 sections: s1: 0 - 90, s2: >90 - 180, s3: >180 - 270, s4: >270 - 360
# the shadow object value keys are width and height
# in s1 width and height are positiv
# in s2 width is positiv and height is negativ
# in s3 width is negativ and height is negativ
# in s4 width is negativ and height is positiv
#       360/0
#     - + | + +
# 270 ––––|––––– 90
#     – – | + –
#        180

-- example calc from shadow data
set shadData to {width:3.2, height:-5.539}
set xOff to shadData's width
set yOff to shadData's height

set shadDist to doJsMath("hypot", {xOff, yOff})

if xOff > 0 and yOff > 0 then
	set shadRad to doJsMath("asin", {xOff / shadDist})
	set angleAdd to 0
	
else if xOff > 0 and yOff < 0 then
	set shadRad to doJsMath("asin", {-yOff / shadDist})
	set angleAdd to 90
	
else if xOff < 0 and yOff < 0 then
	set shadRad to doJsMath("asin", {-xOff / shadDist})
	set angleAdd to 180
	
else if xOff < 0 and yOff > 0 then
	set shadRad to doJsMath("asin", {yOff / shadDist})
	set angleAdd to 270
end if
set shadAngle to (angleAdd + shadRad / radM)
log {shadAngle:shadAngle, shadDist:shadDist}

-- example from interface
set shadAngle to 315
set shadDist to 8

# using the standard circle (radius = 1)
set xOff to doJsMath("sin", (shadAngle * radM))
set yOff to doJsMath("cos", (shadAngle * radM))

set xOff to xOff * shadDist
set yOff to yOff * shadDist
log {width:xOff, height:yOff}



####################################################################
on doJsMath(theMethod, argsList)
	if class of argsList is not list then set argsList to {argsList}
	return (mathObject()'s invokeMethod:theMethod withArguments:argsList)'s toDouble()
end doJsMath

on mathObject()
	if theMathObject = missing value then
		set theContext to current application's JSContext's new()
		log theContext's |name|() as string
		set theMathObject to theContext's objectForKeyedSubscript:"Math"
	end if
	return theMathObject
end mathObject

One more question.
When I want to use the code in my app I assume I’ve to add the framework to the XCode project.
When I try to do it tells me that the framework is 10.15.
So I’m a bit confused - especially I’m still on 10.14

Although it’s aimed at calculating the hypotenuse, it can take a variable number of arguments and returns the square root of the sum of the squares.

Right.

You’re probably running the latest Xcode. IAC, it shouldn’t matter.

Many Thanks again!

How far would it be backwards compatible

JavaScriptCore was introduced in 10.5. A long time ago…