Git Product home page Git Product logo

jxa-lib's Introduction

Typings for JXA

This is a set of typings for AppleScript in JavaScript that works on macOS 10.10 and above.

This is a work in progress and is not yet stable.

Build

To run the examples, run webpack in the root of the project. Then Execute dist/main.js like so:

osascript -l JavaScript dist/examples/index.js

A prompt window will appear for the Finder application.

Library

In the lib directory, a helper library is available to make this easier to use in TypeScript (or ES6). It removes prefixes where possible to be more like Swift and removes long method names. Example:

import { FileManager } from "jxa-lib";

const fm = new FileManager();
let attr;
try {
  attr = fm.attributesOfItem("/some-file");
} catch (e) {
  // Instead of having to pass &error (Ref object) like in Objective-C,
  // an exception is thrown
  console.log("Maybe /some-file does not exist?");
}

// attr type is FileAttributes or undefined, which does not have prefixes removed
if (attr) {
  console.log(attr.NSFileGroupOwnerAccountID); // string
  console.log(attr.NSFileModificationDate); // Date object
}

Examples

See the examples directory.

Example with C functions

You do not have to use ObjC.import() because all modules will do this on their own.

import { exit, free, malloc } from "jxa-lib/stdlib";
import { memchr, memset } from "jxa-lib/string";

const size = 32;
const buf = malloc(size); // returns Ref<number>
memset(buf, 0, size);
for (let i = 0, c = "a".charCodeAt(0); i < size; i++, c++) {
  buf[i] = c;
}

const asciiC = "c".charCodeAt(0);
const asciiD = "d".charCodeAt(0);

// memchr() returns Ref<number> or Ref to NULL
const result = memchr(buf, asciiC, size);
if (result[0]) {
  // this will be asciiC or null/undefined
  console.log(result[0] === asciiC); // true
  console.log(result[1] === asciiD); // true

  // Getting result[30] or above is not defined behaviour
}

free(result);

// Do not print the result of the last expression
exit(0);

jxa-lib's People

Contributors

artoria2e5 avatar dependabot[bot] avatar pje avatar tatsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jxa-lib's Issues

Support CLI args?

Does your webpack configuration for jxa-lib allow keeping the run function global for passing in CLI arguments?

This library for example has a webpack config file + custom webpack plugin that allows for this: https://github.com/pahund/patricks-little-jxa-helpers

The entire module gets set to main and then the global run function gets defined as such: function run(args) { this.main(args); }.

nitpicks (or "draft types that i don't have time to check")

  • whose
  • ObjC C typing
  • Byte type guard

whose filter on Array

Apple has a whose filter for arrays. It is basically filter with an object predicate.

// documentation only gives string and num; we are guessing here
type JXAPrimitive = string | number | boolean | void
interface JXAWhosePredicateExpression {
  // equals, beginsWith...
}
type JXALogical= '_and' | '_or' | '_not'
interface JXAWhosePredicate {
  [logi: JXALogical]: JXAWhosePredicate[],
  [key: string]?: string | number | boolean | JXAWhosePredicateExpression
}
interface JXAArray<T> extends Array<T>, JXAReadonlyArray<T> {}
interface JXAReadonlyArray<T> extends ReadonlyArray<T> {
    /**
     * Returns the elements of an array that meet the condition specified in a filter predicate.
     */
  whose(predicate: JXAWhosePredicate, thisArg?: any): JXAArray<T>;
}

It is possible that they went forward and changed the entire Array.prototype. In that case just remove the extends BS.

ObjC FFI types

Currently bindFunction and registerSubclass do not have type signatures. Judging from the doc it should resemble:

type ObjCType = string | RefType
type ObjCSignature = [ObjCType, ObjCType []]
interface ObjCClass {
  name: string
  properties: {
    [key: string]: string
  }
  methods: (...x?: any[]) => any | {
    type: ObjCSignature
    implementation: (...x?: any[]) => any
  }
}
namespace ObjC {
  export function bindFunction(name: string, args: ObjCSignature);
  export function registerSubclass(class: ObjCClass);
}

Byte concept

Since Ref operates on bytes (uint8_t), it might be a good idea to define a concept of bytes somewhere so it becomes more clear. We will also need utility functions for reading/writing longer integer types and even floats.

Unfortunately the typing part is not something we can do perfectly right now; see microsoft/TypeScript#15480.

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.