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...
).
- Basic
- Name:
Copy GDrive Links
- Command:
$HOME/bin/,gdrive-id.py --html %F | $HOME/bin/,copy-html.sh; notify-send "GDrive Links Copied (HTML)"
- Name:
- Appearance Conditions
- File Pattern:
*
- Appears if selection contains: (I checked all the items)
- File Pattern:
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.
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".
- Workflow receives current:
files or folders
inany application
- Run Shell Script:
source ~/.zshrc; python $(which ,gdrive-id.py) --html $@ | $(which ,copy-html.sh)
- Display Notification:
GDrive Links Copied (HTML)
This creates a .workflow
folder in ~/Library/Services
and a context menu in Finder.