JavaScript

JavaScript For Automation: A Guide For Beginners

JavaScript For Automation: A Guide For Beginners. If you ever did a script on the Mac, you definitely saw AppleScript, here’s a guide.

AppleScript was the de facto standard for shared meshing and scripting user interfaces.
There is a second user interface scripting language: JavaScript for Automation (JXA), as of OS X 10.10.


In this guide, you can find some real-world examples of this alternative to AppleScript.
JXA RacerA JXA script is running in several ways. The default way to execute a JXA script is via the operating system Script Editor.


Ads by Digital Commerce



JavaScript (JXA) Script Editor Package

The JavaScript Interpreter is accessible from the top-left list box at the stop button, which is set to JavaScript rather than AppleScript. Add the following lines to navigate the latest scripting application:

  1. //
  2. Receive and customize the latest framework
  3. for prose. // for scripting.
  4. //
  5. var app = Application.currentAnwend();
  6. App.includeSnapAdd = true;
    The first line will get the JXA object for the code-running function. On the second side, you can add dialog boxes and other goodies to the piece.

Now fill in the script:

  1. //
  2. Give the customer a message.
  3. //
  4. App.displayAlert(“The message is that”);

Example of Message 

When you do that the message dialog with “This is a message” will be shown.

Add the following if you want to select from a list of items:

  1. //
  2. Let the user choose a list.
  3. //
  4. { withProprompt: “Which language do you like?” }]; var lang = app.chooseFromList([‘JavaScript’, ‘AppleScript.’
  5. Notification app.display(lang, { with Title: ‘Language Chooser,’ under title: ‘Your preference is…’ });
    Then select From List function that includes a string array and an optional configurable object. You can customize the text shown above the string list via the Prompt configuration option. We return the preferred string.

They will display a note with the data presented to the user using the Notifications view function. Moreover, with the configuration object, you will specify the name and subtitle of the notification.

Case of Picker List

By clicking the run button, you choose in the dialogue, the right-pointing triangle in this toolbar.
Displayed Message

When you pick one such as JXA, the notification pop-up shows your option. The outcome returned is the boolean fake, if the user selects cancel in the dialog.

Use the displayDialog() feature to get input from the user. For this functionality you have to name it in an attempting…catch structure so the user will throw an error if he pushes a cancel button. Form of code as follows:

  1. //
  2. Get information typed by the customer.
  3. //
  4. Please try {
    { age of a var = app.displayDialog(‘How old are you?’),
    App.displayReturned + years old!, { title ‘How old are you?’ }); { Title:
  5. Catch(s) { }
    App.displayNotification(‘User Cancelated,’ });

Alfred’s JXA

The Quick Edit JXA.alfred workflow Alfred 3 workflow is available for download in the zip format.

This workflow helps the user to copy a selection to a single program and paste the edits into another program beforehand.

This workflow is really helpful and saves a lot of time. My favorite editor is TextWell to render these sudden changes.

Click to rate this post!
[Total: 0 Average: 0]


Ads by Digital Commerce

Scroll to Top