使用applescript搜索文件夹

时间:2014-06-11 15:42:14

标签: networking applescript directory finder

我正在尝试在服务器上创建一个文件夹,人们可以添加照片,然后脚本将它们发送到正确的位置,但是我在搜索部分遇到问题。

正如你在我的代码中看到的那样,它找到发送文件夹的位置的部分被注释掉了,因为我不知道它的语法是什么。

非常感谢任何帮助。

global theWatchedFolder
set theWatchedFolder to choose folder
on idle
tell application "Finder"
    set theDetectedItems to every item of theWatchedFolder
    repeat with aDetectedItem in theDetectedItems
        set jobNumber to display dialog "Please enter the job number for this photo." buttons {"Submit", "Cancel"}
        display dialog "File detected: " & jobNumber
        --tell finder
        -- search for jobNumber in (path to desktop)
        --set jobFolder to top search result
        --end tell
        --set colourFolder to jobfolder & /colour
        move aDetectedItem to the desktop --move to colourFolder
    end repeat
end tell
if theDetectedItems is not {} then
    activate
    display dialog "test move complete"
end if
return 1
 end idle

另外,我担心如果这个脚本在服务器上,观察服务器上的文件夹,那么它就不会为任何向服务器上的文件夹添加文件的人创建弹出窗口。希望我错了,但如果有人能够确认这种方式或其他方式,那将是非常棒的。谢谢:))

1 个答案:

答案 0 :(得分:1)

我可以证实你最大的恐惧。显示对话框显示在目标Finder中。除非您使用远程事件,否则您将始终在运行脚本的同一台计算机上寻址Finder。如果脚本在服务器上运行,则对话框将显示在服务器上运行的Finder中。

我还有一个附注,即您使用空闲处理程序持续运行AppleScript以检查特定文件夹中的任何更新。您是否知道AppleScript作为保持打开的应用程序存在内存泄漏?它不是您想要在服务器上不断运行的软件。最好偶尔在一个新的过程中启动一个新的AppleScirpt(我更喜欢每小时一样)并退出当前正在运行的那个。您仍然可以使用空闲处理程序,但如果空闲处理程序每​​10秒运行一次,我将退出此脚本并在600次循环后启动一个新脚本。

然后回到你的搜索。 Finder没有搜索命令。由于Mac OS X Tiger Apple引入了聚光灯,这是一个用于查找不同类型数据(文件,捆绑,邮件等)的元数据库。然而,聚光灯从来没有可编写脚本,但AppleScript只能在命令行上使用mdlsmdfindmdutil访问。要在命令行上执行命令,我们使用AppleScript中的do shell script命令或do script命令来编写Terminal.app脚本。这是一个如何使用do shell脚本命令

的示例
set theFolder to choose folder
set searchKey to "the*" --use * as wild card 

findMetaDataInFolderByName(theFolder, searchKey)

on findMetaDataInFolderByName(HFSPath, searchKey)
    set options to " -onlyin " & quoted form of POSIX path of HFSPath
    set options to options & " \"kMDItemFSName == " & quoted form of searchKey & "\""
    return paragraphs of (do shell script "mdfind " & options)
end findMetaDataInFolderByName

注意:因为我们在shell中工作,返回的路径是posix路径,可以在任何地方使用前缀为posix文件的路径

但是,您已经提到必须在服务器上调用搜索。通常,当您正确安装服务器时,共享位于应用程序和用户主文件夹之外。默认情况下,这些文件夹仅由聚光灯索引,因此聚光灯需要动态索引。换句话说,与普通聚光灯搜索相比,它在不到一秒的时间内完成得非常慢。所以我建议使用与find相同的脚本的另一个版本。 Find将以递归方式遍历给定目录并打印每个匹配项。

set theFolder to choose folder

set searchKey to "the*" --use * as wild card
findFilesInFolderByName(theFolder, searchKey)

on findFilesInFolderByName(HFSPath, searchKey)
    --the HFSPath can't have a trailing "/"
    set UFSPath to POSIX path of HFSPath
    if UFSPath ends with "/" and UFSPath is not "/" then set UFSPath to text 1 thru -2 of UFSPath
    set options to space & quoted form of UFSPath
    set options to options & " -iname " & quoted form of (searchKey) --iname is case insensitive file name match
    paragraphs of (do shell script "find " & options & " -print 2>/dev/null ; exit 0") --pipe error to /dev/null to exclude permission denied messages
end findFilesInFolderByName

注意:副作用是因为当元数据搜索工作不同时,find会尝试每个文件,因为文件夹也包含在搜索中,所以现在可能会找到更多文件。与findMetaDataInFolderByName()一样,findFilesInFolderByName()将返回posix路径。