Networking 使用applescript搜索文件夹

Networking 使用applescript搜索文件夹,networking,applescript,directory,finder,Networking,Applescript,Directory,Finder,我正在尝试在服务器上创建一个文件夹,人们可以将照片添加到其中,然后脚本将其发送到正确的位置,但是我在搜索部分遇到了问题 正如您在下面的代码中看到的,它找到文件夹发送到哪里的部分被注释掉了,因为我不知道它的语法是什么 任何帮助都将不胜感激 global theWatchedFolder set theWatchedFolder to choose folder on idle tell application "Finder" set theDetectedItems to every i

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

正如您在下面的代码中看到的,它找到文件夹发送到哪里的部分被注释掉了,因为我不知道它的语法是什么

任何帮助都将不胜感激

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

另外,我担心的是,如果这个脚本在服务器上,监视服务器上的文件夹,那么它不会为任何向服务器上的文件夹添加文件的人创建弹出窗口。希望我是错的,但如果有人能以这样或那样的方式证实这一点,那就太棒了。谢谢:)

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

我还有一个补充说明,您可以使用空闲处理程序持续运行AppleScript,以检查特定文件夹中的任何更新。您知道AppleScript作为一个保持打开的应用程序存在内存泄漏吗?这是您不希望在服务器上持续运行的软件。最好每隔一段时间(我更喜欢每小时一次)在新流程中启动一个新的AppleScirpt,然后退出当前正在运行的流程。您仍然可以使用空闲处理程序,但如果空闲处理程序每10秒运行一次,我将退出此脚本并在600次循环后启动一个新的脚本

然后回到你的搜索。Finder没有搜索命令。自从Mac OS X Tiger Apple推出spotlight以来,它是一种用于查找不同类型数据(文件、捆绑包、邮件等)的元数据库。但是,spotlight从来都不可编写脚本,但对于AppleScript,只能使用
mdls
mdfind
mdutil
在命令行上访问。要在命令行上执行命令,我们使用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 path,可以在任何地方使用posix文件作为路径前缀

但是,您提到必须在服务器上调用搜索。正常情况下,如果服务器安装正确,共享将位于应用程序和用户主文件夹之外。默认情况下,这些文件夹仅由spotlight索引,因此spotlight需要动态索引。换句话说,与普通的聚光灯搜索相比,它的速度非常慢,搜索时间不到一秒钟。因此,我建议使用与上面相同的脚本的另一个版本,但使用
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

注意:一个副作用是,当元数据搜索工作不同时,“查找”将尝试每个文件。现在,您可能会找到更多文件,因为搜索中还包括文件夹。就像
findMetaDataInFolderByName()
findFileInfolderByName()
将返回posix路径。

即使是指向另一个网站的链接告诉我如何操作也足够了,我只需要让这段代码正常工作:(这不是我真正想听的,但至少我现在知道了。我只是希望在我告诉老板我可以整理之前我就知道了:/Epicly详细的回答,不过,你绝对值得赏金:)据我所知,你想在文件夹中添加一张照片,并询问用户(客户)将照片添加到服务器时。您可以装载不可见的网络共享(只需将其装载到/Volume目录之外)。然后使脚本可拖放,以便用户可以将照片拖放到脚本上。然后请求作业编号,然后复制不可见的文件这不是你想要的,而是你最终想要的。