大綱:如何在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