AppleScript "list" 例子

分享于 

3分钟阅读

mac软件使用

  繁體

可以共享一些简单的macOS AppleScript list示例吗?

来看看一些常见的AppleScript list例子。

如何创建AppleScript list

可以像这样创建一个简单的AppleScript list:


set groceryList to {"eggs", "milk", "bread"}


这将创建一个AppleScript的groceryList对象,这个对象包含你在大括号中看到的 list。

如何使用AppleScript list

下面的示例演示如何从AppleScript list中检索一个项:


set myList to {"Problem", "There was a problem", "Bummer"}
set theChoice to item 2 of myList
say theChoice


下一个示例显示如何对 list(如何循环访问 list)中的每项执行一些操作:


set myList to {"Problem", "There was a problem", "Bummer"}
repeat with theItem in myList
 say theItem
end repeat


此示例演示如何从 list中获取随机项:


set myList to {"Problem", "There was a problem", "Bummer"}
set theChoice to some item of myList
say theChoice

使用'repeat '遍历iTunes list

下面是一个更复杂的示例,我将展示如何为 list中的每个项目执行一些操作,在本例中,它与我从iTunes检索的AppleScript list交互:


tell application "iTunes"
 activate
 set results to (every file track of playlist "Library" whose artist contains "Tony Bennett")
 repeat with t in results
 play t
 end repeat
end tell

在本例中,变量results包含从iTunes返回的 list。

Mac 使用'repeat '迭代 Finder列表


set listOfNames to {}
tell application "Finder"
 set filelist to every file of the desktop
 repeat with currentFile in filelist
 set currentFileName to (the name of currentFile)
 copy currentFileName to the end of listOfNames
 end repeat
end tell
choose from list listOfNames

AppleScript list说明

  • set filelist to every file of the desktop 使用Finder创建文件对象 list
  • set currentFileName to (the name of currentFile) 获取当前文件的名称部分
  • copy currentFileName to the end of listOfNames 将当前文件名追加到 list的末尾
  • choose from list listOfNames 在对话框中显示名称 list

lis  列表  Apple  Applescript  
相关文章