Comments (14)
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.
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.
Good call, browserify is a nice interim solution for that. Hopefully we can figure out a more "official" way as well!
from jxa-cookbook.
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.
@Tatsh nice! Looking forward to trying your approach out. A "hacked" module system is much better than no module system ๐
from jxa-cookbook.
Added the require()
hack here: https://github.com/dtinth/JXA-Cookbook/wiki/Exotic-Recipes#including-other-files
from jxa-cookbook.
๐
Thank you very much for contributing! :)
from jxa-cookbook.
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
from jxa-cookbook.
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.
why can't require be imported? I'm finding things fail, but I don't get why?
from jxa-cookbook.
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.
@sprig definitely hit that wall... for 2 hours.
from jxa-cookbook.
@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.
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)
- Possible to intercept http link clicks from an app?? HOT 3
- better integrating Objective-C Bridge documentation + proposed expansions HOT 6
- JXA chat network HOT 1
- Problems with Mojave? HOT 20
- Can't import a custom framework HOT 4
- Using IconRef HOT 2
- How can I create a http request with JXA? HOT 5
- How to copy image or file to clipboard? HOT 15
- Integrating with JXA-Snippets example scripts HOT 1
- Object specifiers and Apple Music HOT 4
- How do you get a random number in JXA? HOT 2
- How to make FSEventStream Work HOT 4
- NSInvalidArgumentException: NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}' HOT 7
- Arrow functions now work in shebang files HOT 2
- Please state a license for use/reuse of the text
- How do I initialize a pointer to a specific C struct? HOT 3
- Mismatched method names in ObjC example
- writeToFileAtomicallyEncodingError has "getFileSystemRepresentation:maxLength"
- Add Missing Event Function `GURLGURL` (JXA-equivalent of `open location`)
- Library impoting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jxa-cookbook.