Is there a way to execute a line in a worksheet via Applescript?
I've got a script that preassembles a command and opens it in a new worksheet, and I'd like for the script to execute the line as well to save me a step.
TIA
--
Best Regards,
Chris
There certainly is a way to ask Terminal.app to run a command and there is "do shell script" which has a few problems with quoting. But. . .
<ftp://ftp.macnauchtan.com/Software/BBEdit/WorksheetOpen>
and a couple of other files in the directory <ftp://ftp.macnauchtan.com/Software/BBEdit/>.
I'm stuck on OS 10.3.9 because of my SE/30 file server so there have been no updates since Bare Bones stopped supporting 10.3. Those scripts might not work with newer versions of BBEdit.
What I do is take advantage of the fact that, on opening, a BBEdit worksheet has to make calls to a new instance of the declared shell. At least it has to set a working directory. It's possible to trap that call and cause a few commands in the worksheet to be executed. I identify them between lines like this:
##BEGIN
setenv WORK $HOME/projects/target
cd $WORK
source $HOME/bin/sshstuff
##END
When I open a BBEdit worksheet that I use to work on a project I get execution of that block of lines. I can also set up ID files for scp and ssh which are different for each project.
Watch out for worksheets though. They are really *.plist files that use UNIX line ends for the XML stuff but change to Mac line ends for the part of the XML file that is really the text of the file hidden in an XML entity. You can't open one of those as a regular file with BBEdit.
I usually find it easier to use the osascript tool to call AppleScript commands from a shell script:
osascript << ENDSCRIPT
tell application "BBEdit"
do something
end tell
ENDSCRIPT
--
--> From the U S of A, the only socialist country that refuses to admit it. <--
______________________________________________________________________
Hey Doug,
Thanks. That gives me a couple of things to consider.
I do use the Terminal's 'do script' function quite frequently, and I also run shell scripts directly from Applescript. However in this instance I'm using BBEdit to view output from a filtered curl command, and it's convenient to do it with a worksheet - because if I don't get the output I want I can quickly change the filter parameters and execute again.
I know I can pipe output quite easily to BBEdit via 'bbedit' and do so regularly. I also use BBEdit as my primary Man page viewer (re: another discussion) due to its superior search functions and the fact that it runs on my system 24/7 (although Bwana has been in my tools folder since it came out).
I'm a bit of an automation junkie and have QuicKeys, Keyboard Maestro, and FastScripts active on my system. I also use BBEdit's script menu for a number of things and Script Debugger's as well. (If I can't do it with a keyboard shortcut...)
I've really never used osascript, because I haven't done much shell scripting (however this will change soon). I do use the shell and pipes frequently, so I guess I mean to say that I haven't started scripting *Bash* in a formal way.
--
Best Regards,
Chris
>Hey Folks,
>
>Is there a way to execute a line in a worksheet via Applescript?
>
>I've got a script that preassembles a command and opens it in a
>new worksheet, and I'd like for the script to execute the line
>as well to save me a step.
Not sure if this fits your needs, but...
I use an applescript (called by an assigned key combination) to
select the relevant lines of my worksheets, so all I have to do
then is press Enter (not Return) and the code executes with
whatever params I've supplied. The script also clears away any
output left from the previous execution.
To do this, I use a consistent layout for my worksheets. Here's
an example from one of my sheets (simplified):
## start BBEdit Worksheet ##
A='Project=SEPT2010'
B='base_dir=/Volumes/MyData'
C='source_file=voter_tabs.txt'
D='crosstab=gender pty_group age_cohort exp_vote_method'
perl /Volumes/LIB/make_cross_tab_summaries "$A" "$B" "$C" "$D"
#-#-#
...
## end BBEdit Worksheet ##
You can handle input params in other ways; I like using A, B, C,
... so I can easily provide params in option=value form, then
hand them to the executable as $ variables (in quotes in case
there are spaces or other confusing characters within the
params). Of course, the form of your params may vary according
to what your executable expects.
The applescript finds the line with '#-#-#', first deletes
anything below it (previous output), and then selects the
remaining lines from the top down to and including the '#-#-#'.
That way when I press Enter, all params and the executable are
invoked, and the output appears below the '#-#-#' line.
This allows me to quickly try params, see the results and
possibly do something with them, then go back and quickly
execute again with different params. The reason I don't just do
'Undo' after execution is that I often mess with the result
output, so undoing wouldn't necessarily give me what I want.
Here is the applescript (watch out for email-induced line breaks):
## start Applescript ##
tell application "BBEdit"
activate
set mywindow to shell window 1
set lineX to 1000000
set lineBye to endLine of text of mywindow
repeat with i from 1 to the number of lines of text of mywindow
set linetext to contents of line i of text of mywindow
if linetext contains "#-#-#" then
set lineX to i + 1
exit repeat
end if
end repeat
if lineX < lineBye then
select text from line lineX of text of mywindow to line
lineBye of text of mywindow
set the selection to ""
end if
select text from line 1 of text of mywindow to line (lineX
- 1) of text of mywindow
end tell
## end Applescript ##
As I said, I assign a key combination to invoke this script.
It would seem to me that your applescript that preassembles the
command could create something like the above worksheet layout,
and then the same script or a second one could do what mine
above does to invoke the assembled command.
Really, the layout and the form of the params can be whatever
you want; in your case it might all be preassembled on one line.
The key thing is to be able to select the executable line and
its params. You could probably even script the 'Enter' keypress...
HTH
- Bruce
_bruce__van_allen__santa_cruz_ca_
>Is there a way to execute a line in a worksheet via Applescript?
>
>I've got a script that preassembles a command and opens it in a new
>worksheet, and I'd like for the script to execute the line as well
>to save me a step.
You do mean document/window, I suppose, and not worksheet, which is
an Excel thing?
set _line to ---- use BBEdit's lingo to get it
run script _line
or am I missing the point?
JD
John -- You don't know about BBEdit (shell) worksheets?????
- Bruce
_bruce__van_allen__santa_cruz_ca_
> At 12:18 -0600 08/02/2011, Christopher Stone wrote:
>
>> Is there a way to execute a line in a worksheet via Applescript?
>>
>> I've got a script that preassembles a command and opens it in a new worksheet, and I'd like for the script to execute the line as well to save me a step.
>
> You do mean document/window, I suppose, and not worksheet, which is an Excel thing?
You're in for a treat. Select File > New > Shell Worksheet, and read what's in the window that appears.
— F
Hey Bruce,
Interesting. That's not quite what I had in mind, but it gives me a few ideas that'll make the work-flow a bit more to my liking.
Thanks.
Playing a bit with your script:
tell application "BBEdit"
try
tell text of front shell window
select insertion point after (first line where its text is "#-#-#")
set charOffset to (characterOffset of selection)
if charOffset ≠ (characterOffset of last character) + 1 then
delete (characters charOffset thru -1)
end if
select (lines 1 thru (get endLine of (first line where its text is "#-#-#")))
end tell
end try
end tell
OR
tell application "BBEdit"
try
tell text of front shell window
try
delete (characters (get characterOffset of character after (first line where its text is "#-#-#")) thru -1)
end try
try
select (lines 1 thru (get endLine of (first line where its text is "#-#-#")))
end try
end tell
end try
end tell
--
Best Regards,
Chris
>Playing a bit with your script:
Nice.
I modified the second one to delete starting one character
later, to preserve the end-of-line after #-#-#, so the output
starts on its own line. That's the '+ 1'.
tell application "BBEdit"
try
tell text of front shell window
try
delete (characters ((get characterOffset of character
after (first line where its text is "#-#-#")) + 1) thru -1)
end try
try
select (lines 1 thru (get endLine of (first line where
its text is "#-#-#")))
end try
end tell
end try
end tell
Anyway, best wishes for progress on YOUR script.
- Bruce
_bruce__van_allen__santa_cruz_ca_
______________________________________________________________________
Aha! Got ya. I didn't realize that the output started after the *entire* selection (including empty lines). I've been dumping in the odd 'echo ""' for spacing, and there's a much better way. This will lessen my frustration factor by quite a lot. :)
Thanks again.
--
Best Regards,
Chris
I have no Select File menu item.
File>New>Shell Worksheet
works fine but then I'm limited to 8.5.2
There must be a template somewhere. Has it been discarded? Try this:
bbedit "/Applications/BBEdit.app/Contents/Resources/Default Support Folder/Stationery/Default Worksheet Stationery"
--
--> A fair tax is one that you pay but I don't <--