Avoid the terminal process to close, after BBEdit runs a script

I use BBEdit to write and test Python scripts, and I usually run the scripts in a terminal process, with the "Run in Terminal" command. Is there a way to have the terminal process opened by BBEdit stay "active", after the script is completed? I would like not to have the

[Process completed] 

message and instead to still have an active prompt, either the shell or the the Python environment, with all the variables I have created in the script still existing. This would be similar to the situation when I launch the script from an existing terminal process, either from the shell:

$ python script_name.py

or from the python interpreter:

>>> script_name.py

In particular, in the last case, when the script exits, I still have the process active in the terminal window.


Solution 1:

As I understand, you have a Python script in BBEdit.

Python script

You choose to test the code using the "Run in Terminal" option from the '#!' menu.

Run in Terminal

Now a Terminal window opens, runs the script and exits.

exit code

Now here is the culprit. BBEdit does not only run your script, but also adds an exit. There is no option in BBEdit to remove this exit command. But BBEdit is highly scriptable and there are some workarounds. You can create a BBEdit Script (from the Scripts menu) that runs, but personally I think creating Service is the easiest one (run some AppleScript that opens your script in the Terminal as you would have done manually).

In this treat you have provided a script that actually does what you want.

Solution 2:

Building on the answer of CousinCocaine, and on this answer to an old question I posted myself on the BBEdit forum, I have come to this AppleScript, which is specific for Python:

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# modif Fabio Grazioso
# dCre: 2015/09/22 11:00
# dMod: 2017/10/03 18:40 
# Appl: BBEdit, Terminal
# Task: Attempt to run the front text document in Terminal.app.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Run, @Front, @Document, @Terminal, @Python
-------------------------------------------------------------------------------------------

tell application "BBEdit"
    tell front text document
        if on disk = false then error "Front document is not saved!"
        if modified = true then save
        if contents of line 1 does not start with "#!" then error "No valid shebang line found!"
        set docName to its name
        set docFile to POSIX path of (get its file)
    end tell
end tell

set shCMD to text 2 thru -1 of "
FILE=" & docFile & ";
if [[ ! -x \"$FILE\" ]]; then
  chmod +x \"$FILE\";
fi
"
do shell script shCMD

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set docName to quoted form of docName
set docParentPath to quoted form of ((text items 1 thru -2 of docFile) as text)
set AppleScript's text item delimiters to oldTIDS

tell application "Terminal"
    activate
    if name of windows = {missing value} then do script
    if processes of front window = {} then do script


    tell front window
        if its busy = true then
            do script "cd " & docParentPath & " && python -i " & docName
        else
            do script "cd " & docParentPath & " && python -i " & docName in selected tab
        end if
    end tell
end tell

-------------------------------------------------------------------------------------------

I find this answer better than the one proposed by CousinCocaine just because I can create a keyboard shortcut to the script (AFAIK it is not possible to associate a shortcut to a service).

The steps to follow for this to work are as follows:

  1. Copy the script code in the Script Editor.app (found in the /Applications/Utilities/ folder)
  2. Compile the script (hammer icon on the Editor bar)
  3. Save it in the BBEdit scripts folder: /Users//Library/Application\ Support/BBEdit/Scripts/
  4. Optional In BBEdit associate the script to a keyboard shortcut, in the Preferences -> Menus & Shortcuts -> Scripts (you have to click to the right of the script's name, where it says "none", and press your shortcut)

Here is a screenshot of the preferences' pane

  1. Finally, you create a script in BBEdit, e.g. a python script, you save it, and while it is the front window in BBEdit, you select the AppleScript from BBEdit's Scripts menu. This will send the python script to the Terminal and the script will be executed.

About the AppleSript, notice that the option "-i" in the python call in the line

do script "cd " & docParentPath & " && python -i " & docName

makes so that after the execution of the python script, the python interpreter is not exited, as per the request in the question.

If the lines

do script "cd " & docParentPath & " && python -i " & docName
do script "cd " & docParentPath & " && python -i " & docName in selected tab

are replaced by the lines

do script "cd " & docParentPath & " && ./" & docName
do script "cd " & docParentPath & " && ./" & docName in selected tab

then this AppleScript can launch any script, provided the right "shebang" line is present in the script, as the very first line. For a python script, the shebang line should be:

#!/usr/bin/env python

while for a bash shell script the shebang line should be:

#!/bin/bash

Solution 3:

$ python script_name.py &

This will run the process in the background.

Alternatively you could try the answers here for which I do not take the credit