최근 이미지 파일을 [WebP 포멧](https://developers.google.com/speed/webp?hl=ko)으로 변환할 일이 많아졌다. 아쉽게도 애플 환경은 HEIC 포멧을 밀고 있기 때문에 공식적으로 WebP 변환 방법은 없다. 가장 간단한 방식은 [`cwebp`](https://developers.google.com/speed/webp/docs/cwebp?hl=ko) 커맨드를 활용하는 것이다. 커맨드 실행은 접근성이 낮으니 macOS 파인더에서 선택한 파일을 즉시 변환해주는 스크립트를 작성하고, 이를 [Raycast](https://www.raycast.com/) 스크립트 실행에 연동하였다. ## convert-to-webp-file.sh ```bash #!/bin/bash # Required parameters: # @raycast.schemaVersion 1 # @raycast.title Convert to WebP file # @raycast.mode fullOutput # Optional parameters: # @raycast.icon 🖼️ # Documentation: # @raycast.author chorr # @raycast.authorURL https://raycast.com/chorr # Check if cwebp is installed if ! command -v cwebp &> /dev/null; then echo "cwebp could not be found. Please install it using 'brew install webp'." exit 1 fi # AppleScript to get the selected files in Finder selected_files=$(osascript <<EOD tell application "Finder" set theSelection to selection if (count of theSelection) is 0 then return "no selection" else set selectedPaths to "" repeat with anItem in theSelection set selectedPaths to selectedPaths & POSIX path of (anItem as alias) & linefeed end repeat return selectedPaths end if end tell EOD ) # Check if no files were selected if [ "$selected_files" = "no selection" ]; then echo "No files selected." exit 1 fi # Convert the newline-separated list of selected files into an array IFS=\n' read -r -d '' -a files_array <<< "$selected_files" # Convert each selected image file to WebP for input_file in "${files_array[@]}"; do # Trim any trailing whitespace or newlines input_file=$(echo "$input_file" | tr -d '[:space:]') # Check if the selected file exists and is a image file if [ ! -f "$input_file" ]; then echo "The selected file does not exist: $input_file" continue fi # Set the output file path by replacing the extension with .webp output_file="${input_file%.*}.webp" # Convert image to WebP cwebp "$input_file" -o "$output_file" if [ $? -eq 0 ]; then echo "Successfully converted $input_file to $output_file" else echo "Failed to convert $input_file" fi done ``` 이 스크립트 역시 친절한 ChatGPT 친구가 만들어줬다. ## Prompts ``` bash를 활용한 Raycast Scripts 만들 수 있니? ``` ``` 오케이 좋아. jpg 이미지 파일을 webp 파일로 변환하고 싶어. Raycast Script 적합하게 스크립트 생성. ``` ``` `@raycast.argument1` 대신 finder에서 선택된 파일을 사용할 수 있니? ``` ``` execution error: Can’t make item 1 of selection of application "Finder" into type alias. (-1700) No file selected or the selected file does not exist. -- 이 결과를 반환하고 있어. ``` ``` 만약 `selected_file` 부분이 `selected_files`가 된다면? ``` ``` IFS=\n' read -r -d '' -a files_array <<< "$selected_files" -- 해당 라인 코드에서 files_array 결과는 항상 1개여서 문제가 되고 있습니다. ``` ``` AMAZING! it worked perfectly! ```