簡體   English   中英

在AppleScript中識別終端窗口

[英]Identifying Terminal window in AppleScript

我嘗試在運行ssh時更改終端更改配置文件。 為此,我編寫了此腳本(並定義了一個alias以便ssh可以運行它):

#!/bin/bash

osascript -e "tell application \"Terminal\" to set current settings \
of front window to first settings set whose name is \"AmadanRemote\""

/usr/bin/ssh "$*"

osascript -e "tell application \"Terminal\" to set current settings \
of front window to first settings set whose name is \"AmadanLocal\""

這幾乎可以滿足我的要求。 (它在同一窗口中繪制連續的選項卡是錯誤的,因為配置文件顯然是整個窗口的,但是我不使用選項卡。)問題是,如果在另一個“終端”窗口位於頂部時關閉連接,則AmadanLocal配置文件將應用於錯誤的窗口

因此,產生了一個問題:是否有任何方法可以通過終端的tty設備或任何其他功能(而不是易變的front window )來明確標識終端窗口或標簽?

如果不是,則第一個osascript調用可以返回任何標識特征,以明確標識第二個osascript調用中的相同窗口/選項卡?

(不必是AppleScript-如果JavaScript可以實現,JavaScript也可以。)

編輯:如果有人感興趣,腳本的最終形狀是:

#!/bin/bash

tty=`tty`

osascript <<EOF
tell application "Terminal"
    set W to the first window whose tty of tab 1 is "$tty"
    set T to tab 1 of W
    set the current settings of T to the first settings set whose name is "AmadanRemote"
end tell
EOF

/usr/bin/ssh "$*"

osascript <<EOF
tell application "Terminal"
    set W to the first window whose tty of tab 1 is "$tty"
    set T to tab 1 of W
    set the current settings of T to the first settings set whose name is "AmadanLocal"
end tell
EOF

獎金:實際上,每個選項卡都做正確的事! <3

就在這里。 腳本編輯器中 ,我可以運行以下命令:

tell application "Terminal" to get {properties, properties of tab 1} of window 1

這將為我提供前窗及其活動選項卡的所有屬性。 這是輸出:

{
    {
    selected tab:tab 1 of window id 15491 of application "Terminal",
    closeable:true,
    size:{550,777},
    zoomed:false,
    frame:{730,0,1280,777},
    index:1,
    visible:true,
    position:{730,23},
    class:window,
    origin:{730,0},
    name:"~ — fish  /Users/CK — ttys001",
    miniaturizable:true,
    frontmost:false,
    id:15491, <---------------------------------------① 
    miniaturized:false,
    resizable:true,
    bounds:{730,23,1280,800},
    zoomable:true
    },
    {
    font:"mononoki-Regular",
    title displays device name:true,
    cursor color:{64587,609,65480},
    current settings:current settings of tab 1 of window id 15491 of application "Terminal",
    title displays shell path:false,
    tty:"/dev/ttys001", <-----------------------------② 
    normal text color:{52428,52428,52427},
    title displays window size:false,
    title displays custom title:true,
    contents:"Last login: Mon Jun 18 04:54:37 on ttys001\nCK@CK-mac ~> ",
    row:39,
    process:{
        "login",
        "-fish"
        },
    clean commands:{
        "screen",
        "tmux"
        },
    font antialiasing:true,
    background color:{16383,16383,16383},
    title:"fish  /Users/CK",
    class:tab,
    title displays file name:false,
    history:"Last login: Mon Jun 18 04:54:37 on ttys001\nCK@CK-mac ~> ",
    selected:true,
    size:16,
    bold text color:{65535,65535,65535},
    busy status:false,
    column:60
    }
}

該輸出的前半部分是窗口的屬性,而輸出的后半部分是同一窗口中的選項卡的屬性。

我用箭頭①和②標記了音符的兩個屬性。

屬性①屬於窗口,它是窗口的唯一id 這將在窗口的整個生命周期中固定不變。 因此,在腳本中的某個時刻,將id of the front windowid of the front window存儲在變量中,並通過該變量引用窗:

tell application "Terminal"
    set wID to the id of the front window
    set visible of window id wID to false
end tell

其實,你不必與引用打擾id在所有如果你選擇的實際存儲window對象在一個變量,而不是它的id

tell application "Terminal"
    set W to the front window
    set visible of W to false
end tell

由於AppleScript通過窗口的id值引用了window對象,因此這樣做同樣可靠且不變。

從窗口的可用屬性列表中,我看不到current settings屬性。 但是,該選項卡有current settings屬性。 我知道您不使用選項卡,但是在AppleScript中, Terminal的每個選項卡實際上僅屬於一個窗口,處於一種與直覺相反的選項卡-窗口關系。

因此,您期望包含三個選項卡的窗口應具有一個單個window對象和三個tab對象的AppleScript引用。 通過索引引用它們(可以更改,因此不是在代碼中引用它們的好方法),您會期望會有tab 1 of window 1 tab 2 of window 1 tab 3 of window 1 相反,您得到的是tab 1 of window 1 tab 1 of window 2 tab 1 of window 3

我不知道為什么要這樣實現。 但是,從本質上講, tab對象和window對象似乎是指您在屏幕上看到的相同的框架界面,希望將其稱為window ,而實際上出於某些目的它可能是tab

current settings是您可能想為window進行設置而實際上是為tab設置的時候之一。

如您tty ,選項卡具有一個屬性tty ,它是一個字符串值,用於標識選項卡使用的終端。 在我的情況下,它是"/dev/ttys001" 但是,正如我剛才解釋的那樣,從AppleScript奇怪的角度來看,每個window對象實際上只包含一個tab對象。 因此,如果對包含它的窗口進行引用,則引用該標簽很簡單:它始終是tab 1 of window id wID tab 1 of Wtab 1 of W ,但是您之前存儲了變量。

但是屬於該選項卡的tty屬性可用於引用其包含的窗口,這意味着,如果您知道自己所處的終端,則始終可以在正確的window標識並引用正確的tab 您可以這樣做:

tell application "Terminal"
    set W to the first window whose tty of tab 1 contains "ttys001"
    set T to tab 1 of W
    set the current settings of T to the first settings set whose name is "AmadanLocal"
end tell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM