Skip to main content
Metaist

Google Drive Links: Thunar and Finder

How I added context menu items to Thunar and Finder to copy Google Drive URLs.

Previously: Google Drive Links: rclone

Now that I have a script that can take paths on the filesystem and return URLs, I want an easy way to copy those URLs to the clipboard so that I can paste them in emails.

Generic Copy #

The first step was to create a generic way of copying to the clipboard on both Linux and macOS.

#!/usr/bin/env bash
# Platform-agnostic copy HTML to the clipboard.
# See: https://askubuntu.com/a/1227729
# See: https://stackoverflow.com/a/60768487

main() {
    local html=$(cat $@)
    local utf8=$(echo "$html" | sed -e 's/<[^>]*>//g')
    if command -v xclip &>/dev/null; then # Linux
        echo "$html" | xclip -t text/html -selection clipboard
    elif command -v osascript &>/dev/null; then # MacOS
        local hexd=$(echo "$html" | hexdump -ve '1/1 "%.2x"')
        printf "set the clipboard to «data HTML${hexd}»" | osascript -
        printf "set the clipboard to ((the clipboard as record) & {«class utf8»:\"$utf8\"})" | osascript -
    fi
}

main $@

On Linux, we can just use xclip, but on macOS you actually need to use ActionScript with a hexadecimal encoding of the HTML.

Thunar #

Next, we create context menus. On Linux, I use Thunar which supports Custom Actions (Edit > Configure custom actions...).

Because it takes a few seconds to retrieve the URLs, I use notify-send to make a little notification letting me know that the links can be pasted.

Thunar context menu showing copying Google Drive URLs

Finder #

On macOS, we can use Quick Action Workflows. You may need to enable permissions for Finder.

Open the Automator app and create a new "Quick Action".

This creates a .workflow folder in ~/Library/Services and a context menu in Finder.

Finder context menu showing copying Google Drive URLs