Skip to content

Injections API

Martin Kopischke edited this page Oct 12, 2020 · 2 revisions

Table of Contents

Array

Injections/Array.jxa:10-10

Array object extensions.

Meta

findLast

Injections/Array.jxa:37-51

Get the last array item for which a callback returns true.

Parameters

  • callback TestingCallback Function to execute on each value in the array.

  • thisArg any Object to use as this inside callback.

  • Throws TypeError When callback is not a function.

Returns any The value of the last element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

Meta

  • since: 2.0.0

findLastIndex

Injections/Array.jxa:66-80

Get the index of the last array item for which a callback returns true.

Parameters

  • callback TestingCallback Function to execute on each value in the array.

  • thisArg any Object to use as this inside callback.

  • Throws TypeError When callback is not a function.

Returns any The value of the last element in the array that satisfies the provided testing function. Otherwise, -1 is returned.

Meta

  • since: 2.0.0

takeWhile

Injections/Array.jxa:91-104

Call a callback on every array value while it returns true.

Parameters

  • callback TestingCallback Function to execute on each value in the array.

  • thisArg any Object to use as this inside callback.

  • Throws TypeError When callback is not a function.

compact

Injections/Array.jxa:114-128

Remove all undefined and null values from an array in-place.

Returns Array The compacted original array.

intersection

Injections/Array.jxa:138-144

Get the common item subset of two arrays.

Returns Array The common subset of values.

groupBy

Injections/Array.jxa:157-176

Group array items by the value returned by a callback.

Parameters

  • callback TestingCallback Function to execute on each value in the array.

  • thisArg any Object to use as this inside callback.

  • Throws TypeError When callback is not a function.

Returns object<any, Array> The grouped array items.

flatten

Injections/Array.jxa:186-198

Hoist sub-array items into the main Array in-place.

Returns Array The flattened original array.

unique

Injections/Array.jxa:220-235

De-duplicate Array contents. By default, this compares contents using Array.prototype.includes.

Parameters

  • callback DeduplicatingCallback Function to execute on each value in the array.

  • thisArg any Object to use as this inside callback.

  • Throws TypeError When callback is not a function.

Returns object<any, Array> The grouped array items.

TestingCallback

Injections/Array.jxa:37-51

Function that is called for every element of an array.

Type: Function

Parameters

  • element any The current element being processed in the array.
  • index number? The index of the current element being processed in the array.
  • array Array? The array the callback function was called upon.

DeduplicatingCallback

Injections/Array.jxa:220-235

Function to determine if an array element is unique.

Type: Function

Parameters

  • uniqueValues Array The current array of unique values.
  • element any The current element being processed in the source array.

Returns boolean Is the value unique?

Path

Injections/Path.jxa:16-16

Path object extensions.

Because JXA’s Path object is a missed opportunity as long as it is just a dumb equivalent to the AppleScript alias. This functionality is in the spirit of the Ruby Pathname library, i.e. Path object manipulation independently from the actual existence of the designated object in the file system.

Meta

toArray

Injections/Path.jxa:120-127

Split a Path into an array of part strings.

Returns Array<string>

toNSURL

Injections/Path.jxa:137-143

Convert a Path into an NSURL object.

Returns object The NSURL matching the path.

toJSON

Injections/Path.jxa:153-159

Convert a Path to its String representation when serialising to JSON.

Returns string

equals

Injections/Path.jxa:172-180

Check if the Path points to the same effective location as some other path. Paths are standardised before comparison, but not normalised (as that would exclude comparison of invalid paths).

Parameters

  • otherPath (string | Path) The path to compare locations with.

Returns boolean

exists

Injections/Path.jxa:191-198

Check if a Path points to a file system object.

Properties

  • Does boolean the path point to a file system object?

Meta

  • since: 5.0.0

existsAsVolume

Injections/Path.jxa:260-304

Check if a Path points to a volume.

Properties

  • Does boolean the path point to a volume?

Meta

  • since: 5.0.0

existsAsDirectory

Injections/Path.jxa:260-304

Check if a Path points to a directory.

Properties

  • Does boolean the path point to a directory?

Meta

  • since: 5.0.0

existsAsPackage

Injections/Path.jxa:260-304

Check if a Path points to a (macOS) package.

Properties

  • Does boolean the path point to a package?

Meta

  • since: 5.0.0

existsAsFile

Injections/Path.jxa:260-304

Check if a Path points to a simple file.

Properties

  • Does boolean the path point to a file?

Meta

  • since: 5.0.0

existsAsSymlink

Injections/Path.jxa:260-304

Check if a Path points to a symbolic link.

Properties

  • Does boolean the path point to a symlink?

Meta

  • since: 5.0.0

existsAsAlias

Injections/Path.jxa:260-304

Check if a Path points to a Finder alias.

Properties

  • Does boolean the path point to an alias?

Meta

  • since: 5.0.0

standardize

Injections/Path.jxa:315-322

Reduce empty and current Path parts and resolve parent path parts (where possible, see caveats in stringByStandardizingPath API reference).

Returns Path

resolve

Injections/Path.jxa:336-358

Resolve aliases and symlinks in a Path. Standardises the path before resolving; when neither alias nor symlink are part of the path, or the path is not matching a file system object, this is equivalent to .standardize

  • Throws Error When encountering an invalid Finder alias in the path.

Returns Path

tail

Injections/Path.jxa:368-374

Get the last part of a path.

Properties

shorten

Injections/Path.jxa:385-396

Strip a path of its last component(s).

Parameters

  • by number Number of path components to strip from Path end. (optional, default 1)

Returns Path

extend

Injections/Path.jxa:407-418

Append (a) path part(s) to a path.

Parameters

Returns Path

extension

Injections/Path.jxa:428-434

Get the extension of a Path.

Properties

stripExtension

Injections/Path.jxa:445-452

Strip the extension from a Path. Note this also strips a trailing slash from a directory path.

Returns Path

appendExtension

Injections/Path.jxa:463-470

Append an extension to a Path.

Parameters

  • extension string Extension to append to Path end.

Returns Path

insertSuffix

Injections/Path.jxa:496-507

Insert a suffix between the name and extension of a Path. Note this does not try to be smart in any way, so repeated uses will append multiple suffixes.

Parameters

  • suffix string The suffix to append to the base Path.
  • separator string The separator between base name and suffix. (optional, default '-')

Examples

var path = Path('/path/to/file.ext')
path = path.suffix('foo') // '/path/to/file-foo.ext'
path = path.suffix(3)     // '/path/to/file-3.ext'
path = path.suffix(3)     // '/path/to/file-3-3.ext'
// Find a unique number for a path copy, Finder-like:
var path = Path('/path/to/file.ext')
var index = 1
while (path.suffix(index).exists) index++
path = path.suffix(index)

Returns Path

Meta

  • since: 5.0.0

fromString

Injections/Path.jxa:32-37

Create a Path from a path string. Unlike Path(string), this expands the home directory indicator ~.

Parameters

Returns Path

fromArray

Injections/Path.jxa:48-53

Create a Path from an array of path part strings.

Parameters

Returns Path

fromFileURL

Injections/Path.jxa:64-73

Create a Path from a file:// URL string.

Parameters

Returns Path?

fromNSURL

Injections/Path.jxa:84-88

Create a Path from an NSURL Object.

Parameters

  • url object The NSURL to convert.

Returns Path?

fromBookmark

Injections/Path.jxa:100-110

Create a Path from an an NSData bookmark.

Parameters

  • data object The NSData bookmark.

  • Throws Error When resolving the bookmark fails.

Returns Path

String

Injections/String.jxa:10-10

String object extensions.

Meta

toQuotedForm

Injections/String.jxa:25-31

JXA implementation of AppleScript's quoted form of. Credits to http://stackoverflow.com/a/33050945/990363.

Returns string The quoted form of the string, suitable for shell usage.

toRegExpLiteral

Injections/String.jxa:42-48

Escape the String for usage as a literal in a RegExp. Credits to http://stackoverflow.com/a/6969486/990363.

Returns string The escaped string, suitable for usage as a RegExp literal.

toNSRange

Injections/String.jxa:65-71

Get an NSRange object matching the String.

Returns NSRange

toCapitalizedForm

Injections/String.jxa:87-93

Capitalise a string. To quote the NSString.capitalizedString documentation: A capitalised string is a string with the first character in each word changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values. A word is any sequence of characters delimited by spaces, tabs, or line terminators. Some common word delimiting punctuation isn't considered, so this may not generally produce the desired results for multiword strings.

Returns string

toLocaleCapitalizedForm

Injections/String.jxa:113-151

Capitalise a string, according to any locale-specific case mappings.

Parameters

  • locale (string | Array<string>)? The locale(s) to be used to convert to capitalised form. If multiple locales are given in an Array, the best available locale is used. The default locale is the user's current locale. Note locale comparison with available locales is strict, so make sure you use the canonical, properly cased form of the locale identifier (xx[-XX]).

  • Throws TypeError When a non-string locale argument is passed.

  • Throws RangeError When an invalid locale specifier is passed.

Returns string

transform

Injections/String.jxa:163-170

Transform a string.

Parameters

  • transform string The NSStringTransform to apply.
  • reverse boolean Apply the transform in reverse direction? (optional, default false)

Returns string

NSRange

Injections/String.jxa:65-71

An object that ObjC functions accept as an NSRange equivalent.

Type: object

Properties

  • location number The index in the parent string the range starts at.
  • length number The extent of the range (in UTF-16 characters).