开发者

How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?

JSC seems like the simpler-and-easier, more portable, and quasi-universally-installed - obvious alternative to the node.js's of the world... I have been able to figure out the basics, but there is almost nada floating around out there about it (why?), so here's a simple problem I'm hoping someone can clarify..

I have a nice littlejavascript "class" in a .js file that starts out like this... BTW, it takes a Hex Color code and spits out a "named" color. Neat. Sample Usage:

<script type="text/javascript" src="ntc.js"></script>
<script type="text/javascript">
var n_match  = ntc.name("#6195ED");
    n_rgb        = n_match[0]; // This is the RGB value of the closest matching color
    n_name       = n_match[1]; // This is the text string for the name of the match
    n_exactmatch = n_match[2]; // True if exact color match, False if close-match
alert(n_match);
</script>

The code starts out with, and ends like so...

var ntc = {
     init: function() {
     var color, rg开发者_StackOverflowb, hsl;
  ⤹
}
ntc.init();

I am able, with little muss or fuss, to hardcode some values, at the BOTTOM of this document, like so...

var n_match = ntc.name("#000000");
print(n_match);

and run the code simply and easily from a terminal...

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -d ntc.js ↩

#000000,Black,true

However, for the life of me, I cannot figure out how to pass this sucker some variables..

Like, i just wanna get a callback from 204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020 but can't seem to twist its arm.. The -e option looked promising, but to no avail.

There are SOO many weird, niche tasks javascript has mutated to handle over the years, it would be great to hand them off to this guy.. The figurative trojan horse being that it's probably already installed, maybe even runnable - on potential client machines. As ubiquitous as this thing is though, the documentation is as sparse as the attendance at a Steve Ballmer fan-club meeting...

That said, lol, one of the only semi-useful snippets of info on this JSC was from a posting by an MS employee, from 7 years ago, that suggested... with the title "Commandline.js"

import System;
// This function reads the commandline arguments and prints them
function PrintCommandLineArguments() {
    var args:String[] = System.Environment.GetCommandLineArgs();
    var iValue:int;
// Print the command-line arguments    
for (iValue in args)
    Console.WriteLine(args[iValue]);
}
PrintCommandLineArguments(); 

I couldn't get that to work, but there has to be a way, smarty pants'... Oh and frankly, this just adds to my confusion over the frothing at the mouth that has taken hold of all the server-side JS people of late, as this thing is decidedly old news... Why was this runtime environment poo-pooed in favor of the current hot-topic solutions, anyways? Does JSC suck? Clue me in, sister girlfriends. ∀Ⓛ∃✖


The following seems to work:

# using Bash on Mac OS X 10.6.7
sudo ln -is /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin

# simple example to print passed arguments
jsc --help   # Usage: jsc [options] [files] [-- arguments]
jsc <(echo 'print(arguments[0]); print(arguments);') -- one two three


# http://chir.ag/projects/ntc/
curl -L -O http://chir.ag/projects/ntc/ntc.js
echo '
for(var i in arguments) {
   var n_match = ntc.name(arguments[i]);
   print(n_match);
}
' >> ntc.js

jsc ntc.js -- 204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 \
              404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 \
              a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020


# For more information on using javascript from the command line see, for example: 
# - http://www.phpied.com/javascript-shell-scripting/
# - http://littlecomputerscientist.wordpress.com/2008/12/19/command-line-scripting-with-javascript/
# - http://littlecomputerscientist.wordpress.com/2008/12/20/improving-spidermonkeys-load-for-command-line-javascript/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜