2014-04-01 61 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"}} 

如果你想真正激活的編輯器,這是不這樣做的方式。同樣,你只是分配給一個變量。

相關問題