Introduction
In this article, we will explore how to open a new Safari window using Bash scripting. This can be particularly useful for tech support scenarios, where you need to automate the process of opening multiple windows or URLs for troubleshooting or testing purposes.
Using osascript for automation
AppleScript, or its command-line version osascript, is a powerful tool for automating tasks on macOS. In this case, we will use it to open a new Safari window and load a specified URL.
Basic example
Let's begin with a simple osascript command to open a new Safari window:
osascript -e 'tell application "Safari" to make new document'
Opening a specific URL
Now let's modify the previous example to open a specific URL. For this, we'll use the `set URL` command:
osascript -e 'tell application "Safari" to make new document with properties {URL:"https://www.example.com"}'
Creating a bash function
We can wrap the previous osascript command in a Bash function for reusability. First, we define the function with an arbitrary name (e.g., `open_safari_window`):
function open_safari_window() {
osascript -e 'tell application "Safari" to make new document with properties {URL:"$1"}'
}
Using the bash function for multiple URLs
Now we can use the Bash function defined earlier to open multiple URLs in different Safari windows. Since we're using a Bash function, this allows us to easily manage the opened windows and URLs:
# Opening two URLs in separate Safari windows
open_safari_window "https://www.google.com"
open_safari_window "https://www.apple.com"
In this article, we learned how to use the osascript command in a Bash function to open new Safari windows and load specific URLs. This is an essential skill for macOS automation and can be particularly useful in tech support scenarios.