AppleScript:检查现有项目的列表

AppleScript: Checking a list for an existing item

我不明白为什么这个脚本不起作用。我有一个包含近两千个条目(类别)的列表,但有很多重复项。我只是想创建一个独特类别的列表,但我似乎无法让它发挥作用。

背景:我正在读取一个 CSV 文件,其中有一列标题为:CATEGORIES。我读取了文件,使用换行符为每个条目创建一个数组,遍历数组,再次用逗号分隔符分隔并获取 CATEGORIES 列的内容。这些条目可以是单个类别或多个输入 CAT1; CAT2 或者只是为了更烦人 CAT1 > CAT2。这是我的代码,我暂时忽略可以返回的类别的第三个实例(使用 > 符号),直到我让代码工作。

...
set arrCategories to {}

set theCats to item 14 of arrThisLine
set oatd to AppleScript's text item delimiters
set AppleScript's text item delimiters to ";"
set subCats to every text item of theCats
        
repeat with thisSubCat in subCats
    if thisSubCat does not contain ">" then
        if arrCategories contains thisSubCat then
        else
            copy thisSubCat to end of arrCategories
            log arrCategories
        end if
    end if
end repeat
set AppleScript's text item delimiters to oatd

日志看起来像这样,最终在 arrCategories 中有数千个条目(我在 CSV 中有大约 1000 行要循环)

(*Design*)
(*Design, Design*)
(*Design, Design, Design*)
(*Design, Design, Design, Design*)
(*Design, Design, Design, Design, Revenue*)
(*Design, Design, Design, Design, Revenue, Learning & Development*)
(*Design, Design, Design, Design, Revenue, Learning & Development,  Product & Engineering*)
(*Design, Design, Design, Design, Revenue, Learning & Development,  Product & Engineering, Product & Engineering*)

我确信这只是我遗漏的一些简单的东西,但我无法弄清楚为什么它不拾取重复项。任何帮助将不胜感激。

当使用 repeat with X in Y 形式的 repeat 语句时,循环变量 X 实际上是对列表 Y 中某项的引用。根据您的操作,其内容可能不会被取消引用。

如果尝试执行诸如将循环变量与文本进行比较之类的操作,比较将会失败,因为它将与引用本身而不是其值进行比较。为确保您使用的是实际值,您可以获取循环变量的 contents,或在使用它之前将其强制为所需的 class,例如:

repeat with thisSubCat in subCats
   set thisSubCat to thisSubCat as text
   --

在 Yosemite 或更高版本的系统上,您可以使用 AppleScript 避免重复循环 Objective C:

use AppleScript version "2.4" -- Yosemite or later
use scripting additions
use framework "Foundation"

-- .... INSERT HERE THE BEGINNING OF YOUR SCRIPT

set theCats to item 14 of arrThisLine
set oatd to AppleScript's text item delimiters
set AppleScript's text item delimiters to ";"
set subCats to every text item of theCats
set AppleScript's text item delimiters to oatd

-- remove items with ">"
set stringArray to current application's NSArray's arrayWithArray:subCats
set thePred to current application's NSPredicate's predicateWithFormat:"!self  LIKE '*>*'"
set bList to (stringArray's filteredArrayUsingPredicate:thePred) as list

-- remove duplicates
set aSet to current application's NSOrderedSet's orderedSetWithArray:bList
set arrCategories to (aSet's array()) as list

我测试了以下脚本:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theCats to {"Design", "Design", "Design > Design", "Revenue", "Learning & Development", "Product & Engineering", "Product & Engineering"}

-- remove duplicates, retaining list's order
set aSet to current application's NSOrderedSet's orderedSetWithArray:theCats
set aList to (aSet's array()) as list

-- remove strings with ">"
set stringArray to current application's NSArray's arrayWithArray:aList
set thePred to current application's NSPredicate's predicateWithFormat:"!self  LIKE '*>*'"
set arrCategories to (stringArray's filteredArrayUsingPredicate:thePred) as list

--> {"Design","Revenue","Learning & Development","Product & Engineering"}