Skip to content

Commit e644bfc

Browse files
authored
Add qpdf compression command tool (#1012)
* Add qpdf compression command tool * Updated comments and namings * updated description * Specified how to install qpdf via brew
1 parent bee00cb commit e644bfc

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/bin/bash
2+
3+
# Dependency: This script requires `qpdf` installed: https://github.com/qpdf/qpdf
4+
# Install via homebrew: `brew install qpdf`
5+
6+
# Required parameters:
7+
# @raycast.schemaVersion 1
8+
# @raycast.title Compress PDF
9+
# @raycast.mode compact
10+
# @raycast.packageName QPDF
11+
12+
# Optional parameters:
13+
# @raycast.icon 🗜️
14+
# @raycast.argument1 { "type": "text", "placeholder": "Quality (0-100, default: 50)", "optional": true }
15+
16+
# Documentation:
17+
# @raycast.description Compress selected PDF files. Note: This script requires 'qpdf' to be installed via Homebrew.
18+
# @raycast.author Nicklas Jakobsen
19+
# @raycast.authorURL https://github.com/nicklasjm
20+
21+
# Configuration
22+
QUALITY="${1:-50}"
23+
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
24+
25+
# Check for qpdf dependency
26+
if ! command -v qpdf &> /dev/null; then
27+
echo "Error: qpdf is not installed."
28+
echo "Run 'brew install qpdf' in Terminal."
29+
exit 1
30+
fi
31+
32+
# Get files via AppleScript
33+
SELECTED_FILES=$(osascript <<EOD
34+
tell application "Finder"
35+
set theSelection to selection
36+
if theSelection is {} then
37+
return ""
38+
end if
39+
set output to ""
40+
repeat with theItem in theSelection
41+
set output to output & POSIX path of (theItem as alias) & "\n"
42+
end repeat
43+
return output
44+
end tell
45+
EOD
46+
)
47+
48+
if [ -z "$SELECTED_FILES" ]; then
49+
echo "No files selected in Finder"
50+
exit 1
51+
fi
52+
53+
IFS=$'\n'
54+
count=0
55+
last_output=""
56+
57+
# Loop through files and compress
58+
for f in $SELECTED_FILES; do
59+
# Ignore non-PDF files
60+
if [[ "$f" != *.pdf && "$f" != *.PDF ]]; then
61+
continue
62+
fi
63+
64+
filename=$(basename "$f")
65+
echo "Processing: $filename ($QUALITY%)..."
66+
67+
dir=$(dirname "$f")
68+
base_filename=$(basename "$f" .pdf)
69+
70+
# Filename format: Name_50%.pdf
71+
output="$dir/${base_filename}_${QUALITY}%.pdf"
72+
73+
# Run qpdf
74+
# We use --recompress-flate to ensure images are unpacked and re-evaluated
75+
qpdf --compress-streams=y \
76+
--recompress-flate \
77+
--decode-level=generalized \
78+
--compression-level=9 \
79+
--optimize-images \
80+
--jpeg-quality="$QUALITY" \
81+
--object-streams=generate \
82+
"$f" "$output"
83+
84+
if [ $? -eq 0 ]; then
85+
((count++))
86+
last_output="$output"
87+
else
88+
echo "Error processing: $filename"
89+
sleep 2
90+
fi
91+
done
92+
93+
# Show result
94+
if [ $count -eq 0 ]; then
95+
echo "No files were created."
96+
exit 1
97+
else
98+
# Reveal the last created file in Finder
99+
if [ -n "$last_output" ]; then
100+
open -R "$last_output"
101+
fi
102+
echo "Done! Saved as ..._${QUALITY}%.pdf"
103+
fi

0 commit comments

Comments
 (0)