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:
- Copy the script code in the Script Editor.app (found in the /Applications/Utilities/ folder)
- Compile the script (hammer icon on the Editor bar)
- Save it in the BBEdit scripts folder: /Users//Library/Application\ Support/BBEdit/Scripts/
- 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)
- 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