如何检查文件夹是否包含AppleScript的某种类型的文件?

时间:2013-04-18 22:20:05

标签: file applescript

第一次使用applescripting,我已经整理了一个脚本,该脚本根据文件夹名称创建一个包含电影IMDB链接的.nfo文件。

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

结构是:

    • 电影1
      • 视频文件
      • movie 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。

当前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

1 个答案:

答案 0 :(得分:0)

此示例显示如何检查文件夹中的应用程序包(只是为了便于测试)。用.ifo替换.app,你就拥有了你想要的东西。

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

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