Print URLs from Chrome tabs [macOS]

This article shows a shell script for macOS that automates Google Chrome. It gets the URLs of the tabs in the active Chrome window. It then prints these URLs.

Here is the contents of chrome-print-urls. It uses osascript, so this only works on macOS.

#!/usr/bin/env osascript -l JavaScript

function run(argv) {
    for (t in Application('Google Chrome').windows[0].tabs) {
        console.log(Application('Google Chrome').windows[0].tabs[t].url())
    }
}

console.log prints to STDERR, so you probably want to use the 2>&1 redirection.

Scenario: You have several YouTube video pages open in Chrome. You want to download those videos. You could just copy-and-paste the URLs from the browser to the terminal, but that is bothersome. Instead, you can use chrome-print-urls, save the URLs in a file and use youtube-dl to download the videos.

chrome-print-urls 2>&1 >urls.txt
youtube -a urls.txt -i -f 22/18

Relevant youtube-dl options:

  • -a, --batch-file <file> : File containing URLs to download (‘-‘ for stdin), one URL per line
  • -f, --format <format> : Video format code
  • -i, --ignore-errors : Continue on download errors, for example to skip unavailable videos in a playlist

-f 22/18 selects the 720p video if it is available; otherwise it selects the 480p or 360p video. These format codes are for mp4 files with audio. To see which formats are available, use youtube-dl -F <url>.

If there are tabs other than YouTube videos, you can filter them out using something like:

chrome-print-urls 2>&1 | ack 'youtube.com/watch'

Pin It on Pinterest