1+ #! /usr/bin/env bash
2+ # License: GPLv3
3+ # Credits: Felipe Facundes
4+
5+ : << 'DOCUMENTATION '
6+ This script (text_it) saves clipboard content to a file, supporting Termux, macOS, X11, and Wayland.
7+ It detects the OS and uses the appropriate clipboard tool ('termux-clipboard-get', 'pbpaste', 'xclip', 'wl-paste').
8+ If no text is found or tools are missing, it shows an error. The output file is specified as an argument.
9+ Works cross-platform with clear success/error messages.
10+ DOCUMENTATION
11+
12+ # Colors for messages (optional)
13+ RED=' \033[0;31m'
14+ GREEN=' \033[0;32m'
15+ NC=' \033[0m' # No Color
16+
17+ # Creates a temporary file
18+ temp_file=$( mktemp)
19+
20+ # Definitive file
21+ file=" $* "
22+
23+ show_documentation () {
24+ awk '
25+ BEGIN { inside_block = 0 }
26+
27+ # Check the beginning of the DOCUMENTATION block
28+ /: <<' \' ' DOCUMENTATION' \' ' / { inside_block = 1; next }
29+
30+ # Check the end of the DOCUMENTATION block
31+ inside_block && $0 == "DOCUMENTATION" { inside_block = 0; exit }
32+
33+ # Print lines within the DOCUMENTATION block
34+ inside_block { print }
35+ ' " $0 "
36+ }
37+
38+ help () {
39+ show_documentation
40+
41+ echo -e " \nUsage: ${0##*/ } <filename>"
42+ }
43+
44+ if [[ " $1 " == -h ]] || [[ " $1 " == --help ]]; then
45+ help
46+
47+ # Check if a file name was provided
48+ elif [ -z " $1 " ]; then
49+ echo -e " ${RED} Error: Please provide a filename as argument${NC} \n"
50+ help
51+ exit 1
52+ fi
53+
54+ # Try different clipboard methods
55+ if [[ -n " $TERMUX_VERSION " ]]; then
56+ # Termux (Android)
57+ if command -v termux-clipboard-get & > /dev/null; then
58+ termux-clipboard-get > " $temp_file "
59+ else
60+ echo -e " ${RED} Error: termux-clipboard-get not found (Termux clipboard utility)${NC} "
61+ exit 1
62+ fi
63+ elif [[ " $( uname) " == " Darwin" ]]; then
64+ # macOS
65+ if command -v pbpaste & > /dev/null; then
66+ pbpaste > " $temp_file "
67+ else
68+ echo -e " ${RED} Error: pbpaste not found (macOS clipboard utility)${NC} "
69+ exit 1
70+ fi
71+ elif [[ " ${XDG_SESSION_TYPE,,} " == " wayland" ]]; then
72+ # Wayland
73+ if command -v wl-paste & > /dev/null; then
74+ wl-paste > " $temp_file "
75+ else
76+ echo -e " ${RED} Error: wl-paste not found (wl-clipboard package required)${NC} "
77+ exit 1
78+ fi
79+ else
80+ # X11
81+ if command -v xclip & > /dev/null; then
82+ xclip -o -selection clipboard > " $temp_file "
83+ elif command -v xsel & > /dev/null; then
84+ xsel --clipboard --output > " $temp_file "
85+ else
86+ echo -e " ${RED} Error: No clipboard utility found (xclip or xsel required)${NC} "
87+ exit 1
88+ fi
89+ fi
90+
91+ # Check if the clipboard had content
92+ if [ ! -s " $temp_file " ]; then
93+ echo -e " ${RED} Error: Clipboard is empty or contains no text${NC} "
94+ rm " $temp_file "
95+ exit 1
96+ fi
97+
98+ # Moves the temporary file to the final destination
99+ mv " $temp_file " " $file "
100+
101+ echo -e " ${GREEN} Success: Clipboard content saved to '$file '${NC} "
0 commit comments