5

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.

  • 1
    Simply add an & at the end of the command to run it. – bated Sep 29 '17 at 2:48
  • I am aware of the use of &. But I am running the script while I am in BBEdit. BBEdit has a menu command (see #! ("sebang") menu) that "sends" the current script to the terminal, and executes it. For this answer to be useful, I would need to know how to modify the behavior of this BBEdit menu command, and add a default "&" at the end of the command – Fabio Sep 29 '17 at 3:07
  • 1
    Have you contacted BareBones support at support@barebones.com? They may have a methods for you to accomplish this. – Scot Oct 4 '17 at 1:26
5
+50

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.

  • 1
    Hi CousinCocaine, than you! This seems the right thing for me! Let me try this, and I will grant you the bounty. Since I am not familiar with Apple scripts / services, would it be possible to associate the service to a keyboard shortcut? And in the case it is not possible, would it be complicated to turn this into a script? (In that case I guess remember that it is easy to associate a shortcut to a script) – Fabio Oct 2 '17 at 16:36
  • Hi CousinCocaine, I have played with your code, partially following the instructions found here: arstechnica.com/gadgets/2011/03/… But I can't see it in the services menu, although it is saved in ~/Library/Services. I also saved it a s a script (text file, with .sh extension, in ~/Library/Application\ Support/BBEdit/Scripts/ ) but it gives me the following error: 83:84: syntax error: Expected “,” but found “"”. (-2741) The file /Users/fabio/Library/Application Support/BBEdit/Scripts/python does not exist. Ideas? – Fabio Oct 3 '17 at 3:08
3

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
  • 1
    I see you have done it. Nice. BBEdit a fine piece of software. Happy coding. – CousinCocaine Oct 4 '17 at 13:08
2
$ 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

  • I am aware of the use of &. But I am running the script while I am in BBEdit. BBEdit has a menu command (see #! ("sebang") menu) that "sends" the current script to the terminal, and executes it. For this answer to be useful, I would need to know how to modify the behavior of this BBEdit menu command, and add a default "&" at the end of the command – Fabio Sep 29 '17 at 3:06
  • Could you call a shell script instead which executes your python and backgrounds it? – bated Sep 29 '17 at 15:44
  • From within BBEdit? Maybe yes, but how you do that? – Fabio Sep 30 '17 at 2:59
  • 1
    The OP has a specific question about running script from within BBEdit. Not a specific Bash command. I think a correct answer to another question. – CousinCocaine Oct 1 '17 at 13:21
2

This isn't quite what you want, but it is a reasonable workaround:

FSWatch. Briefly it monitors a directory or file for changes, and runs a script whenever it sees a change.

So instead of running in terminal, you have a terminal window open with fswatch watching the file(s) for changes. When it sees one, it runs the script -- in this case the file being watched.

This has the advantage that it runs in the same window each time, so you aren't popping up new terminal windows all the time.

Directions here:

https://stackoverflow.com/questions/24573584/how-to-watch-file-changes-on-mac-osx-using-fswatch

0

In the Terminal preferences under Profiles, select the profile you use by default, and go to the Shell tab. Under "When the shell exits" select "Close if the shell exited cleanly".

That should make the window/tab close when the script you run exits cleanly.

  • Thank you for the information. However, this is completely different from what I need. I need the terminal open and active :) – Fabio Sep 29 '17 at 10:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .