大纲:如何在AppleScript中返回列表项的名称并将列表与另一个列表进行匹配?
- 我创建的所有正在运行的应用(
activeProcesses
) - 我想它要匹配的文本编辑器的预定义列表(
appList
) - 我
activeProcesses
是从应用程序的列表appList
创建一个新的列表(activeEditorList
)=>第1个问题 - 我想配多少文本编辑从
activeEditorList
运行在activeProcesses
- 匹配名单是我第二个问题
- 输出或取消
- 如果只在文本编辑器从
appList
运行我想返回它的名字 - 如果不止一个文本编辑器正在运行,提示我从中选择一个
activeEditorList
- 如果没有运行取消
- 如果只在文本编辑器从
这里是我的脚本的第一部分:创建一个新的列表了所有正在运行的应用程序。拼图的最后一部分缺失。我的输出中写道:
{item 1 of {"nvALT", "FoldingText", "Byword"}, item 2 of {"nvALT", "FoldingText", "Byword"}}`
,而不是 {"nvALT", "iTerm"}
这里是我的脚本:
set activeEditorsList to {}
set appList to {"nvALT", "FoldingText", "Byword"}
-- Generate variable with running apps
tell application "System Events"
set activeProcesses to (name of every process)
end tell
-- Generate list of running text editors
repeat with appName in appList
if (activeProcesses contains appName) is true then
set activeEditor to appName
copy appName to the end of activeEditorsList
end if
end repeat
return activeEditorsList
问题的探析1:线12 set activeEditor to appName
不工作,我的修正唐徒劳的尝试”不管工作。
当activeEditorsList
变好了,我想再次使用它,以找出是否只有一个文本编辑器中运行(=>返回名称)或一个以上运行(=>提示选择一个从该列表并返回其名称)。
这就是我想象中的匹配的样子:
set appList to {"nvALT", "FoldingText", "Byword"}
set activeEditorsList to {"nvALT", "Byword"}
on count_matches(this_list, this_item)
set the match_counter to 0
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then ¬
set the match_counter to the match_counter + 1
end repeat
return the match_counter
end count_matches
count_matches(appList, (items of activeEditorsList))
问题的探析2:要问题,上面的代码是,在最后一行,我希望它的工作(items of activeEditorsList)
不起作用 - 虽然把"nvALT"
在那里工作。
然后我需要写沿线的东西... if count_matches 0 then cancel, elif 1 return name, else prompt to choose from list
。
我还不知道如何匹配计数,但我想我可以弄明白(随着时间的推移)。另一方面,提示是我已经准备好的一件小事:
set activeEditorsList to {"nvALT", "Byword"}
tell me to set selectedEditor to choose from list activeEditorsList
return selectedEditor as text
我会感谢一些指导。
'匹配列表是我的第二个问题'。你应该把第二个问题分解成第二个问题。 –
@ darrick-herwehe我也是这么想的,但是可能会有人更聪明,他们认为接近我的主要目标完全不同。然后,我会错过一些聪明的细节,并更深入地了解AppleScript的世界。 – pattulus