Skip to content

Instantly share code, notes, and snippets.

@hoishing
Last active March 18, 2024 04:57
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
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><g fill="none" stroke="#66A80F" stroke-linecap="round" stroke-width="1.5"><path d="M4 6V3.6a.6.6 0 0 1 .6-.6h14.8a.6.6 0 0 1 .6.6V6M4 18v2.4a.6.6 0 0 0 .6.6h14.8a.6.6 0 0 0 .6-.6V18"/><path stroke-linejoin="round" d="M13.5 15V9h2.4a.6.6 0 0 1 .6.6v.9A1.5 1.5 0 0 1 15 12v0"/><path stroke-linejoin="round" d="M13.5 15h2.4a.6.6 0 0 0 .6-.6v-.9A1.5 1.5 0 0 0 15 12v0h-1.5m6 3v-3m0 0V9h3v3h-3Zm-18-3v6L3 12l1.5 3V9m6 0h-3v6h3m-3-3h2"/></g></svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment