Last active
July 15, 2024 07:23
-
-
Save hoishing/40baa11323ed08ef584c5dbb47543997 to your computer and use it in GitHub Desktop.
Raycast Scripts
This file contains 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/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 |
This file contains 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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment