Modifying a Javascript to call as a function from Applescript

Feel free to tell me go find a Javascript forum, as I suspect this question is more Javascript than Applescript.

I’m trying to make use of this Creative Commons code for finding a color name from a color value:

Project: http://chir.ag/projects/ntc/
Code: http://chir.ag/projects/ntc/ntc.js

I found Shane’s post here detailing how to call a Javascript handler from an Applescript:
http://forum.latenightsw.com/t/is-it-possible-to-call-javascript-from-applescript/291

But I keep messing around with trying to do it using that Javascript, and I just can’t figure out how to turn it into a function and pass a value in. I just need to modify (if it even needs modification?) the Javascript from the version as provided at the link, to where a version of Shane’s:

my callFunction:"doIt" withArgs:{2, 2} inJSLib:myJSLib

will yield something other than my current

Any help? I’m assuming this is super quick/easy for anyone who actually knows Javascript.

Thanks in advance for any help,

Tom.

That error suggests the library isn’t being loaded.

Any reason for not just re-writing the stuff in AppleScript? It’s not like there’s a lot of code.

My main reason for not rewriting it in Applescript is that I don’t even know Javascript well enough to read it easily to understand what it is I’m writing. I agree, if I had version of this written as an outline of what it does in English, I could probably Applescript it in not very long. I’ll take another look and see how much sense I can make out of it.

Thanks,

Tom.

Also, while rewriting it would probably be easy if I actually understood the Javascript… I figure if someone gave me some Applescript code and said to turn it into a handler, that’s like a 30-second procedure. I assumed it would be the same case with Javascript for someone who knew what they were doing, but I’m flying blind here.

I may be able to find a friend who knows Javascript who can help.

As far as it not loading, I have the script here:

 set jsLibPath to "/Volumes/Hackintosh HD/Users/work/Downloads/ntc.js"

and I wasn’t sure of the extension, I also tried .scptd, but I get the same error either way.

First, the library loading code I posted requires that the library be in one of the recognised Script Libraries folders, not just anywhere.

To rewrite as AppleScript, you need to put the data in as records as a pair of name/value lists. The rest is really just maths, although you could use NSColor to build the HSL equivalents. The principle is to check for a matching RGB value, and if not see how close you get to one of the entries. The closeness is calculated like this, assuming r, g, b, h, s and l are the value for the new color, and the _2 equivalents are for one of the colors in the list:

set ndf1 to (r - r_2)^2 + (g - g_2)^2 + (b - b_2)^2
set ndf2 to (h - h_2)^2 + (s - s_2)^2 + (l - l_2)^2
set ndf to ndf1 + ndf2 * 2

The color with the lowest value for ndf is the nearest.

For the HSL, you could use something like this ASObjC code:

set theColor to current application's NSColor's colorWithRed:r green:g blue:b alpha:1.0
set h to theColor's hueComponent()
set s to theColor's saturationComponent()
set l to theColor's brightnessComponent()

You might want to use one of the other color spaces.

Sorry Shane, that was completely lazy of me to paste in that line of code with the “Downloads” path and not notice I’d grabbed the wrong line. I had first tried running it from “anywhere,” and didn’t realize I still had that script open and copied and pasted from it. However, before I posted, I did put it at /Library/Script Libraries/nts.js and also at /Library/Script Libraries/nts.scptd and tried calling it from both paths, but got the same error. I’m pretty sure that I’m getting that error not because the library isn’t loading, but because I don’t have a handler with that name properly defined within it, because I can’t figure out the javascript.

Thanks so much for the decoding of the Javascript math, I’ll take a look at it tomorrow.

  • Tom.

Your error says you’re calling the handler on missing value – that means the library variable contains missing value.

Anyway, this might be useful:

use framework "Foundation"
use framework "AppKit"
use scripting additions

my getRGBandHSL:"FF02FF"
on getRGBandHSL:colorString
	set {theResult, theRed} to (current application's NSScanner's scannerWithString:(text -6 thru -5 of colorString))'s scanHexInt:(reference)
	set {theResult, theGreen} to (current application's NSScanner's scannerWithString:(text -4 thru -3 of colorString))'s scanHexInt:(reference)
	set {theResult, theBlue} to (current application's NSScanner's scannerWithString:(text -2 thru -1 of colorString))'s scanHexInt:(reference)
	set theColor to current application's NSColor's colorWithRed:theRed / 255 green:theGreen / 255 blue:theBlue / 255 alpha:1.0
	set theHue to theColor's hueComponent()
	set theSat to theColor's saturationComponent()
	set theLight to theColor's brightnessComponent()
	return {{theRed, theGreen, theBlue}, {theHue, theSat, theLight}}
end getRGBandHSL: