AppleScript:检查现有项目的列表
我不知道为什么这个脚本不起作用。我有近2000个条目(类别)的列表,但有很多重复。我只是想创建一个唯一类别的列表,但我似乎无法正常工作。
背景:我正在阅读一个CSV文件,该文件具有标题为:类别的列。我读取文件,使用newline定界符创建每个条目的数组,循环循环段落,再次由逗号分隔符,并获取类别列的内容。这些条目可以是单个类别或以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
看起来像这样的日志,最终在ARRCATEGIOR中有成千上万的条目(我在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*)
我确定这只是简单的东西,但我无法弄清楚为什么它没有拿起它重复。任何帮助将不胜感激。
I can't figure out why this script is not working. I have a list of almost two thousand entries (categories) but there are many duplicates. I'm just trying to create a list of unique categories but I can't seem to get it to work.
Background: I am reading a CSV file that has a column titled: CATEGORIES. I read the file, use a newline delimiter to create an array of each entry, loop through the array, delimit again by the comma separator and get the content for the CATEGORIES column. Those entries could be a single category or multiple entered as CAT1; CAT2
or just to be more annoying CAT1 > CAT2
. Here's my code, I am ignoring the third instance of categories that can be returned (with the >
symbol) for now until I get the code working.
...
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
The log looks like this, eventually there are thousands of entries in arrCategories (I have roughly 1000 line in the CSV to loop over)
(*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*)
I am sure it is just something simple I am missing but I cannot figure out why it is not picking up the duplicates. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在优胜美地或以后的系统上,您可以使用AppleScript目标C避免重复循环C:
我测试了以下脚本:
On Yosemite or later systems you can avoid repeat loops using AppleScript Objective C:
I tested following script:
在使用y 重复语句形式的x重复
时,循环变量x实际上是对列表y中的项目的引用。取决于您的操作,其内容可能不会被删除。 。
如果尝试做类似的事情,例如将循环变量与文本进行比较,则比较将失败,因为它将是参考本身而不是其价值。为了确保您使用的是实际值,您可以获取循环变量的
内容
,或者在使用它之前将其胁到所需的类,例如:When using the
repeat with X in Y
form of the repeat statement, the loop variable X is actually a reference to an item in the list Y. Depending on what you are doing, its contents may not get dereferenced.If trying to do something like compare the loop variable to text, the comparison will fail because it will be to the reference itself rather than its value. To be sure you are using the actual value, you can get the
contents
of the loop variable, or coerce it to the desired class before working with it, for example: