2017-08-22 45 views
1

我想寫一個函數來創建一個選項卡並使用Apple腳本運行一個命令。Apple腳本來創建一個新標籤來運行bash命令


我已經試過

tell application "Terminal" 
    activate 

    my makeTab("desktop", "ls") 

end tell 

on makeTab(name, command) 

    do shell script command 

    tell application "System Events" 
     keystroke "t" using {command down} 
     delay 0.2 
     keystroke "i" using {shift down, command down} 
     keystroke tab 
     keystroke name 
     key code 53 

    end tell 
end makeTab 

結果

我一直得到

sh: eCmd: command no found

enter image description here

對我來說任何提示,我該如何使它工作?

回答

2

這是一個終端衝突,namecommand是AppleScript的保留條款。

您不能將這些術語用作處理程序的參數。


檢查這些變量的顏色。

  • 變量的顏色必須是綠色
  • :這是一個屬性或常量
  • :這是一個命令或一類

因此,更改這些變量的名稱。

如果你想運行在新選項卡的命令,使用do script命令,不do shell script,像這樣:

tell application "Terminal" 
    activate 
    my makeTab("desktop", "ls") 
end tell 

on makeTab(tName, tCommand) 
    tell application "System Events" 
     keystroke "t" using {command down} 
     delay 0.2 
     keystroke "i" using {shift down, command down} 
     keystroke tab 
     keystroke tName 
     key code 53 
    end tell 
    tell application "Terminal" 
     do script tCommand in front window -- run the command in the new tab 
    end tell 
end makeTab 
+0

當我運行你所建議的腳本'終端得到一個錯誤,我得到這個小錯誤: AppleEvent處理程序失敗。「任何想法? – ihue

+0

嘗試在'key code 53'命令 – jackjr300

+0

之後用'delay 0.3'命令在腳本中添加一行。好的。謝謝。 – ihue

相關問題