在applescript中嵌套重复的多个if条件

时间:2015-06-22 03:15:43

标签: applescript

我之前从未使用过AppleScript,所以我对这门语言并不熟悉,但我正在尽我所能。

这是我想要完成的事情: 选择填充.ARW和.JPG文件的文件夹时运行脚本。遍历文件夹中的项目。如果当前项为.ARW,则再次从头开始遍历文件夹。如果此嵌套迭代落在具有相同文件名和JPG扩展名的文件上,请用红色标记原始ARW文件。

TLDR:如果文件夹中的ARW文件与文件夹中的JPG文件共享相同的文件名,请用红色突出显示ARW文件,否则不执行任何操作。

这是我到目前为止编写的代码:

tell application "Finder"
    set totalAlias to the entire contents of (selection as alias)
    set totalCount to the count of items of totalAlias
    set firstName to name of item 1 of totalAlias
    set firstExtension to name extension of item 1 of totalAlias
    set c to 1

repeat while c ≤ totalCount
    set currentAlias to item c of totalAlias
    set currentName to name of currentAlias
    set currentExtension to name extension of currentAlias
    if currentExtension is "ARW" then
        set d to 1
        set compareFile to currentAlias
        set findName to currentName
        set findExtension to currentExtension

        repeat while d ≤ totalCount
            if (name of item d of totalAlias = findName) and (name extension of item d of totalAlias is "JPG") then
                tell application "Finder" to set label index of compareFile to 2

            end if
            set d to (d + 1)
        end repeat
    end if
    set c to (c + 1)
end repeat
end tell

对于出了什么问题的任何想法?我认为这与我的IF和条件有关。

1 个答案:

答案 0 :(得分:0)

试试这个脚本:

tell application "Finder"
    --Just get all the filenames of the target types:
    set allJPG to the name of every file of (entire contents of (selection as alias)) whose name extension = "JPG"
    set allARW to the name of every file of (entire contents of (selection as alias)) whose name extension = "ARW"
    --Send the two lists to a handler to find all the common names
    set targetJPGFiles to my CompareNames(allJPG, allARW)
    --Loop through the common names, find the files, set the tags
    repeat with eachTarget in targetJPGFiles
        set fileToTag to (item 1 of (get every file of (entire contents of (selection as alias)) whose name is (eachTarget as text)))
        set label index of fileToTag to 2
    end repeat
end tell

targetJPGFiles -- This allows you to see the filenames that SHOULD have been tagged

to CompareNames(jp, pn)
    --First, get rid of all the extensions in the ARW files
    set cleanARWNames to {}
    set neededJPGNames to {}
    repeat with eachARWName in pn
        set end of cleanARWNames to characters 1 thru -5 of (eachARWName as text) as text
    end repeat
    --Now, loop through JPG names to find a match
    repeat with eachjpgName in jp
        set searchName to characters 1 thru -5 of (eachjpgName as text) as text
        if cleanARWNames contains searchName then
            set end of neededJPGNames to (eachjpgName as text)
        end if
    end repeat
    return neededJPGNames
end CompareNames

它需要稍微不同的方法,因为它只是比较两个文件名列表,然后返回,找到具有您想要的名称的文件,并进行标记。

它基于我为另一个项目编写的脚本,所以我希望它适合你。

之前我从未在Finder中使用过label index属性,并且通过一些测试发现,在脚本运行后点击文件夹之前我看不到标签。但是,在我这样做之后,所有目标文件都有正确的标签。