Check if fzf is installed

March 11, 2025 · View on GitHub

#!/bin/bash

Check if fzf is installed

if ! command -v fzf &> /dev/null; then echo "Error: fzf is not installed. Please install it first." echo "You can install it with:" echo " - On macOS: brew install fzf" echo " - On Ubuntu/Debian: sudo apt install fzf" echo " - On other systems: see https://github.com/junegunn/fzf#installation" exit 1 fi

Check if curl is installed

if ! command -v curl &> /dev/null; then echo "Error: curl is not installed. Please install it first." exit 1 fi

Check arguments

if [ -z "$1" ]; then echo "Usage: $0 [output-filename]" echo "Example: $0 https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz/edit myfile" echo "Note: If output-filename is provided, the selected format extension will be added to it" exit 1 fi

Extract document ID from URL

URL="$1" CUSTOM_FILENAME="$2"

More robust document ID extraction that works across different grep versions

if [[ "URL"= /document/d/([/]+)]];thenDOCID="URL" =~ /document/d/([^/]+) ]]; then DOC_ID="{BASH_REMATCH[1]}" else # Try alternative extraction method as fallback DOC_ID=(echo"(echo "URL" | sed -E 's|./document/d/([^/?]+).|\1|') fi

if [ -z "$DOC_ID" ]; then echo "Error: Could not extract document ID from URL." echo "Please provide a valid Google Docs URL." exit 1 fi

Format options for fzf (with PDF first) - using simple array instead of associative array

FORMAT_OPTIONS=( "pdf|PDF Document (Default)" "docx|Word Document" "odt|OpenDocument Text" "txt|Plain Text" "html|HTML Document" "rtf|Rich Text Format" "epub|EPUB E-book" )

Display format selection with fzf (removed --header-first option)

SELECTED_FORMAT=(printf"(printf "%s\n" "{FORMAT_OPTIONS[@]}" | fzf --height=10 --layout=reverse --prompt="Select format to download: " --header="Press Enter to select, Esc to cancel")

Exit if no format was selected (user pressed Esc)

if [ -z "$SELECTED_FORMAT" ]; then echo "Download cancelled." exit 0 fi

Extract format code from selection

FORMAT=(echo"(echo "SELECTED_FORMAT" | cut -d'|' -f1)

Define output filename

if [ -n "CUSTOM_FILENAME" ]; then # Use custom filename with format extension FILENAME="{CUSTOM_FILENAME}.FORMAT" else # Use default naming scheme FILENAME="document.FORMAT" COUNT=1

# Check if file already exists and create a unique filename
while [ -f "$FILENAME" ]; do
    FILENAME="document-$COUNT.$FORMAT"
    COUNT=$((COUNT+1))
done

fi

echo "Downloading document as FORMAT..."echo"DocumentID:FORMAT..." echo "Document ID: DOC_ID" echo "Output file: $FILENAME"

Download the file

curl -L "https://docs.google.com/document/d/$DOC_ID/export?format=$FORMAT" -o "$FILENAME"

Check if download was successful

if [ ?eq0];thenecho"Downloadcomplete:? -eq 0 ]; then echo "Download complete: FILENAME" else echo "Error: Download failed." exit 1 fi