Edit
Angles are taken as radians for the cos function of Satimage.Osax, ( in stead of degrees), the dictionary describes how to perform the conversion.
Hopes this helps
Angle in radians = ((pi * angle in degrees) / 180 )
When I was this far, I just wrote them out for you, you have to test them, as I haven’t time at the moment!
to getLumen from lux by surfArea
if surfArea is 0 then error
return lux * surfArea
end getLumen
to convertToCandela from lumen by apexAngleInDegrees
local circleInRads, halfApexAngleInRads
if apexAngleInDegrees = 0 then set apexAngleInDegrees to 360
set circleInRads to 2 * pi
set halfApexAngleInRads to ((pi * apexAngleInDegrees) / 180) * 0.5
return lumen / (circleInRads * (1 - (cos (halfApexAngleInRads))))
end convertToCandela
to convertToLumen from candela by apexAngleInDegrees
local circleInRads, halfApexAngleInRads
if apexAngleInDegrees = 0 then set apexAngleInDegrees to 360
set circleInRads to 2 * pi
set halfApexAngleInRads to ((pi * apexAngleInDegrees) / 180) * 0.5
return ((circleInRads * candela) * (1 - (cos (halfApexAngleInRads))))
end convertToLumen
Yes Bazzie Wazzie, awk is a handy tool, but if you use these functions just alittle, the overhead by the do shell script soon becomes noticeable.
I know, 200 calculations a second, but was just to show that you won’t need some extra software installed. TS said ‘applescript or shell (i don’t like plugins)’, not everybody wants to install all kind of OSAX on their machine (me included). And when you put this code behind a dialog or something the speed difference isn’t noticeable.
But the OP can save the script containing the bundle, and stuff Satimage.osax into the Resources/Scripting Additions folder, or use your solution.
to _cos for angleInDegrees
local _pi
set _pi to pi as text
set _pi to "3." & text 3 thru -1 of _pi as text
return (do shell script "awk 'BEGIN {print cos((" & angleInDegrees & "*" & _pi & ")/180) }'")
end _cos
to __cos for angleInRads
return (do shell script "awk 'BEGIN {print cos(" & angleInRads & ") }'")
end __cos
Where do you get a bytcoded awk by the way, I am curious to try it!
Well that’s one option. You could also use ASObjC Runner:
tell application "ASObjC Runner" to set x to do trig on (45 / 180 * pi) using function "cos"
Or vanilla AppleScript:
on getCosine(x)
--convert from degrees to radians
set x to x * (2 * pi) / 360
-- cosine = π/2 - sine, so...
set x to pi / 2 - x
set answer to 0
set numerator to x
set denominator to 1
set factor to -(x ^ 2)
repeat with i from 3 to 40 by 2
set answer to answer + numerator / denominator
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat
return answer
end getCosine
I haven’t got that far in the dictionary of AsObjC Runner yet Shane!
AsObjC Runner isn’t a plugin! It is a faceless app, that I like!
I like your solution by using the Taylor (or is it McLaurin ) series expansion? but hopefully the tables are precomputed in AsObjC Runner, awk and Satimage.Osax!
set angle to 60 -- in degree
set lumen to 0.11 --125 candela
--lumen to candela
set cmd to "awk 'BEGIN{calc= (2*pi) * (1- (cos (0.5 * (" & angle & "*degree))))}'"
set lum_cand to (do shell script cmd) as real
if lum_cand ≠0 then set lum_cand to 0.11 / lum_cand
but my calculation might not be correct.
Anyway, my problem is solved.
This is a little better. pi is not a constant in awk.
set angle to 60 -- in degree
set lumen to 0.11 --125 candela
set angle to (((pi * angle) / 180) * 0.5) as text
set ofs to offset of "," in angle
set angle to text 1 thru (ofs - 1) of angle & "." & text (ofs + 1) thru -1 of angle
--lumen to candela
set cmd to "awk 'BEGIN{print (1- (cos (" & angle & ")))}'"
set lum_cand to (do shell script cmd)
set lum_cand to 2 * pi * lum_cand
if lum_cand ≠0 then set theCand to lumen / lum_cand
theCand
”> 0,13
Well, my awk version returns 0.00045 less than my applescript version. Given the magnitude of the numbers (148.493135931979 and 148.493583473672) I find that to be perfectly acceptable!
I copied the code from the post, when I run it, it returns 148.493135931979
Here is the code I used:
set angle to 60 -- in degree
set lumen to 125 --125 candela
set angle to (((pi * angle) / 180) * 0.5) as text
set ofs to offset of "," in angle
set angle to text 1 thru (ofs - 1) of angle & "." & text (ofs + 1) thru -1 of angle
--lumen to candela
set cmd to "awk 'BEGIN{print (1- (cos (" & angle & ")))}'"
set lum_cand to (do shell script cmd)
set lum_cand to 2 * pi * lum_cand
if lum_cand ≠0 then set theCand to lumen / lum_cand
theCand
--> 0,13
I’d also like to add that it differs 0,036 from the AS version when given 10000 lumen
Be aware that coercing floating point numbers to text have different results depending on the current decimal separator. Your code assumes the european comma separator, on an US system you get the proper shell compatible number with period separator
Haven’t payed much attention but numbers needs to have always a comma when system language is Dutch. the string “0.133975” return by the shell will be coerced to integer 133975.
Safest way from a string to a number is using
set stringNumber to "0.133975"
set num to run script stringNumber
So when I put run script before the do shell script I get the expected results.
--coerce between numbers and string without considering localization
set x to "1.12"
set x to run script x --coerce to a number
set x to do shell script "osascript -e '\"" & x & "\" as number'" --coerce to a string; dirty but does the job