[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] BBEdit "Save MacPerl Droplet" Script



BBEdit is a very powerful text editor, but even with the excellent
MacPerl extensions available for it, there are many things that could
be done to strengthen its links to MacPerl.

One example of a way to do this with Apple Events scripting is the
following AppleScript, which automates a complicated task that I have
to do dozens of times a day (on bad days, anyway :) when I am testing
a new Perl droplet.

I like to edit my Perl code in a text file in BBEdit and then save a
copy of it as a separate MacPerl droplet (my text files have an
extension of ".src", for "source code," while my droplets have the
same name, minus the extension).

As soon as I am at the point of being able to test a Perl droplet, I
start switching back and forth between the Finder -- where I drop test
files onto the droplet -- and BBEdit -- where I check the results
(often using the handy "BBEdit Find Diffs" AppleScript droplet written
by Alex Maluta that comes on the BBEdit CD).

Every time I find and fix a bug, I have to do a lot of work to get to
the point of testing again.  That's where the following script comes
in.

This script runs a MacPerl syntax check (launching MacPerl, if
necessary) on the Perl program I'm editing in BBEdit.  If the syntax
checks out OK, it saves the program as a droplet (in the same folder
as the source file), and then switches to the Finder for further
testing.

However, if there is a syntax error, the script copies the MacPerl
error messages and creates a new window in BBEdit containing these
messages, at which point the script scrolls to the line in my source
code containing the first syntax error and highlights it.

All of this is done without MacPerl ever becoming the active
application.

I hope others here will find this script useful, and would like to let
people know that the December meeting of the BMUG Scripting SIG will
be discussing this script, as well as other ways of scripting BBEdit
(and linking it to MacPerl).

The BMUG Scripting SIG will meet on Monday, December 16th, at 6:00
p.m. at the BMUG offices, which are at 2055 Center Street in downtown
Berkeley, one-half block West of Shattuck Avenue and the Berkeley BART
station.



RUNNING THIS SCRIPT: You should start out by creating a Perl text file
with a ".src" extension.  Then open this file in BBEdit, and run the
following script.  This script can be modified fairly easily to
accomodate different naming conventions and to allow you to directly
save a droplet that you are editing in BBEdit (using the "Edit MacPerl
Script" extension).
_________________
tell application "BBEdit 4.0"
        
        --Get the name of the Perl program we're working on
        set fileName to file of window 1
        set windowName to name of window 1
        
        -- We want to make sure the Perl program has been saved before we do
anything else
        if (modified of window 1) is true then
                try
                        display dialog "Save " & windowName & "?" buttons {"Cancel",
"Save"} default button {"Save"}
                on error
                        return
                end try
                
                save window 1 to fileName
                
        end if
        
        -- Create the name we want to use for the droplet
        set dropletName to fileName as text
        
        -- Truncate the ".src" at the end (I use this as an extension for
Perl text files)
        -- Using only this line means that the droplet will be saved without
an extension
        set dropletName to characters 1 through -5 of dropletName as text
        
        -- Alternatively, if you use ".pl" as an extension for text files,
use the following line
        --set dropletName to characters 1 through -4 of dropletName as text
        
        -- If you'd like a ".drop" extension for your droplets, use the
following
        --set dropletName to dropletName & ".drop"
        
end tell

tell application "MacPerl"
        
        -- Make sure we have a fresh MacPerl window, so that our error
messages aren't
        -- mixed in with previous messages
        if Window "MacPerl" exists then Close Window "MacPerl"
        
        -- Check the syntax of the Perl program
        try
                Do Script fileName with =ABclass CHCK=BB
                
        on error
                -- If the Perl program failed the syntax check, copy the error
messages from the
                -- MacPerl window, close it, and then set things up to look at the
error messages in BBEdit
                set errorLog to contents of document 1
                Close document 1
                
                tell Application "BBEdit 4.0"
                        
                        -- Set the bounds of the new error log window
                        -- If this is a 13" monitor, put the window close to the left edge
of the screen
                        tell application "Finder" to set {a, b, c, d} to bounds of window
of desktop
                        if c is less than 700 then
                                set NewLeft to 15
                        else
                                -- Otherwise put the new window a little further out
                                set NewLeft to 150
                        end if
                        set NewTop to 244
                        set NewRight to NewLeft + 600
                        set NewBottom to NewTop + 120
                        
                        make new window at end with properties {index:-1, bounds:{NewLeft,
NewTop, NewRight, NewBottom}, status bar:false, line numbers:false,
cursor position:false}
                        
                        set contents of window -1 to errorLog
                        
                        -- Select the line with the first syntax error
                        if word -2 of line 2 of window -1 is "Line" then
                                set lineNum to word -1 of line 2 of window -1
                                
                                try
                                        set lineNum to lineNum as integer
                                on error
                                        -- Display the error log window 
                                        set index of window -1 to 1
                                        return
                                end try
                                
                                select line lineNum of window 1
                                
                                if lineNum is greater than 9 then
                                        set first visible line of window 1 to (lineNum - 9)
                                end if
                                
                                -- Display the error log window 
                                set index of window -1 to 1
                                
                        end if
                end tell
                beep
                return
        end try
        
        -- If the Perl program passed the syntax check, close the MacPerl
window,
        -- save the droplet, and activate the Finder
        Close document 1
        Save fileName in file dropletName as Droplet
        
end tell

tell application "Finder" to activate
_________________