Skip to content
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

Add info on how to run this from right click menu #61

Open
costantinoai opened this issue Nov 12, 2024 · 1 comment
Open

Add info on how to run this from right click menu #61

costantinoai opened this issue Nov 12, 2024 · 1 comment

Comments

@costantinoai
Copy link

costantinoai commented Nov 12, 2024

This is not really an issue, but it would be nice to add information on how to run the extension from the right-click menu on a file, essentially running impress headless.

On Ubuntu, this can be done in nautilus by creating a new script in ~/.local/share/nautilus/scripts/ODP-to-expanded-PDF.sh as follow:

#!/bin/bash

# Define the log file on the desktop
log_file="$HOME/Desktop/expand_animation_log.txt"

# Function to log messages with timestamps
log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$log_file"
}

# Start logging
log "Script started."

# Capture initial list of soffice.bin PIDs
initial_soffice_pids=($(pgrep -f soffice.bin))
log "Initial LibreOffice processes (soffice.bin) running: ${initial_soffice_pids[*]}"

# Check if a file path was provided as an argument
if [ -z "$1" ]; then
  log "Error: No file path provided. Usage: $0 <path_to_presentation_file>"
  exit 1
fi

# Get the full path of the input file and define output PDF path
input_file="$(realpath "$1")"
output_pdf="${input_file%.*}.pdf"  # PDF file generated by the macro

# Check if the file has a supported extension
file_extension="${input_file##*.}"
if [[ "$file_extension" != "odp" && "$file_extension" != "ppt" && "$file_extension" != "pptx" ]]; then
  log "Error: Unsupported file extension '$file_extension'. Supported extensions are .odp, .ppt, .pptx."
  exit 1
fi

# Log input and output paths
log "Input file: $input_file"
log "Output PDF (target): $output_pdf"

# Step 1: Run LibreOffice Impress in headless mode with the macro to expand animations
log "Running LibreOffice Impress in headless mode with macro to expand animations."
libreoffice --impress --headless "$input_file" "macro:///ExpandAnimations.ExpandAnimations.Main" &>> "$log_file" &

# Get the LibreOffice process ID
lo_pid=$!

# Allow time for the macro to process, with a 30-second timeout
log "Waiting for output PDF creation (up to 30 seconds)..."
timeout=30  # maximum wait time in seconds
interval=2  # check every 2 seconds
elapsed=0

while [ ! -f "$output_pdf" ] && [ $elapsed -lt $timeout ]; do
  sleep $interval
  elapsed=$((elapsed + interval))
  log "Checked for output PDF; $elapsed seconds elapsed."
done

# Check if the file was created within the timeout
if [ -f "$output_pdf" ]; then
  log "Output PDF created successfully: $output_pdf"
else
  log "Error: Output PDF not created within the timeout period."
fi

# Capture current list of soffice.bin PIDs
current_soffice_pids=($(pgrep -f soffice.bin))
log "Current LibreOffice processes (soffice.bin) running: ${current_soffice_pids[*]}"

# Identify and kill new LibreOffice processes that were not in the initial list
for pid in "${current_soffice_pids[@]}"; do
  if [[ ! " ${initial_soffice_pids[@]} " =~ " $pid " ]]; then
    log "Terminating new LibreOffice process (soffice.bin) with PID: $pid"
    kill -9 "$pid"
  fi
done

# Remove temporary lock file if it exists
lock_file="$(dirname "$input_file")/.~lock.$(basename "$input_file")#"
if [ -e "$lock_file" ]; then
  log "Removing lock file: $lock_file"
  rm "$lock_file"
else
  log "No lock file found to remove."
fi

# Confirm completion and log final message
log "Conversion completed. Check output PDF at $output_pdf."
log "Script finished."

which will results in a new menu item Scripts --> ODP to expanded PDF.

The only modification needed to the source code is to delete the line:

msgbox "Expansion done! See "+newUrlPdf

from the file in

/home/eik-tb/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu2162478urse.tmp_/ExpandAnimations-0.13.oxt/ExpandAnimations/ExpandAnimations.xba

(make sure to change lu2162478urse with your correct folder name). This turns off the dialog box at the end of the conversion process, which causes LO to get stuck in headless mode (which would be nice to toggle off in a more user-friendly way perhaps :) )

Not sure if this is the right place for this, but I thought I'd share since this was handy for me.

Thanks for the nice tool!

@monperrus
Copy link
Owner

monperrus commented Nov 12, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants