Applescript打开多个选项卡,执行一个简单的请求,然后命名它们

时间:2013-10-26 13:40:35

标签: tabs applescript rename

我只是学习脚本,我想要一个解决方案在一个窗口中打开13个选项卡,为每个窗口运行此选项,每个选项卡的值增加“telnet localhost 2001”...“telnet localhost 2013”​​,然后将每个标签R1..R1命名为SW1..SW4,然后命名为BB1..BB3。

这是第一和第三部分,但不是第二部分。

tell application "Terminal"  
activate  
tell application "System Events" to keystroke "n" using {command down}  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 1 of window 1 to "R1"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 2 of window 1 to "R2"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 3 of window 1 to "R3"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 4 of window 1 to "R4"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 5 of window 1 to "R5"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 6 of window 1 to "R6"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 7 of window 1 to "SW1"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 8 of window 1 to "SW2"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 9 of window 1 to "SW3"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 10 of window 1 to "SW4"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 11 of window 1 to "BB1"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 12 of window 1 to "BB2"  
tell application "System Events" to keystroke "t" using {command down}  
set custom title of tab 13 of window 1 to "BB3"  

end tell

它需要为值运行一个子程序,但我可以按照我想要的方式处理它。

任何人都可以帮我解决我的问题,因为添加了

do script ("telnet localhost 2001")
目前打开每个标签下的

会失败吗?

3 个答案:

答案 0 :(得分:0)

试试这个......

set shortDelay to 0.2
set tabTitles to {"R1", "R2", "R3", "R4", "R5", "R6", "SW1", "SW2", "SW3", "SW4", "BB1", "BB2", "BB3"}

tell application "Terminal"
    activate
    set frontWindow to window 1
    repeat with i from 1 to 13
        if i is not 1 then
            activate
            tell application "System Events" to keystroke "t" using command down
            delay shortDelay
        end if
        set custom title of tab i of frontWindow to (item i of tabTitles)
        set twoDigits to text -2 thru -1 of ("0" & i)
        do script ("telnet localhost 20" & twoDigits) in frontWindow
    end repeat
end tell

答案 1 :(得分:0)

这是我目前的,可怕的代码,但是完成了这项工作。注意我需要首先打开终端或者上面的错误,创建一个额外的" BB2"没有运行telnet脚本的选项卡,在某些选项卡中输入第二个脚本行,有些则没有。

如果有人可以提供帮助,我会感激不尽。

干杯。

告诉应用程序"终端" - 应用程序
    激活
    告诉应用程序"系统事件"击键" n"使用{command down}
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2001"在前窗的标签1中     做脚本" "在前窗的标签1中     做脚本" "在前窗的标签1中     做脚本" sh ip int bri"在前窗的标签1中     延迟0.1
    将窗口1的选项卡1的自定义标题设置为" R1"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2002"在前窗的标签2中     做脚本" "在前窗的标签2中     做脚本" "在前窗的标签2中     做脚本" sh ip int bri"在前窗的标签2中     做脚本" "在前窗的标签2中     延迟0.1
    将窗口1的选项卡2的自定义标题设置为" R2"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2003"在前窗的标签3中     做脚本" "在前窗的标签3中     做脚本" "在前窗的标签3中     做脚本" sh ip int bri"在前窗的标签3中     做脚本" "在前窗的标签3中     延迟0.1
    将窗口1的选项卡3的自定义标题设置为" R3"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2004"在前窗的标签4中     做脚本" "在前窗的标签4中     做脚本" "在前窗的标签4中     做脚本" sh ip int bri"在前窗的标签4中     做脚本" "在前窗的标签4中     延迟0.1
    将窗口1的选项卡4的自定义标题设置为" R4"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2005"在前窗的标签5中     做脚本" "在前窗的标签5中     做脚本" "在前窗的标签5中     做脚本" sh ip int bri"在前窗的标签5中     做脚本" "在前窗的标签5中     延迟0.1     将窗口1的选项卡5的自定义标题设置为" R5"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2006"在前窗的标签6中     做脚本" "在前窗的标签6中     做脚本" "在前窗的标签6中     做脚本" sh ip int bri"在前窗的标签6中     做脚本" "在前窗的标签6中     延迟0.1
    将窗口1的选项卡6的自定义标题设置为" R6"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2007"在前窗的标签7中     做脚本" "在前窗的标签7中     做脚本" "在前窗的标签7中     做脚本" sh ip int bri"在前窗的标签7中     做脚本" "在前窗的标签7中     延迟0.1
    将窗口1的选项卡7的自定义标题设置为" SW1"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2008"在前窗的标签8中     做脚本" "在前窗的标签8中     做脚本" "在前窗的标签8中     做脚本" sh ip int bri | e un"在前窗的标签8中     做脚本" "在前窗的标签8中     延迟0.1
    将窗口1的选项卡8的自定义标题设置为" SW2"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2009"在前窗的标签9中     做脚本" "在前窗的标签9中     做脚本" "在前窗的标签9中     做脚本" sh ip int bri | e un"在前窗的标签9中     做脚本" "在前窗的标签9中     延迟0.1
    将窗口1的选项卡9的自定义标题设置为" SW3"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2010"在前窗的标签10中     做脚本" "在前窗的标签10中     做脚本" "在前窗的标签10中     做脚本" sh ip int bri | e un"在前窗的标签10中     做脚本" "在前窗的标签10中     延迟0.1
    将窗口1的选项卡10的自定义标题设置为" SW4"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2011"在前窗的标签11中     做脚本" "在前窗的标签11中     做脚本" "在前窗的标签11中     做脚本" sh ip int bri | e un"在前窗的标签11中     做脚本" "在前窗的标签11中     延迟0.1
    将窗口1的选项卡11的自定义标题设置为" BB1"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2012"在前窗的标签12中     做脚本" "在前窗的标签12中     做脚本" "在前窗的标签12中     做脚本" sh ip int bri"在前窗的标签12中     做脚本" "在前窗的标签12中     延迟0.1     将窗口1的选项卡12的自定义标题设置为" BB2"
    告诉应用程序"系统事件"击键" t"使用{command down}
    做脚本" telnet localhost 2013"在前窗的标签13中     做脚本" "在前窗的标签13中     做脚本" "在前窗的标签13中     做脚本" sh ip int bri"在前窗的标签13中     做脚本" "在前窗的标签13中     延迟0.1
    将窗口1的选项卡13的自定义标题设置为" BB3"

告诉

答案 2 :(得分:0)

由于终端需要首先打开,在运行工作设置命令之前,只需先将cmd添加到顶部打开终端,作为解决方法!

tell application "Terminal" set newTab to do script -- create a new window with no initial command set current settings of newTab to settings set "Grass" end tell

tell application "Terminal" -- the app activate tell application "System Events" to keystroke "n" using {command down} tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2001" in tab 1 of front window do script " " in tab 1 of front window do script " " in tab 1 of front window do script "sh ip int bri " in tab 1 of front window delay 0.1 set custom title of tab 1 of window 1 to "R1" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2002" in tab 2 of front window do script " " in tab 2 of front window do script " " in tab 2 of front window do script "sh ip int bri " in tab 2 of front window do script " " in tab 2 of front window delay 0.1 set custom title of tab 2 of window 1 to "R2" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2003" in tab 3 of front window do script " " in tab 3 of front window do script " " in tab 3 of front window do script "sh ip int bri " in tab 3 of front window do script " " in tab 3 of front window delay 0.1 set custom title of tab 3 of window 1 to "R3" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2004" in tab 4 of front window do script " " in tab 4 of front window do script " " in tab 4 of front window do script "sh ip int bri " in tab 4 of front window do script " " in tab 4 of front window delay 0.1 set custom title of tab 4 of window 1 to "R4" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2005" in tab 5 of front window do script " " in tab 5 of front window do script " " in tab 5 of front window do script "sh ip int bri " in tab 5 of front window do script " " in tab 5 of front window delay 0.1 set custom title of tab 5 of window 1 to "R5" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2006" in tab 6 of front window do script " " in tab 6 of front window do script " " in tab 6 of front window do script "sh ip int bri " in tab 6 of front window do script " " in tab 6 of front window delay 0.1 set custom title of tab 6 of window 1 to "R6" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2007" in tab 7 of front window do script " " in tab 7 of front window do script " " in tab 7 of front window do script "sh ip int bri " in tab 7 of front window do script " " in tab 7 of front window delay 0.1 set custom title of tab 7 of window 1 to "SW1" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2008" in tab 8 of front window do script " " in tab 8 of front window do script " " in tab 8 of front window do script "sh ip int bri | e un" in tab 8 of front window do script " " in tab 8 of front window delay 0.1 set custom title of tab 8 of window 1 to "SW2" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2009" in tab 9 of front window do script " " in tab 9 of front window do script " " in tab 9 of front window do script "sh ip int bri | e un" in tab 9 of front window do script " " in tab 9 of front window delay 0.1 set custom title of tab 9 of window 1 to "SW3" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2010" in tab 10 of front window do script " " in tab 10 of front window do script " " in tab 10 of front window do script "sh ip int bri | e un" in tab 10 of front window do script " " in tab 10 of front window delay 0.1 set custom title of tab 10 of window 1 to "SW4" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2011" in tab 11 of front window do script " " in tab 11 of front window do script " " in tab 11 of front window do script "sh ip int bri | e un" in tab 11 of front window do script " " in tab 11 of front window delay 0.1 set custom title of tab 11 of window 1 to "BB1" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2012" in tab 12 of front window do script " " in tab 12 of front window do script " " in tab 12 of front window do script "sh ip int bri " in tab 12 of front window do script " " in tab 12 of front window delay 0.1 set custom title of tab 12 of window 1 to "BB2" tell application "System Events" to keystroke "t" using {command down} do script "telnet localhost 2013" in tab 13 of front window do script " " in tab 13 of front window do script " " in tab 13 of front window do script "sh ip int bri " in tab 13 of front window do script " " in tab 13 of front window delay 0.1 set custom title of tab 13 of window 1 to "BB3"end tell