Exotic Recipes

Thai Pangsakulyanont edited this page Jan 5, 2015 · 11 revisions

Various strange hacks and cool stuff.

Suppressing osascript's Last Statement Result

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)

Using CoffeeScript

osascript supports reading scripts from standard input. That means you can compile CoffeeScript and pipe it into osascript.

Example

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 JavaScript

The sections about importing other scripts have been moved to Importing Scripts.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.