我正在编写一个bash脚本,负责在我的mac上设置桌面背景。我可以用以下方式设置桌面背景:
$ osascript -e 'tell app "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid White.png"'
但是,我还需要获取桌面图片的路径。我得到的最接近的是:
$ osascript -e 'tell app "Finder" to get desktop picture'
这将返回桌面图片的路径,但是我使用的格式非常奇怪:
document file Solid White.png of folder Solid Colors of folder Desktop Pictures of folder Library of startup disk
我有什么方法可以获得当前桌面图片的路径:
/Library/Desktop\ Pictures/Solid\ Colors/Solid\ White.png
答案 0 :(得分:6)
我发现答案可以缩短为一行:
osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
答案 1 :(得分:4)
像这样:
osascript -e '
tell application "Finder"
set theDesktopPic to desktop picture as alias
set theName to posix path of theDesktopPic
end tell'
/Users/mark/Documents/Carbon fibre.png
答案 2 :(得分:0)
以下在MacOS Mojave 10.14.6下使用脚本编辑器和osascript
对我有用:
tell application "System Events" to get pictures folder of every desktop
输出:
{"/Users/jchilders/Library/Mobile Documents/com~apple~CloudDocs/Wallpapers"}
将every
替换为first
以获得一个条目:
tell application "System Events" to get pictures folder of first desktop
输出:
"/Users/jchilders/Library/Mobile Documents/com~apple~CloudDocs/Wallpapers"
apple.stackexchange.com here的原始答案。