返回

个性化您的 macOS 桌面:打造自定义动态壁纸

闲谈

引言

在 macOS 中,动态壁纸是一个绝佳的功能,可以让您使用一系列动画或视频作为桌面背景。然而,苹果公司提供的预设选项可能有限,无法满足您的个性化需求。通过结合 Applescript 和 Launchctl 的强大功能,您可以创建自定义动态壁纸,让您的桌面在一天中的不同时间展示不同的图像或动画。

准备工作

在开始之前,您需要确保已安装以下内容:

  • macOS Mojave 或更高版本
  • Applescript Editor(位于 /Applications/Utilities/ 中)
  • Launchctl(在终端中可用)

编写 Applescript 脚本

  1. 打开 Applescript Editor。
  2. 创建一个新的脚本。
  3. 粘贴以下脚本:
set desktopPicture to POSIX path of (choose file with prompt "选择您的动态壁纸图像或动画" of type {"jpg", "jpeg", "png", "gif", "mov", "mp4"})
do shell script "launchctl unload ~/Library/LaunchAgents/com.example.dynamicwallpaper.plist"
do shell script "rm ~/Library/LaunchAgents/com.example.dynamicwallpaper.plist"
set theText to ¬
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
    <key>Label</key>
    <string>com.example.dynamicwallpaper</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>-e</string>
        <string>tell application \"Finder\"
set desktop picture to \"" & desktopPicture & "\"
end tell</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>0</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>0</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>1</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>1</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
    </array>
</dict>
</plist>"
set theFile to open for access "~/Library/LaunchAgents/com.example.dynamicwallpaper.plist" with write permission
set eof of theFile to 0
write theText to theFile
close access theFile
do shell script "launchctl load ~/Library/LaunchAgents/com.example.dynamicwallpaper.plist"
  1. <string>/usr/bin/osascript</string> 中的路径更改为您安装的 Applescript Editor 的路径。
  2. <string>tell application \"Finder\" set desktop picture to \"" & desktopPicture & "\" end tell</string> 中的 desktopPicture 替换为您选择的壁纸图像或动画的路径。
  3. <array> <dict> <key>Hour</key> <integer>0</integer> <key>Minute</key> <integer>0</integer> </dict> <dict> <key>Hour</key> <integer>0</integer> <key>Minute</key> <integer>30</integer> </dict> <dict> <key>Hour</key> <integer>1</integer> <key>Minute</key> <integer>0</integer> </dict> <dict> <key>Hour</key> <integer>1</integer> <key>Minute</key> <integer>30</integer> </dict> </array> 中的 HourMinute 值更改为您希望壁纸更改的时间间隔。

创建 Launchctl 任务

  1. 在终端中输入以下命令:
sudo nano ~/Library/LaunchAgents/com.example.dynamicwallpaper.plist
  1. 粘贴前面编写的 Applescript 脚本。
  2. 保存并退出文件。
  3. 输入以下命令以加载任务:
launchctl load ~/Library/LaunchAgents/com.example.dynamicwallpaper.plist

测试您的动态壁纸

动态壁纸现在应该根据您指定的间隔自动更改。要测试它,请等待下一个间隔时间,或者使用以下命令手动触发壁纸更改:

launchctl kickstart -k com.example.dynamicwallpaper

结论

通过使用 Applescript 和 Launchctl,您已经创建了一个自定义动态壁纸,可以在一天中的不同时间展示不同的图像或动画。这为您提供了个性化桌面体验并反映您当前的心情或活动的机会。享受您的新动态壁纸,让您的 macOS 桌面变得生动起来!