alexwlchan / safari.rs

Command-line utilities for interacting with Safari on macOS
MIT License
24 stars 0 forks source link
macos rust safari

Archived 11 February 2024: this was a fun experiment in using Rust, but I'm no longer using or maintaining it – I've replaced it with JavaScript For Automation (JXA) scripts that I run directly.


safari.rs

safari.rs provides some tools for interacting with Safari on the command-line.

Commands

  1. Get the URL from a given window or tab:

    $ # Get the URL of the frontmost tab of the frontmost window
    $ safari url
    https://github.com
    
    $ # Get a URL from the second window from the tab
    $ safari url --window=2
    https://example.com/foo
    
    $ # Third tab from the left of the second window
    $ safari url --window=2 --tab=3
    https://example.com/foo

    I have the first two commands bound to shortcuts furl and 2url for quick access.

  2. Get a list of URLs from every open tab:

    $ safari list-tabs
    https://github.com
    https://example.com/foo
    https://crates.io/crates/urlparse
    ...
  3. Go through and batch close tabs:

    $ safari clean-tabs youtube.com,twitter.com

    I find this useful for quickly cutting down my open tabs.

  4. Get a list of URLs from Reading List:

    $ safari reading-list
  5. Get a list of URLs from all your devices with iCloud Tabs:

    $ safari icloud-tabs

    You can get a list of known devices with icloud-tabs --list-devices, and filter the URLs with the --device flag:

    $ safari icloud-tabs --device="Alex's iPhone"

Installation

You need Rust installed. Then to install:

$ cargo install --git https://github.com/alexwlchan/safari.rs

It's tested in Travis with the current version of stable Rust, but it only gets tested when it was last modified (at time of writing, that was Rust 1.40.0).

URL transformations

The commands that produce URLs do a bit of cleaning before they return:

Motivation

I first got the idea for a script to access Safari URLs from Dr. Drang. I've been through several different versions – AppleScript, shell, Python – gradually adding the cleaning features – and now I've written a new version in Rust.

Why Rust?

License

MIT.