Skip to content

Instantly share code, notes, and snippets.

@hoishing
Last active July 15, 2024 07:23
Show Gist options
  • Save hoishing/40baa11323ed08ef584c5dbb47543997 to your computer and use it in GitHub Desktop.
Save hoishing/40baa11323ed08ef584c5dbb47543997 to your computer and use it in GitHub Desktop.
Raycast Scripts
#!/usr/bin/osascript
tell application "Finder"
set selectedItems to selection
set itemList to ""
repeat with anItem in selectedItems
set itemList to itemList & POSIX path of (anItem as alias) & "\n"
end repeat
return itemList
end tell
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title WebP Converter
# @raycast.mode silent
# Optional parameters:
# @raycast.icon webp.svg
# @raycast.argument1 {"type": "text", "placeholder": "quality(default 80)", "optional": true}
# Documentation:
# @raycast.description Convert images to WebP format
# check if cwebp installed
if ! command -v /opt/homebrew/bin/cwebp &>/dev/null; then
echo "cwebp not found, install with brew ⚠️"
exit 1
fi
# Fetch selected files in Finder using AppleScript
selected_files=$(osascript get-finder-items.applescript)
# Set Internal Field Separator to newline for loop iteration
IFS=$'\n'
# exit with error if no files select
if [[ ! $selected_files ]]; then
echo "no file selected ⚠️"
exit 1
fi
# set default quality to 80 if user not set the optional param
quality=${1:-"80"}
# Loop through each file
for img in $selected_files; do
# only process specific image types
if ! echo "$img" | grep -iE "^.*(png|jpg|jpeg|bmp|tiff)$" >/dev/null; then
echo "⚠️ error: only support png, jpg, bmp, tiff"
continue
fi
output_file="${img%.*}.webp"
# perform lossless compression if user set the quality to 100
if [[ $quality == "100" ]]; then
opt=(-z 5)
else
opt=(-q $quality)
fi
# array with command and its arguments
cmd=(/opt/homebrew/bin/cwebp -quiet "$img" -o "$output_file" "${opt[@]}")
# execute the command
"${cmd[@]}"
# print results
if [[ ! $? ]]; then
echo "can't convert $img"
exit 1
else
echo "webp created "
fi
done
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment