Using the `osascript` Command with Examples

Using the `osascript` Command with Examples

  • Osx
  • November 5, 2023

The osascript command in macOS allows users to run AppleScript or JavaScript for Automation (JXA) code directly from the command line. This provides a convenient way to automate tasks, control applications, and interact with system features programmatically. In this article, we will explore various use cases of the osascript command and provide code examples for each.

Example 1: Run an AppleScript command

The first use case demonstrates how to run a single AppleScript command using the osascript command. The -e option allows us to specify the AppleScript code as a command-line argument.

osascript -e "say 'Hello world'"

Motivation: This example can be useful for creating spoken alerts or notifications on MacOS. By running the say command with a specific string, we can make our system speak the desired message.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • -e: Specifies that the next argument is an AppleScript command.
  • "say 'Hello world'": The AppleScript command to speak the phrase “Hello world”.

Example Output: The system will produce a spoken output of the phrase “Hello world”.

Example 2: Run multiple AppleScript commands

In this example, we demonstrate how to execute multiple AppleScript commands sequentially using the osascript command. By chaining multiple -e options, we can provide multiple AppleScript statements within a single command.

osascript -e "say 'Hello'" -e "say 'world'"

Motivation: This use case is useful for executing multiple related AppleScript commands one after another. It allows us to perform multiple automated actions without having to run separate osascript commands for each statement.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • -e: Specifies that the next argument is an AppleScript command.
  • "say 'Hello'": The AppleScript command to speak the word “Hello”.
  • "say 'world'": The AppleScript command to speak the word “world”.

Example Output: The system will produce spoken outputs of both “Hello” and “world” one after another.

Example 3: Run a compiled, bundled, or plaintext AppleScript file

In this example, we demonstrate how to run an AppleScript file with the osascript command. The AppleScript file can be either compiled (*.scpt), bundled (*.scptd), or plaintext (*.applescript).

osascript path/to/apple.scpt

Motivation: This use case is particularly useful when we have complex and lengthy AppleScript code that would be difficult to manage as command-line arguments. By saving the AppleScript code in a separate file, we can easily execute and manage it using the osascript command.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • path/to/apple.scpt: The path to the AppleScript file to be executed.

Example Output: The AppleScript code in the specified file will be executed, and the output depends on the content and logic of the script.

Example 4: Get the bundle identifier of an application

This example demonstrates how to retrieve the bundle identifier of a specific application using the osascript command. The bundle identifier can be used for various purposes, such as launching an application using the open -b command.

osascript -e 'id of app "Application"'

Motivation: Obtaining the bundle identifier of an application is useful when we need to programmatically interact with that application or perform actions specific to it. It can be used in scripting scenarios where application-specific commands need to be executed.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • -e: Specifies that the next argument is an AppleScript command.
  • 'id of app "Application"': The AppleScript command to retrieve the bundle identifier of the specified application, where “Application” should be replaced with the name of the desired application.

Example Output: The command will output the bundle identifier string for the specified application.

Example 5: Run a JavaScript command

In this example, we demonstrate how to use the osascript command to run a JavaScript command using the -l JavaScript option. This allows us to execute JavaScript code instead of AppleScript.

osascript -l JavaScript -e "console.log('Hello world');"

Motivation: This example is useful when we want to leverage JavaScript instead of AppleScript for automation tasks. JavaScript is often preferred by developers who are more familiar with the JavaScript programming language and its ecosystem.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • -l JavaScript: Specifies that the code following this option is JavaScript.
  • -e: Specifies that the next argument is a JavaScript command.
  • "console.log('Hello world');": The JavaScript command to log the message “Hello world” to the console.

Example Output: The specified JavaScript code will be executed, and the output will be the logged message “Hello world” in the console.

Example 6: Run a JavaScript file

Lastly, we demonstrate how to execute an external JavaScript file using the osascript command. This allows us to keep complex JavaScript code in separate files and execute them through the command line.

osascript -l JavaScript path/to/script.js

Motivation: Keeping JavaScript code in separate files provides better code organization and reusability. By using the osascript command to execute external JavaScript files, we can separate code logic from command-line arguments and easily modify or update the script.

Explanation:

  • osascript: The command to run AppleScript or JXA.
  • -l JavaScript: Specifies that the code following this option is JavaScript.
  • path/to/script.js: The path to the JavaScript file to be executed.

Example Output: The specified JavaScript file will be executed, and the output depends on the content and logic of the script.

Conclusion

The osascript command in macOS provides a flexible and convenient way to run AppleScript and JavaScript code from the command line. In this article, we explored several use cases of the osascript command, including executing AppleScript commands, running multiple commands, executing AppleScript files, retrieving bundle identifiers, running JavaScript commands, and executing JavaScript files. By understanding these examples, developers and automation enthusiasts can leverage the power of osascript to automate tasks and control macOS applications efficiently.

Related Posts

How to use the command `nvm` (with examples)

How to use the command `nvm` (with examples)

The nvm command is a tool that allows you to easily manage different versions of Node.

Read More
How to use the command 'shuf' (with examples)

How to use the command 'shuf' (with examples)

The ‘shuf’ command is a utility that generates random permutations. It can be used to randomize the order of lines in a file, generate random numbers within a specified range, and more.

Read More
How to use the command 'jigsaw' (with examples)

How to use the command 'jigsaw' (with examples)

Jigsaw is a Laravel-based static site builder for PHP. It allows developers to easily create static websites using the Laravel framework.

Read More