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

嵌套中的多个if条件在applescript中重复,applescript,Applescript,我以前从未使用过AppleScript,所以我对这门语言很不熟悉,但我已经尽力了 以下是我试图实现的目标: 在选择填充有.ARW和.JPG文件的文件夹时运行脚本。遍历文件夹中的项目。如果当前项为.ARW,请从头开始再次遍历文件夹。如果此嵌套迭代位于具有相同文件名和JPG扩展名的文件上,请使用红色标记原始ARW文件 TLDR:如果文件夹中的ARW文件与文件夹中的JPG文件共享相同的文件名,请以红色突出显示ARW文件,否则不执行任何操作 以下是我迄今为止编写的代码: tell application

我以前从未使用过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和条件有关。

试试这个脚本:

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中使用过
标签索引
属性,并且通过一些测试发现,在脚本运行后单击文件夹之前,我无法看到标签。但是,在我这样做之后,所有目标文件都有正确的标记。

尝试以下脚本:

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中使用过
标签索引
属性,并且通过一些测试发现,在脚本运行后单击文件夹之前,我无法看到标签。不过,在我这么做之后,所有的目标文件都有正确的标签。

嘿,克雷格,非常感谢!我修改了您的脚本以突出显示ARW而不是JPG,它工作得非常好。我想我唯一剩下的问题是,为什么脚本要花这么长时间才能执行(对于40个ARW,在3Ghz i7 MBPro上大约需要3分钟)?Applescript中是否存在固有的慢度?本例中的慢度是由
整个内容
命令造成的。用于向下搜索所选文件夹中的所有子文件夹。如果没有子文件夹,则可以使用的每个文件(选择项作为别名),这样可以加快速度。其他编剧认为,每当你使用
告诉应用程序“Finder”
块时,你都需要在它运行时制作一个三明治。嘿,克雷格,非常感谢!我修改了您的脚本以突出显示ARW而不是JPG,它工作得非常好。我想我唯一剩下的问题是,为什么脚本要花这么长时间才能执行(对于40个ARW,在3Ghz i7 MBPro上大约需要3分钟)?Applescript中是否存在固有的慢度?本例中的慢度是由
整个内容
命令造成的。用于向下搜索所选文件夹中的所有子文件夹。如果没有子文件夹,则可以使用的每个文件(选择项作为别名),这样可以加快速度。其他脚本编写者认为,每当你使用
告诉应用程序“Finder”
块时,你都需要在它运行时制作一个三明治。