Repository navigation
Exotic Recipes
Various strange hacks and cool stuff.
As mentioned in Invoking JXA from Shell Scripts section, osascript will print the result of the last statement that is run. You can end your script with void 0 to force last statement to have undefined value. However, a blank line will be printed instead.
Sometimes this is unwanted. You can just tell the osascript process to exit(3).
ObjC.import("stdlib")
$.exit(0)osascript supports reading scripts from standard input. That means you can compile CoffeeScript and pipe it into osascript.
Here is an example script that renames selected files in Finder — changes @2x to _2x.
rename.coffee:
for file in Application('Finder').selection()
file.name = file.name().replace '@2x', '_2x'Running it:
coffee -cp rename.coffee | osascript -l JavaScriptLike CoffeeScript, using TypeScript is easily done by first compiling and then sending the output to osascript -l JavaScript.
jxa-lib is available to provide type information. In an editor like VS Code, this makes figuring out which properties to use much simpler, including what arguments are passed. Not everything is included yet, but the most basic to many advanced use cases are possible.
jxa-lib also contains a library. In compiled form with browserify, a lot of cruft like calls to ObjC.bindFunction can be avoided. malloc can be correctly after using a standard import:
import { malloc } from 'jxa-lib/stdlib';`An example of AppleScript to TypeScript:
tell application "iTunes"
if selection is {} then
display dialog "No tracks selected. Select some tracks with some
stale tags."
buttons {"I will!"}
default button 1 with icon 0
else
set sel to selection
repeat with myTrack in sel
if myTrack's class is file track then
tell myTrack
refresh myTrack
end tell
end if
end repeat
display dialog "Selected tags have been freshened up."
buttons {"Okay"}
default button 1
end if
end tellconst app = Application('iTunes') as ITunesApplication;
app.includeStandardAdditions = true;
if (!app.selection().length) {
const msg = 'No tracks selected. Select some tracks with some stale tags.';
app.displayDialog(msg, {
buttons: ['I will!'],
defaultButton: 1,
withIcon: 0
});
} else {
for (const track of app.selection()) {
if (track.class() === 'fileTrack') {
app.refresh(track as FileTrack);
}
}
app.displayDialog('Selected tags have been freshened up.', {
buttons: ['Okay'],
defaultButton: 1
});
}To build this, tsconfig.json must be made aware of jxa-lib like any other TypeScript project. Something like Browserify should be used to combine the code into one file.
The sections about importing other scripts have been moved to Importing Scripts.
- Foreword
- Conventions Used in This Cookbook
- Using JavaScript for Automation
- ES6 Features in JXA
- Getting the Application Instance
- User Interactions
- User Interaction with Files and Folders
- Using Objective-C (ObjC) with JXA
- Shell and CLI Interactions
- Importing Scripts
- iTunes
- Keynote
- Messages
- System Events
- Safari & Chrome
- Script Editor
- XML
- Examples