如何在AppleScript中获取每个项目的多个属性
AppleScript 获取文件名称与关联注释的简便方法
原代码可获取桌面文件夹中所有文件的注释:
tell application "Finder" set targ to (path to desktop folder) as alias get comment of every item of targ end tell
若想同时获取每个文件的名称与注释,直接使用get {name, comment} of every item of targ会得到两个独立列表(分别是所有名称和所有注释),无法对应到单个文件。
简便解决方案
通过循环遍历每个文件项,将单个文件的名称和注释打包为记录并收集到列表中:
tell application "Finder" set targ to (path to desktop folder) as alias set itemList to every item of targ set resultList to {} repeat with anItem in itemList set end of resultList to {file_name:name of anItem, file_comment:comment of anItem} end repeat end tell
执行后resultList中的每个元素都是包含对应文件名称和注释的记录,可直接通过file_name和file_comment字段访问单个文件的信息。
另一种更紧凑的写法:
tell application "Finder" set targ to (path to desktop folder) as alias set resultList to {} repeat with anItem in (get every item of targ) set end of resultList to {name:name of anItem, comment:comment of anItem} end repeat end tell
说明
AppleScript的every item集合操作在处理多属性时,会按属性维度聚合结果而非按个体维度,因此直接批量获取多属性会得到分离的列表。通过显式循环逐个处理每个文件项,才能保证名称和注释的对应关系。
内容的提问来源于stack exchange,提问作者Thomas Tempelmann
相关产品推荐
相关产品推荐

