Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active December 30, 2023 22:14

AppleScript to JXA

2023-03-26 23:08:38

user:

tell application "Notes"
   repeat with eachNote in every note
      set noteTitle to the name of eachNote
      set noteBody to the body of eachNote
      set output to noteTitle & "\n" & noteBody & "\n"
      log output
   end repeat
end tell

Write that in JavaScript that talks to Apple automation instead


assistant: To write a script in JavaScript that talks to Apple automation, you can use the JavaScript for Automation (JXA) language. Here's the equivalent script using JXA:

const Notes = Application('Notes');
Notes.includeStandardAdditions = true;

const notes = Notes.notes();
const noteData = [];

notes.forEach((note) => {
  const noteTitle = note.name();
  const noteBody = note.body();
  const output = noteTitle + '\n' + noteBody + '\n';
  noteData.push(output);
});

noteData.forEach((data) => {
  console.log(data);
});

To run this script, save it as a .scpt file with a text editor and then run it using the Script Editor app on macOS or using the command line with osascript:

osascript -l JavaScript path/to/your/script.scpt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment