automator terminal doScript command problem

Hi, I am using ScriptEditor with javascript.


var app = Application('Terminal');

app.doScript("cd ~/Downloads;clear;");
app.activate();


It is work fine.

but i want to open the command to exist Terminal window's tab. so i put the code this:


var app = Application('Terminal'),
  frontWin = app.windows[0],
  frontTab = frontWin.tabs[-1];

frontTab.push(new app.Tab())
// Error -1700: Can't convert types.

frontTab.doScript("cd ~/Downloads;clear;");
// Error on line 8: Error: Named parameters must be passed as an object
app.activate();


But it is not work. please help me fix the problem.


Thanks.

Accepted Reply

You need to specify the tab you want to use, after checking that there is a window (or getting the tab will fail):


var app = Application('Terminal')
app.activate
try {
  currentWindow = app.windows.at(0) // is there a window?
  currentTab = currentWindow.selectedTab()
  app.doScript("echo testing", {in: currentTab}) }
catch(err) {
  console.log(err) // no window
  app.doScript("echo 'test new window'");  }

Replies

You need to specify the tab you want to use, after checking that there is a window (or getting the tab will fail):


var app = Application('Terminal')
app.activate
try {
  currentWindow = app.windows.at(0) // is there a window?
  currentTab = currentWindow.selectedTab()
  app.doScript("echo testing", {in: currentTab}) }
catch(err) {
  console.log(err) // no window
  app.doScript("echo 'test new window'");  }

Tanks for your help.


Do you know how to open command with new Tab in the exist Teminal window ?


I found in Stackoverflow http://stackoverflow.com/questions/27451735/open-new-terminal-tab-with-os-x-javascript-for-automation

but there no saitisfied answer.

On my system the keystroke command in Terminal doesn't work correctly, and the make command doesn't work correctly, either (also mentioned in your posted link). Sorry, but the Terminal scripting support looks like it is poorly executed. The JXA Apple Event implementation is also crap for anything more than the simplest commands, so be prepared for a lot of frustration.

Thanks.


There is my finally code:

function commandExistWindow(command) {
    var Terminal = Application('Terminal')
    Terminal.activate()
    try {
        currentWindow = Terminal.windows.at(0)
        currentTab = currentWindow.selectedTab()
        Terminal.doScript(command, {
            in: currentTab
        })
    } catch(err) {
        // console.log(err) // no window 
        Terminal.doScript(command);
    }
}
var currentFolderPath = getCurrentFinderFolderPath()
var cmd = `tmux -2 -u new-window -c "${currentFolderPath}"`
commandExistWindow(cmd)