自定义Bash脚本在Terminal可运行但RayCast中无输出
解决Raycast脚本无法生成Gif文件的问题
问题核心原因
你遇到的关键问题是工作目录不匹配:Raycast运行脚本时的默认工作目录并非你日常终端使用的目录(比如桌面),直接写> out.gif会把文件生成到Raycast的私有运行目录中,导致你在预期位置找不到文件。
另外需注意:Raycast的环境变量PATH可能和终端不一致,理论上会导致无法找到ffmpeg或gifsicle,但从你描述的“执行完成”来看,这个可能性较低,不过也可以一并排查。
解决方案
1. 指定输出文件的绝对路径
把生成Gif的命令里的out.gif替换为目标目录的绝对路径,比如和截图文件同目录:
ffmpeg -i "$recent_file" -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > "$directory/out.gif"
这样生成的Gif会直接出现在/Users/willbeing/Desktop/Screenshots目录下,方便查找。
2. 切换工作目录后执行命令
在执行转换命令前,先切换到目标目录:
cd "$directory" ffmpeg -i "$recent_file" -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif
3. 确保命令工具的路径可访问
如果遇到命令找不到的情况,可以在脚本开头添加PATH配置,或者使用工具的绝对路径:
# 添加PATH(根据你的实际安装路径调整) export PATH="/usr/local/bin:$PATH" # 或者直接用绝对路径执行 /usr/local/bin/ffmpeg -i "$recent_file" -pix_fmt rgb24 -r 10 -f gif - | /usr/local/bin/gifsicle --optimize=3 --delay=3 > "$directory/out.gif"
修正后的完整脚本
#!/bin/bash # Required parameters: # @raycast.schemaVersion 1 # @raycast.title Gif Most Recent Screen Recording # @raycast.mode fullOutput # Optional parameters: # @raycast.icon 🤖 # Documentation: # @raycast.author willbeing # @raycast.authorURL https://raycast.com/willbeing directory="/Users/willbeing/Desktop/Screenshots" # Replace with your desired directory # Check if the directory exists if [ ! -d "$directory" ]; then echo "Directory does not exist." exit 1 fi # Retrieve the most recent .mov file recent_file=$(ls -t "$directory"/*.mov 2>/dev/null | head -n 1) # Output the most recent .mov file if [ -n "$recent_file" ]; then # 用绝对路径生成Gif文件 ffmpeg -i "$recent_file" -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > "$directory/out.gif" echo "Gif generated at: $directory/out.gif" else echo "No .mov files found in the directory." fi
内容的提问来源于stack exchange,提问作者willbeing
相关产品推荐
相关产品推荐

