为Find Any File添加AppleScript支持时的枚举重叠问题解决问询
解决Find Any File AppleScript中重叠枚举的方案
问题背景
为Find Any File添加AppleScript功能时遇到枚举冲突:
type是枚举类型,包含textual、numerical、date等值verb为整数,其含义随type动态变化:- 当
type为textual时:0→contains、1→equals、2→matches regex - 当
type为date时:0→equals、1→is later than、2→is earlier than
- 当
需要让verb可通过AppleScript优雅访问,但直接定义单一<enumeration>会因equals重复导致编译器无法区分,且希望避免字符串解析、笨拙的前缀命名等方案,目标写法为:
make new search rule name equals "xyz"
或
make new search rule with properties {noun: name, verb: equals, argument: "xyz"}
可行解决方案
方案1:上下文感知的枚举分组(平衡实现复杂度与兼容性)
将verb按type拆分为多个枚举类型,在AppleScript字典中分别定义:
enumeration type TextualVerb contains textual equals matches regex end enumeration enumeration type DateVerb date equals is later than is earlier than end enumeration
同时将search rule的verb属性设为variant类型,允许接受不同组的枚举值。在应用逻辑中,根据type的值校验verb是否属于对应枚举组。
脚本示例:
make new search rule with properties {noun: name, type: textual, verb: textual equals, argument: "xyz"} make new search rule with properties {noun: creation date, type: date, verb: date equals, argument: "2024-01-01"}
- 优点:编译器能明确区分枚举,实现逻辑简单
- 缺点:枚举名带前缀,与GUI显示的“equals”不完全一致,但可通过字典注释说明对应关系
方案2:术语别名+运行时上下文推断(最贴近理想写法)
在AppleScript字典中给重复的枚举项设置共享别名,同时在应用的AppleScript处理逻辑中,通过noun或type推断verb的上下文:
- 字典中定义:
enumeration type TextualVerb contains equals -- 别名textual equals matches regex end enumeration enumeration type DateVerb equals -- 别名date equals is later than is earlier than end enumeration
- 在处理
make new search rule命令时:- 先通过
noun确定对应的type(比如name对应textual,creation date对应date) - 再根据
type将verb的枚举值映射为对应整数
- 先通过
脚本示例(完全符合理想写法):
make new search rule name equals "xyz" make new search rule creation date equals "2024-01-01"
- 优点:用户写法简洁直观,与GUI显示完全一致
- 缺点:需要在应用端实现上下文推断逻辑,复杂度较高,需处理
noun与type的映射关系
方案3:预定义常量记录(兼顾简洁性与类型安全)
定义包含type和verb的record类型,并预封装常用组合为常量:
- 字典中定义:
record type VerbSpec type: TypeEnumeration verb: integer end record constant textual contains : {type:textual, verb:0} constant textual equals : {type:textual, verb:1} constant date equals : {type:date, verb:0} constant date is later than : {type:date, verb:1}
- 将
search rule的verb属性设为VerbSpec类型
脚本示例:
make new search rule with properties {noun: name, verb: textual equals, argument: "xyz"} make new search rule with properties {noun: creation date, verb: date is later than, argument: "2024-01-01"}
- 优点:编译器能识别预定义常量,写法简洁,无需用户手动构造record,同时保证类型安全
- 缺点:常量名仍带前缀,但比方案1更直观,且用户无需关注内部整数映射
总结
- 追求极致用户体验:优先选择方案2,虽然实现复杂,但完全符合理想写法
- 平衡实现成本与体验:选择方案3,预定义常量兼顾简洁性与类型安全
- 快速实现兼容:选择方案1,枚举分组逻辑简单,适合快速迭代
内容的提问来源于stack exchange,提问作者Thomas Tempelmann
相关产品推荐
相关产品推荐

