2014-04-01 59 views
1

大纲:如何在AppleScript中返回列表项的名称并将列表与另一个列表进行匹配?

  1. 我创建的所有正在运行的应用(activeProcesses
  2. 我想它要匹配的文本编辑器的预定义列表(appList
  3. activeProcesses是从应用程序的列表appList创建一个新的列表(activeEditorList=>第1个问题
  4. 我想配多少文本编辑从activeEditorList运行在activeProcesses
    • 匹配名单是我第二个问题
  5. 输出或取消
    • 如果只在文本编辑器从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 

我会感谢一些指导。

+0

'匹配列表是我的第二个问题'。你应该把第二个问题分解成第二个问题。 –

+0

@ darrick-herwehe我也是这么想的,但是可能会有人更聪明,他们认为接近我的主要目标完全不同。然后,我会错过一些聪明的细节,并更深入地了解AppleScript的世界。 – pattulus

回答

0

添加第二个答案来解决整个脚本,而不仅仅是单个问题。

对于脚本的第一部分,您只需要获取当前正在运行的预定义应用程序的列表。您已经拥有该部分:

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 appName is in activeProcesses then 
     set end of activeEditorsList to appName 
    end if 
end repeat 

对于第二部分,您不需要匹配任何内容。你已经用activeEditorsList完成了。你只需要找出你有多少匹配,并从那里工作,使用AppleScript的count命令

editorCount = (count activeEditorsList) 
if editorCount = 1 then 
    return item 1 of activeEditorsList 
else if editorCount > 1 then 
    return choose from list activeEditorsList 
else 
    -- Handle 0 items 
end if 

作为一个侧面说明,对于0编辑的情况下,这听起来像这个代码是在子常规。你可能要么抛出一个错误,要么返回一个空类型的值(null,missing value,""),并取消调用该例程的脚本。

+0

感谢Darrik,这确实使得匹配无用,看起来比我想象的要简单得多。我不得不定义'编辑器Count',并用'set editorCount来替换它以计算activeEditorsList'的工作。不过......我想到的是将结果传递给另一个应用程序。因此,我的输出/结果应该是项目x,c.f的名称。 '“nvALT”'不是''nvALT“,”FoldingText“,”Byword“,”Mou“}'的第1项......这将我们带回到问题1中。几乎在那里,确实知道一种方法,项目参考? – pattulus

+0

好吧,明白了:'将activeEditorsList的item 1作为文本返回 – pattulus

0

(按我的意见,我将只解决你的第一个问题)

我不知道什么行不通约set activeEditor to appName。您将变量activeEditor的值设置为应用程序的名称。这对我有用。 (我也做了一些肤浅的编辑代码。)

set activeEditorsList to {} 
set appList to {"Sublime Text 2"} 

tell application "System Events" 
    set activeProcesses to (name of every process) 
end tell 

repeat with appName in appList 
    if appName is in activeProcesses then 
     set activeEditor to appName 
     set end of activeEditorsList to appName 
    end if 
end repeat 
activeEditor 
--> {item 1 of {"Sublime Text 2"}} 

如果你想真正激活的编辑器,这是不这样做的方式。同样,你只是分配给一个变量。

相关问题