Mac: 从终端获取当前桌面图片路径
Mac: Getting path of current desktop picture from terminal
我正在编写一个 bash 脚本来负责设置 mac 上的桌面背景。我可以设置桌面背景:
$ osascript -e 'tell app "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid White.png"'
不过,我还需要GET桌面图片的路径。我得到的最接近的是:
$ osascript -e 'tell app "Finder" to get desktop picture'
这个 return 是桌面图片的路径,但格式很奇怪,我无法使用:
document file Solid White.png of folder Solid Colors of folder Desktop Pictures of folder Library of startup disk
有什么方法可以获取当前桌面图片的路径 return:
/Library/Desktop\ Pictures/Solid\ Colors/Solid\ White.png
?
像这样:
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
我发现答案可以缩短到一行:
osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
在 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 的原始答案。
我正在编写一个 bash 脚本来负责设置 mac 上的桌面背景。我可以设置桌面背景:
$ osascript -e 'tell app "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid White.png"'
不过,我还需要GET桌面图片的路径。我得到的最接近的是:
$ osascript -e 'tell app "Finder" to get desktop picture'
这个 return 是桌面图片的路径,但格式很奇怪,我无法使用:
document file Solid White.png of folder Solid Colors of folder Desktop Pictures of folder Library of startup disk
有什么方法可以获取当前桌面图片的路径 return:
/Library/Desktop\ Pictures/Solid\ Colors/Solid\ White.png
?
像这样:
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
我发现答案可以缩短到一行:
osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
在 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 的原始答案。