Code Monkey home page Code Monkey logo

Comments (14)

danielbayley avatar danielbayley commented on May 28, 2024 2

Apple really should consider making this compete with Node, and still have all the OS X stuff, just as with their packaged Python.

They should just ship Node & npm with macOS instead of this crapโ€ฆ and just replace all the OS specific stuff with npm modules to cover it.

from jxa-cookbook.

dtinth avatar dtinth commented on May 28, 2024

First, thanks for that link! Added to examples.

Regarding require()ing stuff, I haven't found any official solution as well.

My solution is to use Browserify to package modules up into a single-file script, which makes the script portable. I've put it in the exotic recipes.

from jxa-cookbook.

af avatar af commented on May 28, 2024

Good call, browserify is a nice interim solution for that. Hopefully we can figure out a more "official" way as well!

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 28, 2024

This is what I currently use. A lot of scripts I write have boiler-plate in them, due to the lack of include/require/etc. requireHack is a hacky version of node's require().

Top of script

ObjC.import('Foundation');
var fm = $.NSFileManager.defaultManager;
var requireHack = function (path) {
    var contents = fm.contentsAtPath(path.toString()); // NSData
    contents = $.NSString.alloc.initWithDataEncoding(contents, $.NSUTF8StringEncoding);

    var module = {exports: {}};
    var exports = module.exports;
    eval(ObjC.unwrap(contents));

    return module.exports;
};

Rest of script:

h = requireHack('./test1.js');
console.log(h.helperFunc())

ObjC.import('stdlib');
$.exit(0);

test1.js contents:

exports.helperFunc = function () {
    return 'this is imported code';
};

So invocation of osascript -l JavaScript main.js output:

this is imported code

from jxa-cookbook.

af avatar af commented on May 28, 2024

@Tatsh nice! Looking forward to trying your approach out. A "hacked" module system is much better than no module system ๐Ÿ‘

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 28, 2024

Added the require() hack here: https://github.com/dtinth/JXA-Cookbook/wiki/Exotic-Recipes#including-other-files

from jxa-cookbook.

dtinth avatar dtinth commented on May 28, 2024

๐Ÿ‘

Thank you very much for contributing! :)

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 28, 2024

So, not so much for modules but here is what Apple's guide says for singles files:

You can use scripts as libraries by storing them in ~/Library/Script Libraries/.
If you have a script library "toolbox.scpt":

function log(message) {
   TextEdit = Application('TextEdit')
   doc = TextEdit.documents['Log.rtf']
   doc.text = message
}

a script can use the library like this:

toolbox = Library('toolbox')
toolbox.log('Hello world')

Note: Any code outside of functions in a library will be executed when the library is instantiated.

If you plan to distribute your script but it uses libraries, you could create an installer without any dependencies, error if the user already has ones installed with the same name (or auto-fix your script during install).

I still kind of prefer either making it one giant script with Browserify or using the require() emulation function. Arguably though require() could become:

require = (nameOrPath) ->
    try
        return Library(nameOrPath)
    catch
        # read and eval as before

https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/#//apple_ref/doc/uid/TP40014508-CH109-SW14

from jxa-cookbook.

sprig avatar sprig commented on May 28, 2024

Thanks for this tip!

Maybe it is obvious to everyone here, but it took me quite long to realise that 'require' itself can't be imported (e.g. via a 'Library' call), or else anything that one 'require's would be useless.

Just saying it in case someone hits this wall like I did. Incidentally, if anyone knows why this is the case, I'd be happy to know.

from jxa-cookbook.

joedaniels29 avatar joedaniels29 commented on May 28, 2024

why can't require be imported? I'm finding things fail, but I don't get why?

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 28, 2024

Apple really should consider making this compete with Node, and still have all the OS X stuff, just as with their packaged Python.

from jxa-cookbook.

joedaniels29 avatar joedaniels29 commented on May 28, 2024

@sprig definitely hit that wall... for 2 hours.

from jxa-cookbook.

sprig avatar sprig commented on May 28, 2024

@joedaniels29 frankly I'm not sure what I meant anymore. I think I meant that If you could import require, you could just as well import anything else without require

from jxa-cookbook.

yoodu avatar yoodu commented on May 28, 2024

I optimize the method of Emulating npm's require() to return the object by text, so it can place to the Library๏ผˆI found the function in Library script can't return a function which will be clear)

Library script: ~/Library/Script Libraries/jsUtil.scpt

function require_(path) {
  var handle = app.openForAccess(path)
  var contents = app.read(handle)
  app.closeAccess(path)
	
  const objText = `
	let module = { exports: {} }
	let exports = module.exports
	${contents}
	module.exports
	`
  return objText
}

Top of script

const { require_ } = Library('jsUtil')
const { test } = eval(require_('absolute path of util.js')) //use eval to get the object

test('jxa')

util.js

module.exports = {
  test: function (val)  {
    console.log('test', val)
    return val
  }
}

from jxa-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.