Automator – Launch terminal at certain folder

automatorterminal

Is there a way in Automator to open the terminal application at a certain folder location with 2 tabs at that location?

So for example open terminal with 2 tabs that have already changed directory from the root folder to Documents/Websites

Best Answer

Terminal can be finicky beast when it comes to automation; however, with the use of UI Scripting the following example AppleScript code can be used in Script Editor and saved as an application, or in Automator in a Run AppleScript action.

Note that using UI Scripting may require accessibility privileges be granted.

This was tested and works under macOS High Sierra.

do shell script "open -a 'Terminal' ~/Documents/Websites"
tell application "Terminal" to activate
tell application "System Events"
    keystroke "t" using {command down}
    keystroke tab using {control down, shift down}
end tell

The example AppleScript code above will open a new Terminal window to the specified location and then open a new tab to the same location while setting focus back to the first tab.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.