繁体   English   中英

使用applescript搜索文件夹

[英]Search for a folder using applescript

我试图将文件夹放置在服务器上,人们可以向其中添加照片,然后脚本将它们发送到正确的位置,但是我在搜索部分遇到了麻烦。

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

任何帮助将不胜感激。

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

另外,我担心如果此脚本在服务器上,正在监视服务器上的文件夹,那么它将不会为向服务器上的文件夹添加文件的任何人创建弹出窗口。 希望我是错的,但是如果有人能够以一种或另一种方式确认这一点,那将是很棒的。 谢谢 :)

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

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

然后返回您的搜索。 Finder没有搜索命令。 自从Mac OS X Tiger Apple引入聚光灯以来,这是一个元数据库,用于查找不同类型的数据(文件,文件包,邮件等)。 但是,Spotlight从来没有编写脚本,但是对于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

注意:因为我们在外壳中工作,所以返回的路径是posix路径,可以在任何位置使用posix文件作为前缀

但是,您已经提到必须在服务器上调用搜索。 通常,如果正确安装了服务器,则共享位于应用程序和用户主文件夹的外部。 默认情况下,这些文件夹仅由Spotlight索引,因此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

注意:副作用是因为在元数据搜索工作不同的情况下find会尝试每个文件,您现在可能会发现更多文件,因为搜索中还包含文件夹。 就像findMetaDataInFolderByName()findFilesInFolderByName()将返回posix路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM