如何在applescript中获取桌面列表 [英] How to get a list of the desktops in applescript

查看:35
本文介绍了如何在applescript中获取桌面列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个applescript,让我将桌面图片更改为硬盘驱动器文件夹中的随机图片

I'm trying to make an applescript that let's me change the desktop picture to a random picture in a folder on my hard drive

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

到目前为止它运行良好,我唯一的问题是当我连接另一台显示器时,他的桌面图片不会改变.我尝试用 我在进行网络搜索时发现的以下代码

So far it works perfectly, the only issue I have is when I connect another monitor, his desktop picture won't change. I tried solving this problem with the following code I found making a web search

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

但是这段代码无法编译,因为我收到一个语法错误:

But this code won't compile as I get a syntax error saying:

需要类名但找到属性.desktop 突出显示在第 4 行

Expected class name but found property. With desktop highlighted on row 4

推荐答案

您在找到的代码中省略了 tell 应用程序系统事件"块.

You have omitted the tell application "System Events" block from the code you found.

在 Applescript 中,某些命令存在于特定应用程序的字典中,必须用告诉应用程序"块引用.在这种情况下,每个桌面"调用位于系统事件"应用中.

In Applescript some commands exist in the dictionary of specific applications and must be referenced with a 'tell application' block. In this case the 'every desktop' call is in the "System Events" app.

试试这个.

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

这篇关于如何在applescript中获取桌面列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆