JXA - Classes, Subclasses / Libraries, Sub-Libraries. Is it possible?

Scratch that. I think I’m misunderstanding the mechanism :slight_smile:

Hey ComplexPoint,

Thanks for the attempt! I did think about npm and it would definitely make a lot of things easier, however I am not only making this library for me. I would prefer it in native apple Scripting Languages since then others will be able to use it easily without need for installing dependencies.

JXA/Javascript is fantastic for this as it is very close to the programming languages I am used to, and is native on all apple machines Yosemite+. If I have to stick to the “boiler-plate” method, I will do just that :slight_smile:

With regards to how a library works, I am under the impression that when JXA launches a library it actually runs it as it’s own stand-alone application/process running in ObjC JavascriptCore. When you instantiate a library, what you actually get in the original script appears to be an [Object Specifier] pointing to the application running in ObjC JSCore. This is how I’ve interpreted the documentation / errors I have received anyway.

1 Like

I have a technique I used previously, but up until now I haven’t adapted it for more general use.

The technique is basically an “include” feature, where I can put special comments in a JXA script to say “include FileUtils.scpt”, or whatever. Then I run a utility, and it copies the indicated file(s) into the script.

What’s the use of that?, you may ask. The use is, I can run the utility anytime I change one of the “include” files, and it updates the script to have the latest version in it. So when I fix a bug, I can easily update all scripts that use it.

I hope that makes sense. Anyway, I think I’ll update this to be a more general-purpose utility, and start using it. Let me know if it interests you at all. It’s cool if it doesn’t - it’s not the perfect solution.

That's likely the best way to go indeed. Even more ideal if it were part of a script editor (perhaps even written itself in JXA). Alternatively written as a plugin for atom editor...

Ooh, I like what you're thinking. When will you get it done? :wink: I'll beta-test it!

I barely have any time for such a project at the moment, but will look into it at some point I am sure!

Had a brief look around and it appears that to execute a shell script from a file you can use the following function:

const exec = require('child_process').exec;
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

or

const execFile = require('child_process').execFile;
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});

See this

To execute applescript/JXA from atom commands such as this can be used:

var cmd = "osascript -e 'set x to "a"\nsay x'"

Not entirely sure how well this will work with more complex scripts, like those requiring subclassing for instance… However it would likely be better to compile these to .app… I’ll see if I get time this weekend to look into it!

Not sure I entirely followed that, but go for it! :slight_smile:

Regarding the class issue, i just asked Sal Soghoian whether it was possible. The response i got was:

Ah I see. Alas, the class support you seek is available in libraries. It was planned to be added, but Apple decided to focus on other things instead.

I interpret this as:

  • Classes are available in libraries
  • Sub-Libraries were planned to be added, but Apple decided to focus on other things.

That's just my interpratation though. My original message that motivated this response:

I know very well the problem and I am fairly fine with it, since many windows applications are the same - I'm fairly used to having to resort to UI scripting.

My main issue is I can't figure out how to make a JavaScript class equivalent in a JXA library... There must be a way but I have no idea how?

1 Like

Hi all,

I haven’t made this a package yet, and neither have I got this auto-running the OSAScript on “compile”, however I have created the following code.

See it in action:

It produces a file in the same directory called e.g. “myscript_transpiled.scpt”. This script can then be officially compiled with osascript compile process. This is tomorrow’s job! :stuck_out_tongue:

1 Like