From 972cd5514f86305f5cf3e00a16ad506e2dde277f Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 6 Sep 2018 12:42:49 +0200 Subject: [PATCH] macOS: Reduce clutter shown when opening terminal By writing the needed special commands to a temporary file. What we cannot get rid of is the command for opening a bash that sources that file. Since Terminal usually opens a login shell, but we cannot set a special file for sourcing in that case, the special commands include mimicking the behavior of a login shell by reading the corresponding config files. This is in preparation to setting up the environment for the shell. Since we do not start a new process for the Terminal on macOS, we will need to explicitly export the whole environment after the fact, resulting in potentially dozens of export commands to be executed. Change-Id: Ia24cf1f00e62411734f5d6514d073e11d4cdae6e Reviewed-by: Tobias Hunger --- share/qtcreator/scripts/openTerminal.py | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/share/qtcreator/scripts/openTerminal.py b/share/qtcreator/scripts/openTerminal.py index 5aabf74cf53..3ee0dc652cc 100755 --- a/share/qtcreator/scripts/openTerminal.py +++ b/share/qtcreator/scripts/openTerminal.py @@ -29,20 +29,15 @@ import os import pipes import subprocess import sys +from tempfile import NamedTemporaryFile -def quote_for_applescript(arg): +def quote_applescript(arg): return arg.replace('\\', '\\\\').replace('"', '\\"') -def quote(arg): - return quote_for_applescript(pipes.quote(arg)) +def quote_shell(arg): + return pipes.quote(arg) -def quote_args(args): - return [quote(arg) for arg in args] - -def cd_cwd_arguments(): - return ['cd', os.getcwd()] - -def apple_script(shell_script): +def apple_script(shell_command): return ''' --Terminal opens a window by default when it is not running, so check on applicationIsRunning(applicationName) @@ -63,13 +58,21 @@ tell application "Terminal" set currentWindow to first window whose tabs contains currentTab activate end tell -'''.format(shell_script) +'''.format(shell_command) def main(): - args = quote_args(cd_cwd_arguments()) + [';'] + quote_args(sys.argv[1:]) - shell_script = ' '.join(args) - osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE) - osascript_process.communicate(apple_script(shell_script)) + # create temporary file to be sourced into bash that deletes itself + with NamedTemporaryFile(delete=False) as shell_script: + quoted_shell_script = quote_shell(shell_script.name) + commands = ('cd ' + quote_shell(os.getcwd()) + '\n' + + ' '.join([quote_shell(arg) for arg in sys.argv[1:]]) + '\n' + + 'rm ' + quoted_shell_script + '\n' + ) + shell_script.write(commands) + shell_script.flush() + shell_command = quote_applescript('source ' + quoted_shell_script) + osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE) + osascript_process.communicate(apple_script(shell_command)) if __name__ == "__main__": main()