modern_lisp-machine/misc/scripts/copy_image.sh

49 lines
1.8 KiB
Bash
Executable File

EXEC_SHELL_PATH=$(command -v bash)
# Define the function to copy the most recent image file to the primary clipboard
copy_most_recent_image_to_clipboard() {
# Specify the correct folder paths
folder_path_x="/mnt/Data/mpv-screenshots/"
folder_path_y="/mnt/Data/mpv-screenshots/screenshots/"
# Check if both folders exist
if [ ! -d "$folder_path_x" ] || [ ! -d "$folder_path_y" ]; then
echo "Error: One or both folders do not exist."
return 1
fi
# Get the most recent file from FolderX
most_recent_x=$(find "$folder_path_x" -maxdepth 1 \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.bmp" -o -iname "*.tiff" \) -exec stat --format='%Y %n' {} + | sort -nr | head -n 1)
# Get the most recent file from FolderY
most_recent_y=$(find "$folder_path_y" -maxdepth 1 \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.bmp" -o -iname "*.tiff" \) -exec stat --format='%Y %n' {} + | sort -nr | head -n 1)
# Compare the modification times and choose the most recent file
if [ "$(echo "$most_recent_x" | cut -d' ' -f 1)" -gt "$(echo "$most_recent_y" | cut -d' ' -f 1)" ]; then
most_recent_file="$most_recent_x"
else
most_recent_file="$most_recent_y"
fi
# Extract the file path
file_path=$(echo "$most_recent_file" | cut -d' ' -f 2-)
# Check if xclip is installed
if ! command -v xclip &> /dev/null; then
echo "Error: xclip is not installed."
return 1
fi
# Determine the MIME type of the file
mime_type=$(file -b --mime-type "$file_path")
# Copy the most recent image file to the primary clipboard using xclip
xclip -selection clipboard -t "$mime_type" -i < "$file_path"
echo "Copied most recent image file to the primary clipboard: $file_path"
}
# Execute the function
copy_most_recent_image_to_clipboard