Evaluate External JavaScript Files in Yosemite

In my recent post https://lawlessguy.wordpress.com/2014/10/22/javascript-libraries-in-yosemite/, I lamented about the way the Library() function behaves.  I put together a crude hack that helped to achieve something very close to what I had envisioned, but the approach is just too messy.

Tonight, I devised a simple way for an automation script to evaluate an externalized JavaScript file.  Please consider the external file tmp.js:

ObjC.import ('stdio');
$.puts("In the script");

This is the file we will dynamically load and evaluate.  Here’s the function and sample usage code that provides these capabilities:

Screen Shot 2014-10-24 at 10.32.45 PM

If we save the above script as myeval.scpt, we can then invoke it from the command-line using osascript:

Screen Shot 2014-10-24 at 10.35.30 PM

This technique could then be used to load/eval() the external library I had developed in my previous post.  I found that the filename would only work with an relative path if I was running from osascript.

I found that if the file didn’t load, the returned string “undefined” would appear in the returned value.

Note the use of ObjC.unwrap() to convert the returned NSString to a JavaScript string.

Here’s my new version of the jimslib library.  It is contained in a file called jimslib.js:

// Copright © 2014 by James K. Lawless
// http://jiml.us
// MIT / X11 open source license.  Please see 
// http://www.mailsend-online.com/license2014.php

// This file is Jim's catch-all base JS library for
// personal use with OS/X.
__g=new Object();
__g.app=Application.currentApplication();
__g.app.includeStandardAdditions=true;

msg=function(x,y) {
   if(typeof(x)!="string") {
      x=x.toString();
   }
   if(y==null) { 
      return __g.app.displayDialog(x);
   }
   else {
      return __g.app.displayDialog(x,y); 
   }
}

msg("loaded!");

The test program for the above looks like this:

// Copright © 2014 by James K. Lawless
// http://jiml.us
// MIT / X11 open source license.  Please see 
// http://www.mailsend-online.com/license2014.php

ObjC.import('Cocoa');
function fileEval(filename) {
   var tmp=$.NSString.stringWithContentsOfFile(filename);
   var code=ObjC.unwrap(tmp);
   if(code=="undefined") {
      return false;
   }
   eval(code);
   return true;
}

if(fileEval("/Users/jimlawless/Progs/jimslib.js")) {
   msg("Testing");
   msg(2);
   msg(__g);
}

 

The output is:

Screen Shot 2014-10-25 at 9.59.28 AM

Screen Shot 2014-10-25 at 9.59.36 AM

Screen Shot 2014-10-25 at 9.59.42 AM

Screen Shot 2014-10-25 at 9.59.48 AM

While there’s still some boilerplate code involved, I like this approach better than the Library() hack.

Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming, Uncategorized and tagged , . Bookmark the permalink.

2 Responses to Evaluate External JavaScript Files in Yosemite

  1. jakewolpert says:

    Jim

    I set up a wordpress to share some of the work I did. https://jejaju.wordpress.com .

    I have a semi-functional CGI with GET parameters. I return text to audio.

    I simplified your file reader function down to
    function read(fn){
    return $.NSString.stringWithContentsOfFile(fn).js
    }

    Thanks for your posts. Keep them coming!

    Jake

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s