alexwlchan / safari.rs

Command-line utilities for interacting with Safari on macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  • Convert links for a mobile site to the desktop site – for example, converting mobile.twitter.com to twitter.com.
  • Stripping tracking junk (e.g. UTM tracking parameters) from URLs.
  • Removing some extraneous information that isn't generally useful.

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?

  • It's really fast. The Rust script returns immediately – when I tried writing this with Python, I had a noticeable delay when typing ;furl. This is a tool I use dozens of times a week, so every second counts.
  • I like Rust, and I’ve been enjoying playing with it recently.

License

MIT.

About

Command-line utilities for interacting with Safari on macOS

License:MIT License


Languages

Language:Rust 92.9%Language:Python 6.2%Language:AppleScript 0.9%