Send line to Terminal - is this possible?

802 views
Skip to first unread message

Lars Dalby

unread,
Dec 5, 2010, 8:14:45 AM12/5/10
to TextWrangler Talk
Dear list

I just recently started using Textwrangler, mainly for editing R code
(http://www.r-project.org/). As I am running multicore functions in R,
I can't use the built-in editor. So I run R from the Terminal. But I
really miss the ability to send lines and selections using a keyboard
shortcut.
Is there any way to set up Texwrangler to send lines to an application
running in the Terminal by use of a keyboard shortcut?

Best
Lars

Spencer

unread,
Dec 6, 2010, 11:52:30 PM12/6/10
to TextWrangler Talk
Lars,

One solution is to write a applescript to run the selected lines in
Terminal. There are a number of tools available which then let you
assign a keyboard shortcut for this applescript.

You can see my applescript at: http://pastebin.com/B1vHQEKu . My setup
is as follows: In TextWrangler I have my code open, and I've selected
the lines I want to run in R. In Terminal.app I am running R from the
command line. I then run the following applescript (I called it "Send
Selection to R"), and the selected text is copied to Terminal. If
nothing is selected it will run the current line where the cursor is.
I believe this mimics the behavior of the R.app editor. Note that you
can easily run commands directly in R.app by uncommenting the right
line at the top of the script.

-Spencer

Lars Dalby

unread,
Dec 11, 2010, 4:50:46 PM12/11/10
to TextWrangler Talk
Perfect, thank you very much Spencer!

Lars


On Dec 7, 5:52 am, Spencer <quant...@gmail.com> wrote:
> Lars,
>
> One solution is to write a applescript to run the selected lines in
> Terminal. There are a number of tools available which then let you
> assign a keyboard shortcut for this applescript.
>
> You can see my applescript at:http://pastebin.com/B1vHQEKu. My setup

Jim

unread,
Dec 20, 2010, 12:47:47 PM12/20/10
to TextWrangler Talk
Spencer,

Thank you very much for your script. I would like to use it, but it
will not compile for me. The applescript editor returns:

Syntax Error
Expected end of line, etc. but found identifier

and highlights the word "terminal" in this line of code:
set term to current terminal

I am quite new to applescript and am not sure how to proceed.

Jim


On Dec 6, 8:52 pm, Spencer <quant...@gmail.com> wrote:
> Lars,
>
> One solution is to write a applescript to run the selected lines in
> Terminal. There are a number of tools available which then let you
> assign a keyboard shortcut for this applescript.
>
> You can see my applescript at:http://pastebin.com/B1vHQEKu. My setup

Thomas Fischer

unread,
Dec 21, 2010, 2:20:47 AM12/21/10
to textwr...@googlegroups.com
Hello,

if you are not really using it, you might comment out the block for iTerm:
put a "#" in front of the respective lines:

# else if myapp is equal to "iTerm" then

# end tell
else if myapp is equal to "R" then

If you are using it, check the version number, I had the same problem with iTerm 0.8, 0.10 seems to behave better.

Best
Thomas

Lars Dalby

unread,
Dec 22, 2010, 8:32:27 AM12/22/10
to TextWrangler Talk
Hi Spencer, Jim and Thomas

I have made slight modifications to the script provided by Spencer
earlier in this thread. My modifications was made so I could make a
"service" in Automator for which I then was able to select a keyboard
shortcut. It works well for me.

My script in Automator looks like this:

global myapp, editor, CurrLine

set myapp to "Terminal"

set editor to "TextWrangler"

set CurrLine to my getCurrentLine()

if myapp is equal to "Terminal" then
tell application "Terminal"
activate
set mytab to selected tab of front window
do script CurrLine in mytab
end tell
else if myapp is equal to "" then
else
display dialog "Don't know how to send lines to " & myapp
end if

to getCurrentLine() -- gets either the selected text or the line the
cursor is on
tell window 1 of application editor
set sel to selection
set SelLength to length of sel

if SelLength > 0 then
return sel as string
else
try
set lineNum to startline of sel
on error
-- startline is sometimes missing from sel. Access it by the class
name instead
set lineNum to «class SLin» of sel
end try
set lineTxt to line lineNum as string
return lineTxt
end if
end tell
end getCurrentLine

Hope you can make it work.
And thanks again to Spencer for making the script available in the
first place.

All the best,
Lars

Spencer

unread,
Jan 3, 2011, 7:35:23 PM1/3/11
to TextWrangler Talk
Lars,

I'm using iTerm 0.9.6.20090923. Looking through the changelog, it does
seem that there were some additions to the applescript dictionary in
version 0.9, so this might account for why wasn't working for you.

-Spencer

Jim Moon

unread,
Jan 18, 2011, 7:21:50 PM1/18/11
to textwr...@googlegroups.com
Thank You All for all of the help.  I am very appreciative.

Thomas, your suggestion aided in my getting the script to compile.

Lars, your script compiles fine in my Applescript Editor.  

Spencer, I appreciate your pre-compiled script.  I would like to have the option to send R code to iTerm.  I plan to try again to compile your original script.

I am so new to the Mac, that I seem to be having difficulty knowing how to set up the script to be accessed by keyboard shortcut or any other means.  

Lars, I have located the Automator application, but do not see exactly how to set up a script as a service.  Some cursory googling did not bring me to a solution.  Isn't there a specific directory where the MacOS likes for its compiled applescripts to be?

I realize that this is a TextWrangler forum and not a Automator/Applescript forum, but because of the specificity of this TW facility, I am emboldened to ask for detailed assistance.  Thank you very much in advance if you may take any time to provide further guidance.

Thanks again to you all.

Jim

--
You received this message because you are subscribed to the
"TextWrangler Talk" discussion group on Google Groups.
To post to this group, send email to textwr...@googlegroups.com
To unsubscribe from this group, send email to
textwrangler...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/textwrangler?hl=en
If you have a feature request or would like to report a problem,
please email "sup...@barebones.com" rather than posting to the group.



--
Jim Moon
jgm...@gmail.com
503.995.4169

Roy McCoy

unread,
Jan 18, 2011, 7:35:39 PM1/18/11
to textwr...@googlegroups.com
On Jan 19, 2011, at 1:21 AM, Jim Moon wrote:

> I am so new to the Mac, that I seem to be having difficulty knowing how to set up the script to be accessed by keyboard shortcut or any other means.

You can run them with QuicKeys, that's what I use.

http://startly.com/products/quickeys/mac/4/


Roy McCoy
UEA, Rotterdam NL

Lars Dalby

unread,
Jan 20, 2011, 3:09:53 PM1/20/11
to TextWrangler Talk
Hi Jim

I have tried to describe how to set up the shortcut using Automator:

open automator
choose service from the startup screen
locate the "Run applescript" action and drag it to the field on the
right hand side of the app.
Delete the initial text in the "Run applescript" box
Paste the script from this thread
click the hammer to check that it compiles.
Save

You have now created a service that you can access from any
application (click on the application name in the upper right corner
and you can find your service.)

Setting up the shortcut:
Go to System preferences > Keyboard
choose the "keyboard shortcut" tab.
click on "Services"
On the right hand side - scroll down to the bottom, where you will
find your newly created script.
Double click in the white space on right side of the line where your
script is.
Choose your shortcut by pressing the desired combination of keys.

You should now be able to use the script via a shortcut:-)

Hope it you can make it work. Otherwise just ask again.
> > textwrangler...@googlegroups.com<textwrangler%2Bunsubscribe@google groups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/textwrangler?hl=en
> > If you have a feature request or would like to report a problem,
> > please email "supp...@barebones.com" rather than posting to the group.

Lars Dalby

unread,
Jan 30, 2011, 1:57:47 PM1/30/11
to TextWrangler Talk
Hi all
I just discovered that Textwrangler has a built-in functionality,
where you can set keyboard shortcuts for scripts. I works very well
and is faster than using Automator.
Here's how to:
In Finder:
Copy the script file to this path:
~/Library/Application Support/TextWrangler/Scripts
In Textwrangler:
Go to window>palettes>scripts
Choose your script and click on "Set Key…"
Choose your shortcut and you are all set to go!

-Lars

On 20 Jan., 21:09, Lars Dalby <lars.da...@gmail.com> wrote:
> Hi Jim
>
> I have tried to describe how to set up the shortcut using Automator:
>
> open automator
> choose service from the startup screen
> locate the "Run applescript" action and drag it to the field on the
> right hand side of the app.
> Delete the initial text in the "Run applescript" box
> Paste the script from this thread
> click the hammer to check that it compiles.
> Save
>
> You have now created a service that you can access from any
> application (click on the application name in the upper right corner
> and you can find your service.)
>
> Setting up the shortcut:
> Go to System preferences > Keyboard
> choose the "keyboard shortcut" tab.
> click on "Services"
> On the right hand side - scroll down to the bottom, where you will
> find your newly created script.
> Double click in the white space on right side of thelinewhere your
> script is.
> Choose your shortcut by pressing the desired combination of keys.
>
> You should now be able to use the script via a shortcut:-)
>
> Hope it you can make it work. Otherwise just ask again.
>
> On Jan 19, 1:21 am, Jim Moon <jgm...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Thank You All for all of the help.  I am very appreciative.
>
> > Thomas, your suggestion aided in my getting the script to compile.
>
> > Lars, your script compiles fine in my Applescript Editor.
>
> > Spencer, I appreciate your pre-compiled script.  I would like to have the
> > option tosendR code to iTerm.  I plan to try again to compile your
> > > > > Expected end ofline, etc. but found identifier
>
> > > > > and highlights the word "terminal" in thislineof code:
> > > > >                   set term to currentterminal
>
> > > > > I am quite new to applescript and am not sure how to proceed.
>
> > > > > Jim
>
> > > --
> > > You received this message because you are subscribed to the
> > > "TextWrangler Talk" discussion group on Google Groups.
> > > To post to this group,sendemail to textwr...@googlegroups.com
> > > To unsubscribe from this group,sendemail to

Jim Moon

unread,
Feb 4, 2011, 7:20:02 PM2/4/11
to textwr...@googlegroups.com
Hi Guys,

Thank you all again so much for all of the help.  I have learned a variety of interesting and useful things.

I have installed iTerm.  And I compiled three scripts--one for sending line(s) to iTerm, one to Terminal and one to R64.  They are in the TextWrangler scripts directory, and shortcuts were created for each of them.  I added code to activate TextWrangler after sending the line(s).  And I may experiment to add code to do more than that.

Thank you again!

Jim

To post to this group, send email to textwr...@googlegroups.com

To unsubscribe from this group, send email to
textwrangler...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/textwrangler?hl=en
If you have a feature request or would like to report a problem,
please email "sup...@barebones.com" rather than posting to the group.
Reply all
Reply to author
Forward
0 new messages