script/text filter advice

124 views
Skip to first unread message

Tim Gray

unread,
Aug 29, 2021, 10:14:56 PM8/29/21
to BBEdit Talk
Is AppleScript the best/most powerful way to script text manipulation based on where the cursor currently is? I know Text Filters can be very useful, but only seem to operate on selected text (or the whole document). But I'm looking to modify the current line if nothing is selected or modify the selected lines if multiples are selected, even if they are only partially so. I'm assuming that AppleScript is the only tool available for stuff like this in BBEdit.

Thanks,
Tim

jj

unread,
Aug 30, 2021, 4:06:15 AM8/30/21
to BBEdit Talk
Hi Tim,

BBEdit offers plenty of options to manipulate text:

  • Scripting with Script Editor or Script Debugger using BBEdit's rich vocabulary of objects and commands: 
      ◦ AppleScript, AppleScriptObjc, JavaScript
  • Any executable that can read BBEdit's environment variables: 
      ◦ Interpreted languages: python, perl, javascript, ruby, php, etc;
      ◦ Shell scripts and command line utilities;
      ◦ Custom compiled executables.

Which is better depends on the task at hand and your coding preferences.

Here is a sample shell script that reads the environment variables BBEdit's sets up:

    #!/usr/bin/env sh

    # Name of the document's current language (not set if language is "none")
    echo BB_DOC_LANGUAGE $BB_DOC_LANGUAGE ;

    # Emacs mode of the document's current language
    echo BB_DOC_MODE $BB_DOC_MODE ;

    # name of the document
    echo BB_DOC_NAME $BB_DOC_NAME ;

    # path of the document (not set if doc is unsaved)
    echo BB_DOC_PATH "\"$BB_DOC_PATH\"" ;

    # (zero-based) end of the selection range (not set if not text document)
    echo BB_DOC_SELEND $BB_DOC_SELEND ;

    # (one-based) de-tabbed column number of BB_DOC_SELEND
    echo BB_DOC_SELEND_COLUMN $BB_DOC_SELEND_COLUMN ;

    # (one-based) line number of BB_DOC_SELEND
    echo BB_DOC_SELEND_LINE $BB_DOC_SELEND_LINE ;

    # (zero-based) start of the selection range (not set if not text document)
    echo BB_DOC_SELSTART $BB_DOC_SELSTART ;

    # (one-based) de-tabbed column number of BB_DOC_SELSTART
    echo BB_DOC_SELSTART_COLUMN $BB_DOC_SELSTART_COLUMN ;

    # (one-based) line number of BB_DOC_SELSTART
    echo BB_DOC_SELSTART_LINE $BB_DOC_SELSTART_LINE;

    # length of the selection
    SEL_LENGTH=`expr $BB_DOC_SELEND - $BB_DOC_SELSTART` ;
    echo SEL_LENGTH $SEL_LENGTH ;
    
Scripts should be placed in ~/Library/Application Support/BBEdit/Scripts to appear on BBEdit's Script menu.
    
HTH,

Jean Jourdain

Christopher Stone

unread,
Aug 30, 2021, 5:20:36 AM8/30/21
to BBEdit-Talk
On Aug 29, 2021, at 21:12, Tim Gray <tg...@protozoic.com> wrote:
Is AppleScript the best/most powerful way to script text manipulation based on where the cursor currently is?


Hey Tim,

Basically.  Although as JJ mentions environment variables offer a good deal of information to shell scripts, it's more complicated to use that information in the shell than with AppleScript.

Here's your basic AppleScript for the task:

--------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# dCre: 2021/08/30 04:02
# dMod: 2021/08/30 04:02 
# Appl: BBEdit
# Task: Working with the selection in the front text window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Selection, @Text, @Window
--------------------------------------------------------

tell application "BBEdit"
    tell front text window

        

        --» Acquire relevant information about the selection.
        tell selection
            set selectionLength to its length
            set start_Line to its startLine
            set end_Line to its endLine
        end tell

        

        --» Process the selection.
        if selectionLength = 0 or start_Line = end_Line then
            # Process line start_Line.
        else
            # Process lines start_Line through end_Line.
        end if

        

    end tell
end tell

--------------------------------------------------------

Let us know if you need any more help.


--
Best Regards,
Chris


Tim Gray

unread,
Aug 30, 2021, 9:13:56 AM8/30/21
to BBEdit Talk
Chris and JJ,

Thanks! I suspect it is time to really actually try to learn AppleScript. At the same time, I of course had forgotten that Unix Filters (now Text Filters) had access to useful BBEdit environment variables. With that I should be able to accomplish some of what I’d like to do in a language I am more comfortable with like Python. 

Thanks again!
--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages