AppleScript的“谁的”条款

AppleScript因易于阅读而难以写入而获得饶舌这有点值得称赞但问题不在于语言本身本身,而在于不同应用程序脚本字典的语法变幻莫测AppleScript语言本身相当小,实际上提供了一些非常简洁的功能。

其中之一是a的概念过滤参考, 用一个谁的条款。

例如,使用BBEdit,您可以获得前窗中每个单词的列表,如下所示:

tell application "BBEdit"
   tell window 1
      set my_list to every word 
   end tell
end tell

But what if we only want the words that contain the letter “z”? In most programming languages, the way you’d tackle this would go something like this:

  1. 创建一个列表(a.k.a所有单词的数组。
  2. 循环遍历每个单词,一次一个。
  3. Check if the current word contains the letter “z”; if so, append it to a second list.

使用AppleScript谁的但是,我们可以一次完成所有这些,如下所示:

tell application "BBEdit"
   tell window 1
      set my_list to every word whose characters contains "z"
   end tell
end tell

或者,如果您只想要以“z”开头的单词:

tell application "BBEdit"
   tell window 1
      set my_list to every word whose first character is "z"
   end tell
end tell

您可以使用谁的在任何可编程脚本的应用程序中创建过滤引用的子句例如,在Mac OS X Finder中,您可以获取Documents文件夹中以“A”开头的所有项目的列表:

tell application "Finder"
   set f to folder "Documents" of home
   set my_list to every item of f whose name starts with "A"
end tell

或者使用BBEdit的创建者代码在Documents文件夹中的每个文件:

tell application "Finder"
   set f to folder "Documents" of home
   set my_list to every file of f whose creator type is "R*ch"
end tell

说得好谁的条款下周工作。