1

Background

I am using a combination of AppleScript and Bash to create a program that launches a set of commands in Terminal.app then closes that window when it terminates.

terminal.sh:

#!/bin/bash # Usage: # terminal Opens the current directory in a new terminal window # terminal [PATH] Open PATH in a new terminal window # terminal [CMD] Open a new terminal window and execute CMD # terminal [PATH] [CMD] Opens a new terminal window @ the PATH and executes CMD # # Example: # terminal ~/Code/HelloWorld ./setup.sh [ "$(uname -s)" != "Darwin" ] && { echo 'Mac OS Only' return } function terminal() { local cmd="" local wd="$PWD" local args="$*" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if [ -d "$1" ]; then wd="$1" args="${*:2}" fi if [ -n "$args" ]; then cmd="$args" fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - osascript <<EOF tell application "Terminal" activate tell window 1 do script "cd $wd ; $cmd" repeat while busy delay 1 end repeat my close() end tell end tell on close() activate application "Terminal" delay 0.5 tell application "System Events" tell process "Terminal" keystroke "w" using {command down} end tell end tell end close EOF } terminal "$@" 

Issue

Currently, the AppleScript does not close the window when the script itself completes.

1
  • 1
    @user3439894 Very good point! Deleted my comment (and upvoted your excellent answer). Commented Jan 20, 2020 at 18:58

1 Answer 1

1

I copied and pasted your code into an empty file, saved it and made it executable.

I then ran it in Terminal and received the following error:

 175:183: syntax error: A command name can’t go after this “my”. (-2740) 

I then changed the name of the close() handler to closeWindow() and the script worked as desired.

Note that close is a built-in AppleScript command and cannot be used as the name of a handler

Sign up to request clarification or add additional context in comments.

8 Comments

Is there a way to do this without using System Events because this requires accessibility permissions? I was thinking about modifying com.apple.terminal shellExitAction to 0 instead of 2 which closes the window on exit, but it doesn't actually update the preferences when default write com.apple.terminal shellExitAction -int 0 for the current terminal window.
@Nicholas Adamou, I'm running macOS High Sierra and do not have Terminal or System Events in Accessibility permissions and it works fine for me in that it is actually closing the window it opened after the command passed to the executable runs. That said, the shellExitAction key is not a global Terminal setting but one at the profile level, so if you setting it e.g. defaults write com.apple.terminal shellExitAction -int 0 it's not going to work. If you make this change from the Terminal > Preferences… it works immediately.
Yeah, I am running the latest version of macOS 10.15.2 Catalina and it will ask for permissions upon doing it this way. I am trying to find a way to do this without having to manually open Terminal > Preferences > Profiles > Shell. Even running sudo defaults write com.apple.terminal shellExitAction -int 0 doesn't affect it because the preferences are cached apparently.
@Nicholas Adamou, Caching isn't the immediate issue as defaults write com.apple.terminal shellExitAction -int 0 is not valid because shellExitAction is not a global key in Terminal, it's set under the Window Settings key under the name of the profile key. In some cases, using ; killall cfprefsd after a properly formed defaults write command will force the change to update. You may have to use /usr/libexec/PlistBuddy to change/set the value of a nested key. It probably also can be done with defaults but getting the syntax for a multi-level nested key isn't easy
@Nicholas Adamou, In other words, if you remove everything from the output of defaults read com.apple.terminal not relevant to the shellExitAction key, its hierarchy is, e.g., { "Window Settings" = { "Basic" = { shellExitAction = 2; }; }; } not { shellExitAction = 2 }, the latter would be a global key in Terminal but the shellExitAction key is not a global key in Terminal. { "Default Window Settings" = "Basic"; } is a global key.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.