重装系统后AppleScript调用jq失败:sh:jq: command not found求助
解决Devonthink3日志脚本中“sh:jq: command not found”错误
我在Devonthink3中用脚本创建每日日志,创建笔记时会自动添加当日天气及预报。脚本正常运行了约90天,重装操作系统后执行脚本时抛出错误:Failed to parse weather data: sh:jq: command not found。我已经安装了JSON Helper和jq,但问题依然存在,以下是脚本中获取天气的核心代码:
-- Fetch and format weather data on getWeather() set weatherAPIURL to "http://api.weatherapi.com/v1/forecast.json?key=86a511ece53749c88e9205427250301&q=Detroit&days=1&aqi=no&alerts=no" try set weatherData to do shell script "curl -s " & quoted form of weatherAPIURL on error errMsg display dialog "Failed to fetch weather data: " & errMsg return "" end try try set currentTempFt to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.temp_f'" set currentCondition to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.condition.text'" set currentWindMph to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.wind_mph'" set currentWindDir to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.wind_dir'" set currentHumidity to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.humidity'" set feelslikeF to do shell script "echo " & quoted form of weatherData & " | jq -r '.current.feelslike_f'" set forecastCondition to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].day.condition.text'" set forecastMaxTempF to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].day.maxtemp_f'" set forecastMinTempF to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].day.mintemp_f'" set forecastAvgHumidity to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].day.avghumidity'" set forecastUV to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].day.uv'" set astroSunrise to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].astro.sunrise'" set astroSunsetAt to do shell script "echo " & quoted form of weatherData & " | jq -r '.forecast.forecastday[0].astro.sunset'" set formattedDate to short date string of (current date) set formattedTime to time string of (current date) set output to formattedDate & " " & formattedTime & return & ¬ "Currently, it's " & currentTempFt & "º, and " & currentCondition & " outside, the wind is " & currentWindMph & " mph from the " & currentWindDir & " and the Humidity is " & currentHumidity & ". " & ¬ "Currently, it feels like " & feelslikeF & "º. Today's forecast calls for " & forecastCondition & ", with a high of " & forecastMaxTempF & " ºF and a low of " & forecastMinTempF & " º, today's humidity will be " & forecastAvgHumidity & "%, with a UV index of " & forecastUV & ". " & ¬ "Sunrise is at " & astroSunrise & " this morning, and Sunset will be at " & astroSunsetAt & " this evening." return output on error errMsg display dialog "Failed to parse weather data: " & errMsg return "" end try end getWeather
解决方法
指定jq的绝对路径
重装系统后,AppleScript的do shell script默认PATH可能不包含jq的安装目录。打开终端执行which jq,得到jq的绝对路径(比如/usr/local/bin/jq或/opt/homebrew/bin/jq),然后把脚本中所有jq替换成这个绝对路径。例如:set currentTempFt to do shell script "echo " & quoted form of weatherData & " | /usr/local/bin/jq -r '.current.temp_f'"显式设置PATH环境变量
在脚本的do shell script命令中添加PATH设置,确保shell能找到jq。可以在调用curl的代码前统一设置:set weatherData to do shell script "export PATH=\"/usr/local/bin:/opt/homebrew/bin:$PATH\"; curl -s " & quoted form of weatherAPIURL这样后续所有shell命令都会使用这个包含jq路径的PATH。
验证jq安装有效性
打开终端执行jq --version,如果能正常输出版本信息说明安装没问题;若报错,重新用Homebrew安装:brew install jq,确保安装过程无异常。
内容的提问来源于stack exchange,提问作者Stephen Ogletree
相关产品推荐
相关产品推荐

