我正在尝试运行此脚本来更改壁纸,但是当文件的路径位于变量中时遇到问题
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\""
还请注意,$()
形式是命令替换。