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

问题描述 投票:0回答:1

我正在尝试运行此脚本来更改壁纸,但是当文件的路径位于变量中时遇到问题

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
1个回答
0
投票

单引号可防止变量被扩展。您将需要使用其他单引号来分隔字符串,或者切换到双引号并根据需要进行转义:

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\""

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

© www.soinside.com 2019 - 2024. All rights reserved.