[TxMt] R bundle: Execute line / selection
Hans-Joerg Bibiko
bibiko at eva.mpg.de
Mon Dec 18 09:44:55 UTC 2006
Hi,
>
>> Of course, to use the clipboard will destroy its content, but this
>> can be discussed.
>
> Yeah this is really not acceptable. Perhaps there is a way to write
> the information in some sort of temp file, or an unnamed pipe? (I
> think there is some trick with something like "<(commandshere)"
> that you can add to the end of a command, so that the output of
> commandshere is fed as a filename would have been fed otherwise, or
> something line that). We do need a way to get things to go back and
> forth without touching the clipboard. The clipboard is the user's
> space, we shouldn't be messing with it without the user asking for it.
Yes, yes I know the clipboard is the user's territory. To restore the
old clipboard content is doable but you destroy TM history list.
It was ONLY a suggestion because it made things easier and fast ;)
So, to avoid the clipboard and speed up the AppleScript a bit I found
this:
Command 'Execution Line/Selection'
_________________________
echo
TASK=`tail -c +2`
# check whether R.app is running
if [ $(ps -xc | grep ' R$' | wc -l) -eq 0 ]; then
open -a R
# sleeps for 5 sec - can be fine-tuned
sleep 5
osascript <<-AS
tell application "System Events"
set visible of process "R" to false
end tell
AS
fi
RES=$(osascript "$TM_BUNDLE_SUPPORT/bin/tm_get_r.scpt" "$TASK" 2>/dev/
null | tail -n +2)
echo -en "$RES"
NL=$(echo -en "$RES" | tail -n 1)
if [ "$NL" != "> " -a "$NL" != "+ " ]; then
echo -en "\n> "
fi
##########################################
# source code for 'tm_get_r'
#
#
#on run (argv)
# tell application "System Events"
# -- Get a reference to the text field
# set text_area to (process "R")'s (window "R Console")'s (scroll
area 1)'s text area 1
# -- Get text before and after our command
# set oldtext to text_area's value
# set x to item 1 of argv
# tell application "R" to cmd x
# set newtext to text_area's value
#
# -- Find the difference between old and new
# text from ((oldtext's length) + 1) to -1 of newtext
# end tell
#end run
#
###########################################
__________________________
The tricky thing is to pass parameters to AppleScript. I've chosen
the variant via an argument list. I tried to get the command via
AppleScript's "system attribute" but I found no way to export the
variable TASK in such a way that the AppleScript will find it.
The new command has the same features like the variant using the
clipboard.
The nice thing with the clipboard also was that I don't have encoding
problem like print("façade") but maybe I find a fast way to solve it.
Attached is the compiled AppleScript, the sourde code you find above.
Please check this command for bugs or improvements. I did a tiny
benchmark test and it turns out that this command is about 35% faster
than the variant with embedded AppleScript.
Cheers,
-Hans
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tm_get_r.scpt
Type: application/octet-stream
Size: 67622 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20061218/fd19ba4d/attachment.scpt>
-------------- next part --------------
PS You have to copy this compiled file into R bundle's /Support/bin
folder. (Or compile it by yourself)
More information about the textmate
mailing list