2012-11-01 53 views
3

我有一個應用程序,同時打開幾個窗口。 我想帶一個特定的窗口前景(我知道它的標題)。Applescript - 使窗口前景

目前我正在使用組合鍵來完成這項任務,但我想嘗試一些不同的方法,因爲我遇到了這種方法的一些問題。

tell application "System Events" 
    set frontmost of process "appIT" to true 
    keystroke "1" using command down 
    delay 0.2 
end tell 

回答

5

如果您的應用程序編寫腳本,並允許設置一個窗口的指數,你可以做以下(基於How do I make a Safari window active using AppleScript (elegantly)?答案)

to raiseWindow of theApplicationName for theName 
    tell the application named theApplicationName 
     activate 
    set theWindow to the first item of ¬ 
     (get the windows whose name is theName) 
    if index of theWindow is not 1 then 
      set index to 1 
      set visible to false 
      set visible to true 
     end if 
    end tell 
end raiseWindow 

知名度的觸發是必要處理伴隨着切換應用程序出現的一些奇怪現象。如果您不切換可見性,那麼當您切換離開並返回到應用程序時,該窗口不會成爲第一個。不幸的是,這種切換將窗口縮小到碼頭,然後恢復它,這是一個非常戲劇性的UI中斷。

這裏的另一種方式,我發現要處理的怪事:

to raiseWindow2 of theApplicationName for theName 
    tell the application named theApplicationName 
     activate 
    set theWindow to the first item of ¬ 
     (get the windows whose name is theName) 
     if the index of theWindow is not 1 then 
      set the index of theWindow to 2 
     tell application "System Events" to ¬ 
      tell application process theApplicationName to ¬ 
       keystroke "`" using command down 
     end if 
    end tell 
end raiseWindow2 
0

我不認爲系統事件可以改變進程的前窗。當然,您可以關閉前窗,直到您想要的窗口處於頂部。這不是一個真正的解決方案,因爲你可能不想關閉窗口。真的,只有當應用程序本身是Apple腳本並允許你這樣做時,唯一的辦法就是實現這一點。

14

這可以通過使用"AXRaise"操作來實現,但某些窗口(例如使用X11的應用程序)除外。

試試這個。

set theTitle to "some title" 
tell application "System Events" 
    tell process "appIT" 
     set frontmost to true 
     perform action "AXRaise" of (windows whose title is theTitle) 
    end tell 
end tell 
+0

美麗!它至少可以爲我的應用程序預期。謝謝。 – KingBOB

+1

在OS X Yosemite上,此腳本的執行給了我錯誤(不能只複製粘貼,因爲我有本地化的錯誤消息):系統事件出錯:無法配置進程「appIT」爲true。自2012年以來有所改變? –

+0

@AntonKoval'執行窗口1的動作「AXRaise」在優勝美地爲我工作 – ling