-
Notifications
You must be signed in to change notification settings - Fork 947
Add qpdf compression command tool #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Dependency: This script requires `qpdf` installed: https://github.com/qpdf/qpdf | ||
| # Install via homebrew: `brew install qpdf` | ||
|
|
||
| # Required parameters: | ||
| # @raycast.schemaVersion 1 | ||
| # @raycast.title Compress PDF | ||
| # @raycast.mode compact | ||
| # @raycast.packageName QPDF | ||
|
|
||
| # Optional parameters: | ||
| # @raycast.icon 🗜️ | ||
| # @raycast.argument1 { "type": "text", "placeholder": "Quality (0-100, default: 50)", "optional": true } | ||
|
|
||
| # Documentation: | ||
| # @raycast.description Compress selected PDF files. Note: This script requires 'qpdf' to be installed via Homebrew. | ||
| # @raycast.author Nicklas Jakobsen | ||
| # @raycast.authorURL https://github.com/nicklasjm | ||
|
|
||
| # Configuration | ||
| QUALITY="${1:-50}" | ||
| export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" | ||
|
|
||
| # Check for qpdf dependency | ||
| if ! command -v qpdf &> /dev/null; then | ||
| echo "Error: qpdf is not installed." | ||
| echo "Run 'brew install qpdf' in Terminal." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Get files via AppleScript | ||
| SELECTED_FILES=$(osascript <<EOD | ||
| tell application "Finder" | ||
| set theSelection to selection | ||
| if theSelection is {} then | ||
| return "" | ||
| end if | ||
| set output to "" | ||
| repeat with theItem in theSelection | ||
| set output to output & POSIX path of (theItem as alias) & "\n" | ||
| end repeat | ||
| return output | ||
| end tell | ||
| EOD | ||
| ) | ||
|
|
||
| if [ -z "$SELECTED_FILES" ]; then | ||
| echo "No files selected in Finder" | ||
| exit 1 | ||
| fi | ||
|
|
||
| IFS=$'\n' | ||
| count=0 | ||
| last_output="" | ||
|
|
||
| # Loop through files and compress | ||
| for f in $SELECTED_FILES; do | ||
| # Ignore non-PDF files | ||
| if [[ "$f" != *.pdf && "$f" != *.PDF ]]; then | ||
| continue | ||
| fi | ||
|
|
||
| filename=$(basename "$f") | ||
| echo "Processing: $filename ($QUALITY%)..." | ||
|
|
||
| dir=$(dirname "$f") | ||
| base_filename=$(basename "$f" .pdf) | ||
|
|
||
| # Filename format: Name_50%.pdf | ||
| output="$dir/${base_filename}_${QUALITY}%.pdf" | ||
|
|
||
| # Run qpdf | ||
| # We use --recompress-flate to ensure images are unpacked and re-evaluated | ||
| qpdf --compress-streams=y \ | ||
| --recompress-flate \ | ||
| --decode-level=generalized \ | ||
| --compression-level=9 \ | ||
| --optimize-images \ | ||
| --jpeg-quality="$QUALITY" \ | ||
| --object-streams=generate \ | ||
| "$f" "$output" | ||
|
|
||
| if [ $? -eq 0 ]; then | ||
| ((count++)) | ||
| last_output="$output" | ||
| else | ||
| echo "Error processing: $filename" | ||
| sleep 2 | ||
| fi | ||
| done | ||
|
|
||
| # Show result | ||
| if [ $count -eq 0 ]; then | ||
| echo "No files were created." | ||
| exit 1 | ||
| else | ||
| # Reveal the last created file in Finder | ||
| if [ -n "$last_output" ]; then | ||
| open -R "$last_output" | ||
| fi | ||
| echo "Done! Saved as ..._${QUALITY}%.pdf" | ||
| fi |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must provide the information that an extra application must be installed on the user's computer to run your Script Command as defined on "Scripts that require installation of runtimes and dependencies" section of our CONTRIBUTING.md documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just this one is missing.