Re: Preventing extraneous output from osascript -l JavaScript
Re: Preventing extraneous output from osascript -l JavaScript
- Subject: Re: Preventing extraneous output from osascript -l JavaScript
- From: Axel Luttgens <email@hidden>
- Date: Wed, 16 Dec 2015 23:12:01 +0100
> Le 15 déc. 2015 à 22:30, Paul Scott a écrit :
>
> When I run the following script from Terminal, I get “true” printed on the console. I don’t want the that extraneous output.
>
> #!/usr/bin/osascript -l JavaScript
> var app = Application.currentApplication();
> app.includeStandardAdditions = true;
> [
> { name: "item1", value: 1 },
> { end: true }
> ]
> .some(function(o){
> if ( o.name ) console.log(app.doShellScript('/bin/echo "' + o.name + '" "' + o.value + '"'))
> else return true;
> });
>
> Note that if I wrap everything in a function then I no longer get the “true” printed, but I still get an extraneous linefeed, and I don’t want that either.
>
> #!/usr/bin/osascript -l JavaScript
> var app = Application.currentApplication();
> app.includeStandardAdditions = true;
> (function(){
> [
> { name: "item1", value: 1 },
> { end: true }
> ]
> .some(function(o){
> if ( o.name ) console.log(app.doShellScript('/bin/echo "' + o.name + '" "' + o.value + '"'))
> else return true;
> });
> })();
>
> I can easily prevent any extraneous output in AppleScript by placing “return” as the last statement. But there does not seem to be any way to prevent it in JavaScript. Any ideas?
Hello Paul,
I you allow, your example appears somewhat convoluted. For example, why should your anonymous function return nothing in one case, true in the other case? And who would consume that returned value, if any? Could you tell us a bit more about your goal?
Anyway, it seems that the "problem" may be reduced to:
#!/usr/bin/osascript -l JavaScript
var app = Application.currentApplication();
app.includeStandardAdditions = true;
Assuming that above script is saved as paul.sh in the current working directory, this is what I get here:
$ ./paul.sh
2015-12-16 22:28:22.801 paul.sh[12839:6641764] warning: failed to get scripting definition from /usr/bin/osascript; it may not be scriptable.
true
$
I don’t know why that warning; it seems it affects some people, not others (I’m here with Yosemite; if someone had a clue…).
Anyway, it seems that osascript tends to write to stdout the result of the script’s last executed statement.
So, lets add an additional statement to the script:
#!/usr/bin/osascript -l JavaScript
var app = Application.currentApplication();
app.includeStandardAdditions = true;
void(0);
and run it:
$ ./paul.sh
2015-12-16 22:43:04.850 paul.sh[12880:6646437] warning: failed to get scripting definition from /usr/bin/osascript; it may not be scriptable.
$
One thus now gets that empty line written to stdout (would be logged as "undefined" in SE).
Currently, there is only one way I could devise: abruptly stop the script’s execution, so that osascript has nothing to crunch and output, something like an uncaught exception:
#!/usr/bin/osascript -l JavaScript
var app = Application.currentApplication();
app.includeStandardAdditions = true;
throw new Error();
Let’s try:
$ ./paul.sh
2015-12-16 22:50:37.626 paul.sh[12897:6647984] warning: failed to get scripting definition from /usr/bin/osascript; it may not be scriptable.
./paul.sh:81:99: execution error: Error on line 3: Error (0)
$
Of course, all those error messages are written to stderr; let’s try again by dropping those outputs:
$ ./paul.sh 2>/dev/null
$
No more extraneous output written to stdout.
That said, all of the above may appear as trying to work against the available tools
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden