【问题标题】:How do I check if a folder contains a certain type of file with AppleScript?如何使用 AppleScript 检查文件夹是否包含某种类型的文件?
【发布时间】:2013-04-18 22:20:05
【问题描述】:

第一次编写applescripting,我已经编写了一个脚本,该脚本创建了一个.nfo 文件,其中包含基于文件夹名称指向电影的IMDB 链接。

目前,脚本设置为向用户询问包含所有电影的文件夹,并在该文件夹中循环,直到所有子文件夹都已处理完毕。

结构是:

  • 电影
    • 电影 1
      • 视频文件
      • 电影 1.nfo
    • 电影 2
      • 视频文件
      • 电影2.nfo
    • 电影 3
      • 视频文件
      • 电影 3.nfo

等等……

我需要帮助添加一个 if-then-else 语句,该语句首先检查 nfo 文件是否已经存在,如果存在,只需增加计数器以移动到下一个文件夹或运行我当前的脚本代码.

我尝试过类似的东西

if (theFiles contains files whose name extension is nfo) then
    i = i + 1
else
    -- My Code
end if

它似乎并不关心,只是再次处理所有文件夹,用新的 nfo 替换当前的 nfo。

当前 AppleScript:

property imdburl : ""
property newurl : ""
property FolderPath1 : ""
property FolderPath : ""
property Foldername : ""

tell application "Finder"
    activate
    set theFolder to choose folder with prompt "Select the Movie Folder."
    set theList to every folder of theFolder
    repeat with i from 1 to count the theList
        set thisItem to item i of theList as alias
        set currentName to name of thisItem
        --set currentfolder to get the POSIX path of thisItem
        set theFiles to every file of folder thisItem as alias list

        if (theFiles contains files whose name extension is nfo) then
            i = i + 1
        else
            tell application "Finder"
                set FolderPath1 to thisItem -- sets file path to folder you select
                set ParentFolder to container of FolderPath1 -- sets the parent folder of the folder you select
                set Foldername to name of folder FolderPath1 -- sets the folder name as text
                set FolderPath to get the POSIX path of FolderPath1
                set newurl to "http://google.com/search?q=" & Foldername & "+imdb&btnI=I%27m+Feeling+Lucky"
                --set the clipboard to newurl -- sets foldername to the clipboard
            end tell

            tell application "Safari"
                open location newurl
                activate
                delay 3
                set imdburl to URL of current tab of window 1
                set the clipboard to FolderPath
                close window 1
            end tell

            tell application "TextEdit"
                activate
                make new document
                set text of document 1 to imdburl
                set Foldername to text 1 thru -(7 + 1) of Foldername
                save document 1 in FolderPath & "/" & Foldername
                close document 1
            end tell

            tell application "Finder" to set name extension of (files of FolderPath1 whose name extension is "rtf") to "nfo"
        end if
    end repeat
end tell

【问题讨论】:

    标签: file applescript


    【解决方案1】:

    这个例子展示了如何检查文件夹中的应用程序包(只是为了便于测试)。将 .app 替换为 .ifo 即可获得所需的内容。

    on foo()
        tell application "Finder"
            set someFolder to choose folder
            set aList to name of files of someFolder as list
            set AppleScript's text item delimiters to return
            return ".app" is in (aList & return as text)
        end tell
    end foo
    
    on run {}
        foo()
    end run
    

    请注意,与通常所说的相反,重复循环并不是获取文件夹文件列表的必要条件。

    【讨论】:

    • 必须将此作为分支点才能使 if 语句正常工作currentName if (currentnfo & ".nfo" is in (aList)) then i = i + 1 else
    • 请注意,这种方法不适用于隐藏的 Finder 项目,例如.git 文件夹,除非显式更改 Finder 配置以显示此类项目。
    猜你喜欢
    • 2014-10-29
    • 2012-10-02
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多