On Dec 13, 2008, at 12:53 PM, Dennis wrote:

> On Dec 13, 2008, at 9:54 AM, gshenaut wrote:
>
>> I was hoping it to be built into BBedit, but I guess it isn't, so I
>> wanted to make a script to do it, but that turns out to be  
>> nontrivial.
>
> There are several of ways to do something like this…

Ugh, I don't know how it looks in your email client, but the  
formatting on my last post is almost unreadable on mine. Here it is  
again in a (hopefully) more readable condition. Sorry about that.

---

There are several of ways to do something like this and it's fairly  
trivial as long as you have a little patience in exploring your  
options. Here are a few things that come to mind:

(1) Work entirely within AppleScript:

Here's a basic start for a script that you can develop further:

property theCommand : ""
tell application "BBEdit"
   tell window 1
      set currentSelection to selection as string
      set theCommand to text returned of ¬
         (display dialog "Enter shell command:" ¬
         default answer theCommand buttons {"Execute"} ¬
         default button 1 with icon 1)
      try
         set theOutput to ¬
         (do shell script theCommand & " '" & ¬
         currentSelection & "'") & return
      on error errMsg number errNum
         beep
         display dialog "Error: " & errNum & ". " & errMsg ¬
         with icon stop buttons {"Cancel"} default button 1
      end try
      set selection to theOutput
   end tell
end tell


(2) Use BBEdit Unix Filters, avoiding AppleScript altogether:

Without AppleScript, it's more difficult to collect input from a GUI  
prompt. But you could write a Unix Filter for all the shell commands  
you want to perform, where only the text to be processed varies.

Or, if the commands themselves vary as well, you could type them in  
BBEdit and pass them along to a Unix Filter as part of the input. Then  
"eval" the entire thing from within your shell script.

Here's an example of the latter approach using Python (but you can use  
any language you want):

#!/usr/local/bin/python
import fileinput
import string
import os
if __name__ == "__main__":
   cmdResult = ''
   for my_line in fileinput.input():
     cmdResult = cmdResult + os.popen(my_line).read()
   print cmdResult


(3) Combine options 1 and 2 above:

This is an AppleScript I threw together a while back and never really  
finished, so there's lots of room for improvement. But the basic idea  
is to execute the selected text as a shell command, like we did in  
option 2 above.

The difference, however, is that because it uses AppleScript, we can  
do some fancier things: use a different interpreter based on the  
source language of the current document ('php -r' for PHP code, 'perl - 
e' for Perl, 'ruby -e' for Ruby, etc.), display GUI error dialogs, and  
have more flexibility in how the returned text is presented.

Give it a try if you're interested, and feel free to modify it to suit  
your needs.

http://web.me.com/dennisrande/downloads/Run%20Shell%20Command.zip


(4) Use a BBEdit shell worksheet, a special document type that allows  
you to store and execute Unix command lines. See page 322 of the  
BBEdit User Manual for more information.


Hope this gives you some ideas.

-Dennis
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "BBEdit Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a specific feature request or would like to report a suspected (or 
confirmed) problem with the software, please email to "[email protected]" 
rather than posting to the group.
-~----------~----~----~----~------~----~------~--~---

Reply via email to