我正在尝试制作一个苹果脚本,让我将桌面图片更改为硬盘驱动器上文件夹中的随机图片
tell application "Finder"
set desktopPictures to folder "Path:To:Desktop:Pictures:"
set fileList to name of every file of desktopPictures
set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
set fileName to item theNum of fileList
set desktop picture to file fileName in desktopPictures
end tell
到目前为止,一切正常,我遇到的唯一问题是当我连接另一台显示器时,他的桌面图片不会改变。 我尝试用我在网络搜索中发现的以下代码来解决这个问题
tell application "Finder"
set desktopPictures to folder "Path:To:Desktop:Pictures:"
set fileList to name of every file of desktopPictures
set theDesktops to a reference to every desktop
repeat with aDesktop in theDesktops
set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
set fileName to item theNum of fileList
set picture of aDesktop to file fileName in desktopPictures
end repeat
end tell
但是这段代码无法编译,因为我收到语法错误:
Expected class name but found property.
第 4 行突出显示 desktop
您在找到的代码中省略了告诉应用程序“系统事件”块。
在 Applescript 中,某些命令存在于特定应用程序的字典中,必须使用“告诉应用程序”块来引用。在这种情况下,“每个桌面”调用位于“系统事件”应用程序中。
试试这个。
tell application "Finder"
set desktopPictures to folder "Path:To:Desktop:Pictures:"
set fileList to name of every file of desktopPictures
tell application "System Events"
set theDesktops to a reference to every desktop
end tell
repeat with aDesktop in theDesktops
set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
set fileName to item theNum of fileList
set picture of aDesktop to file fileName in desktopPictures
end repeat
end tell
这对我在 Mojave 使用 3 个显示器(每个显示器有 8 个桌面)有效。获得监视器列表后,它使用任务控制来访问每个桌面,然后将其图片设置为用户图片文件夹中的随机 jpeg。语音部分可以删除,这只是为了告知,因为它是一个演示。使用键盘命令进行任务控制切换比 shell 脚本更快,但并非每个人都具有相同的键盘设置,因此 shell 脚本被设置为默认值。它以一个很好的警告开始,因为它改变了东西。如果您在运行时单击任何内容,它就会停止。
tell application "System Events" to set MonitorQTY to count of every desktop
set msg to "Your total number of monitors is " & MonitorQTY
say msg without waiting until completion
display dialog msg buttons {"Cancel", "Assign RANDOM wallpaper to each desktop!"} with icon 0
----set MissionControlToggle----
set toggleMissionControl to "do shell script \"/Applications/Mission\\\\ Control.app/Contents/MacOS/Mission\\\\ Control\" " & linefeed & "delay 1"--any keyboard
#set toggleMissionControl to "tell application \"System Events\" to (key code 126 using control down)" & linefeed & "delay 1" -- depends function keys
----count desktopspaces----
run script toggleMissionControl --on.
tell application "System Events" to tell application process "Dock" to tell group "Mission Control"
set DesktopNMBR to 0
repeat with MonitorNMBR from 1 to MonitorQTY
set dsktpBTNS to (every UI element of list 1 of group "Spaces Bar" of group MonitorNMBR) -->list of desktops as buttons.
set dsktpQNTY to count of dsktpBTNS
say "Monitor " & MonitorNMBR & " has, " & dsktpQNTY & " desktop spaces." without waiting until completion
repeat with eachDesktopButton in dsktpBTNS
click eachDesktopButton --causes mission control to close.
set DesktopNMBR to DesktopNMBR + 1
say "This is desktop " & DesktopNMBR without waiting until completion
----set random wallpaper----
say "assigning random wallpaper" without waiting until completion
set rndmWALLPAPER to some item of (paragraphs of (do shell script "mdfind -onlyin ~/Pictures/ '.jpeg' "))
tell application "System Events" to set picture of desktop MonitorNMBR to rndmWALLPAPER
-----------------------------
run script toggleMissionControl --on again.
end repeat
end repeat
say "Your total number of desktops is, " & DesktopNMBR without waiting until completion
end tell
----exit----
run script toggleMissionControl --off
say "Process complete."