Applescript 如何在不阻止查找程序的情况下监视文件夹的内容

Applescript 如何在不阻止查找程序的情况下监视文件夹的内容,applescript,desktop,finder,Applescript,Desktop,Finder,从下面的代码中可以看出,我对这一点非常陌生。我的代码基本上可以工作,但我的主要问题是它占用了Finder,有时它不会设置桌面图片,但大部分时间都会设置 该脚本只监视一个文件夹,如果添加了“***.jpg”,则会将桌面图片设置为该文件夹 这是我的第一个剧本,所以我有很多东西要学 set reset to "" display notification "Alarm Front Active " & (current date) as string tell application "Fi

从下面的代码中可以看出,我对这一点非常陌生。我的代码基本上可以工作,但我的主要问题是它占用了Finder,有时它不会设置桌面图片,但大部分时间都会设置

该脚本只监视一个文件夹,如果添加了“***.jpg”,则会将桌面图片设置为该文件夹

这是我的第一个剧本,所以我有很多东西要学

set reset to ""
display notification "Alarm Front Active  " & (current date) as string
tell application "Finder"
    set path_to_sourceFull to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
    set path_to_source to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
    set directory1 to "/Volumes/photo/FRONT CAM 1/20190929/images" as text -- from nsa310 network drive 
    set path_to_destinationFull to "Macintosh HD:Users:rekordbox:Documents:temp folder 2"
    set path_to_destination to ":Users:rekordbox:Documents:temp folder 2"
    set directory2 to "/Users/rekordbox/Documents/temp folder 2" as text

    repeat while reset = ""
        set allok to ""
        set filelist to name of every item in folder path_to_source --of startup disk       
        set listSizesaved to count of filelist
        delay 1

        repeat while allok = ""
            set filelist to name of every item in folder path_to_source --of startup disk           
            set listSize to count of filelist
            if listSize = listSizesaved then
            else
                set filelist to name of every item in folder path_to_source --of startup disk
                set listSize to count of filelist
                set LastAddedFile to item listSize of filelist
                set allok to "ALARM"
                set listSizesaved to listSize -- (save the updated) count               
                set activefile to (path_to_source & LastAddedFile)
                set selectedpicture to (directory1 & "/" & LastAddedFile)
                tell application "System Events" to tell every desktop to set picture to selectedpicture
                delay 1
                display notification "ALARM FRONT TRIGGERED...." & (current date) as string
                delay 1
            end if
        end repeat
    end repeat

end tell

我想,你想要的剧本是:

on adding folder items to thisFolder after receiving filelist
    set droppedFile to first item of filelist

    tell application "System Events"
        tell every desktop
            set picture to droppedFile
        end tell
    end tell
end adding folder items to
(我省略了“警报”部分,因为我不确定它的意义。)

要使用此脚本,请将其复制到脚本编辑器中,保存在文件夹
~/Library/Scripts/folder Action Scripts/
,然后打开小程序“folder Actions Setup”。在左侧添加所需文件夹,然后选择刚保存在右侧的文件。它应该是这样的: …其中左侧的复选标记显示已为文件夹(我称之为“测试文件夹”)启用文件夹操作,并且已附加脚本(我称之为“FADtop.scpt”)

将一个图像放到文件夹中,它就可以正常工作了

一般来说,除非绝对需要,否则不要编写查找器脚本;始终使用系统事件。Finder是一个繁忙的应用程序,编写脚本可能会破坏系统。并尽量避免这种设计模式:

(* Don't do this! *)
repeat 
    (* test for something *)
    delay x
end
delay
命令的资源效率不是特别高。如果您真的想使用轮询系统来测试某个事件,通常最好创建一个独立的应用程序,并在空闲时使用
处理程序。通过这种方式,您可以让系统唤醒和休眠脚本,从而显著提高性能

编辑

由于文件夹操作似乎不适用于ftp拖放到远程驱动器,因此这里有一种相当有效的文件夹轮询方法。将以下脚本另存为保持打开的应用程序(选择“应用程序”作为文件类型,然后单击“保持打开”复选框)。然后启动应用程序,让它在后台运行

property dateOfLastFileChosen : missing value
property targetFolder : "/Volumes/photo/FRONT CAM 1/20190929/images"
property idleTime : 300 -- 300 seconds is five minutes

on run
end run

on idle
    tell application "System Events"
        if exists folder targetFolder then
            if dateOfLastFileChosen is missing value then
                set recentFiles to every file of folder targetFolder whose visible is true
            else
                set recentFiles to every file of folder targetFolder whose modification date > dateOfLastFileChosen and visible is true
            end if

            set newFile to my mostRecentFileOfList(recentFiles)
            if newFile is not missing value then
                set dateOfLastFileChosen to modification date of newFile
                tell every desktop
                    set picture to (POSIX path of newFile)
                end tell
            end if
        end if
    end tell

    return idleTime -- check every 5 minutes (300 seconds)
end idle

on mostRecentFileOfList(fileList)
    set maxDateObj to missing value
    repeat with thisFile in fileList
        if maxDateObj is missing value then
            set maxDateObj to contents of thisFile
        else if modification date of thisFile is greater than modification date of maxDateObj then
            set maxDateObj to thisFile
        end if
    end repeat
    return maxDateObj
end mostRecentFileOfList

没有试图从@Ted Wrigley那里抢走风头,他们的解决方案为文件夹操作提供了AppleScript代码,我觉得有足够多的评论和条目可以添加到我的帖子中,作为OP困境的另一个答案

首先,我将在下面的AppleScript文件夹操作中处理
告诉每个桌面
将图片设置为droppedFile
行代码。如果用户仅将一个监视器/显示器连接到计算机,但创建了多个不同的“空间”,则
告诉每个桌面
将图片设置为droppedFile
代码行将仅更改当前活动“空间”桌面的桌面图片。其他桌面背景将不会更改。但是,如果用户将多个监视器/显示器连接到计算机,则
告诉每个桌面
将图片设置为droppedFile
代码行将更改每个连接监视器/显示器的当前活动“空间”桌面的桌面图片。如果后者不是所需的结果,则应将
告诉每个桌面
更改为
告诉当前桌面

在测试了@Ted Wrigley提供的AppleScript文件夹操作代码后,我注意到图像文件正在从FTP服务器下载到测试文件夹中,我将文件夹操作脚本附加到该文件夹中,在图像实际传输完成之前,看起来是这样的。因为文件在那里而不是那里,所以它不会触发文件夹操作

接下来,我想我会添加一个延迟作为文件夹操作代码的开始,以允许从FTP服务器传输图像文件,从而完成。我增加了180秒的延迟,以允许传输完成,并且工作正常。传输完成后,文件如下所示

根据您预计在任何给定时间传输的文件数量以及文件大小的因素。。。您可能需要显著增加延迟时间

on adding folder items to thisFolder after receiving theseFiles
    delay 180
    set newBackground to first item of theseFiles

    tell application "System Events"
        set picture of current desktop to newBackground -- Single Display Attached
        --set picture of every desktop to newBackground -- Multiple Displays Attached
    end tell
end adding folder items to

使用无限重复循环进行轮询非常昂贵。你很不愿意那样做。一个更好的方法是一个文件夹动作箭牌。。。。哇,看起来很棒,这正是我想要的。。所以我可以先说声谢谢。但是我遇到了一个问题,所以我希望你不介意给我多一点帮助。。当我在mac上使用文件夹时,您的脚本运行良好,但当我使用相同的文件夹并将其连接到网络上的驱动器时,脚本无法运行。。我曾经尝试过允许没有密码的Guest进行测试……据我所知,文件夹操作应该在网络驱动器上工作,但每当卸载网络驱动器时,它就会禁用文件夹操作(然后需要重新启动)。这是可以编写脚本的,但不幸的是,我没有网络驱动器可供测试。如果您的访问权限设置为“Guest”,则可能会遇到安全问题。使用launch agent可能更有效,但请先查看是否可以让文件夹操作工作。我可以看到文件夹操作设置左侧的文件夹名称更改为红色字体。我从未见过这种情况,但听起来不太乐观。这是什么样的份额,NAS?给我一天左右的时间,我会写一个替代方案(现在有点忙,对不起…)顺便问一下,你为什么要把drop文件夹放在远程驱动器上?为什么不把它放在你的本地卷上,然后把文件从网络拖到那里呢?摄像头直接记录到硬盘上,这样我就不得不开着我的mac电脑了。如果mac电脑被偷了,我就没有录音了。除非我把它送到云端,请随时偷我的雷声。我喜欢安静的生活。谢谢@wch1zpink,非常感谢您对发生的事情的解释。你的添加成功了。我一直在为此发愁,因为我永远不会放弃。现在一切都有意义了。我有很多东西要学,所以谢谢你和我