osascript 使用包含路径的变量更改墙纸失败

osascript changing wallpaper with variable containing the path fails

我正在尝试 运行 这个脚本来更改我的墙纸,但是当文件路径在变量中时我 运行 遇到了问题

sh run.sh
wallpaper_path="$(pwd)/assets/wallpaper.jpg"
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "$(wallpaper_path)"'

33:48: execution error: Finder got an error: AppleEvent handler failed. (-10000)

另一方面,绝对路径工作正常

osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/stupifatcat/workspace/project/assets/wallpaper.jpg"'

有谁知道我做错了什么吗?

单引号字符串防止变量被扩展。您将需要使用额外的单引号来分隔字符串,或切换为双引号并根据需要转义:

osascript -e 'tell application "Finder" to set desktop picture to POSIX file "'$wallpaper_path'"'

osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$wallpaper_path\""

另请注意,$() 形式是命令替换。