Saturday, February 11, 2023

Applescript to Open a URL in Chrome

Write a applescript to open a Chrome browser window to a given URL A and another URL B in another window, the windows must be visible and stay next to each other

To run this AppleScript from the terminal, you can use the osascript command. Here's an example:


osascript -e 'tell application "Google Chrome" activate set window1 to make new window with properties {bounds:{0, 0, 500, 500}} set window2 to make new window with properties {bounds:{500, 0, 1000, 500}} set URL A to "https://www.exampleA.com" set URL B to "https://www.exampleB.com" tell window1 to set URL to URL A tell window2 to set URL to URL B end tell'

You can save the script to a file with a .scpt extension and run it from the terminal using the following command:

osascript path/to/script.scpt

Bash script:

#!/bin/bash URL_A="https://www.exampleA.com" URL_B="https://www.exampleB.com" google-chrome --new-window $URL_A & window1_pid=$! google-chrome --new-window $URL_B & window2_pid=$! # Wait for the first window to open while true; do if xdotool search --pid $window1_pid 2> /dev/null; then break fi sleep 0.1 done # Get the window ID of the first window window1_id=$(xdotool search --pid $window1_pid | head -1) # Wait for the second window to open while true; do if xdotool search --pid $window2_pid 2> /dev/null; then break fi sleep 0.1 done # Get the window ID of the second window window2_id=$(xdotool search --pid $window2_pid | head -1) # Move and resize the windows xdotool windowmove $window1_id 0 0 xdotool windowsize $window1_id 500 500 xdotool windowmove $window2_id 500 0 xdotool windowsize $window2_id 500 500

This script uses the google-chrome command to open two Chrome browser windows and the xdotool command to move and resize the windows. The script sets the URL for each window to URL_A and URL_B respectively. The script waits for each window to open and then gets the window ID of each window. Finally, the script uses xdotool to move and resize the windows to specific positions on the screen.