Last active
February 7, 2024 19:30
-
-
Save etiennecollin/041fb6321b0064f5b376a261ab86486d to your computer and use it in GitHub Desktop.
This is a script to convert Office files to PDF using LibreOffice
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
#!/usr/bin/env bash | |
# -*- coding: utf-8 -*- | |
# Author: Etienne Collin | |
# Date: 2023/05/20 | |
# Updated: 2024/02/07 | |
# Email: [email protected] | |
############################################################################### | |
# This is a script to convert Office files to PDF using LibreOffice. | |
# The script will convert all the files passed as arguments to PDF and | |
# provides the option to automatically remove the original office files after | |
# the conversion. | |
############################################################################### | |
# Path to the LibreOffice executable | |
libre_path="/Applications/LibreOffice.app/Contents/MacOS/soffice" | |
# Check if LibreOffice is installed | |
if [ ! -x "$(command -v "$libre_path")" ]; then | |
echo "LibreOffice is not installed. Please install LibreOffice and try again." | |
exit 1 | |
fi | |
# Check if the original files are to be deleted | |
while true; do | |
printf "Delete the original Office files after conversion? [y/n]: " | |
read input | |
# Check if the input is valid | |
if [ "$input" = "y" ] || [ "$input" = "n" ]; then | |
break | |
else | |
echo "Invalid input. Please enter 'y' or 'n'." | |
fi | |
done | |
# Pass the files to convert as arguments | |
files=("$@") | |
# Loop over the files | |
for file in "${files[@]}"; do | |
# Get the absolute path of the file and its directory | |
file_path=$(readlink -f "$file") | |
dir=$(dirname "$file") | |
name=$(basename "$file") | |
# Run the conversion command | |
"$libre_path" --headless --convert-to pdf "$file_path" --outdir "$dir" &>/dev/null | |
echo "Converted $name to PDF" | |
# Remove the file if needed | |
if [ "$input" = "y" ]; then | |
# Use trash to move to trash if utility is installed | |
# Can be installed with `brew install trash` | |
if [ -x "$(command -v trash)" ]; then | |
trash "$file_path" | |
else | |
rm "$file_path" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment