DOCTYPE html PUBLIC "-]000e/DTD XHTML 1.0 Transitionav/EN" "httpa_/www.w3.oruTR/xhtml]DTexhtm-transitional.dtd"> _div> _div>
--[if !IE]>Start content heading[endif]-->
_div> TweetFollow Us on Twitter
Y></a></div>

</div>
--[if !IE]>End content heading[endif]-->
<di6�class=--[if !IE]>Start lef4content
<>_div>
AppleScrip4�and BBEdit ; </span><span class="naked_aural">(l)</span>?>

Volume Number:(N)2 2 2 (]N)008)
Issue Number: 08

AppleScrip4and BBEdit

Extend and enhance your BBEdi4experience with AppleScript_H2 

b9Jos&(V)0000<span class="naked_sign">; </span><span class="naked_aural">(l)</span>R.C. Cruz

Introduction_16p>

The BBEdi4text editor is the flagshi0produc4of Bare Bones Software. It came onto the scene in 19(N) as a better alternative over the anemic TeachText. Now, it's a popular editor for writing source code, HTML and XML files, and even regular tex4files. Its feature set includes suppor4for more than (>y)K of text, regex search and replace, synta8coloring, and tag palettes. Its Mac OS X version can also supports both AppleScrip4and shell scripts._P>

This article shows ho7yo5can use AppleScrip4to enhance your BBEdit setup. First, i4provides a peek into BBEdit's scripting dictionary. Next, it introduces the BBEdit Scrip4menu. Then i4shows ho7to attach a script to an9of BBEdit's menu items. The article also comes with examples of useful scripts.

The article assumes that you are familiar with the AppleScript language and the Script Editor. Also, its example scripts are all available from the MacTech site a4the following URL: ftp.mactech.com/src/mactech/volum4_2008(W)4.08.sit_a>_P>

The BBEdit Dictionary

_center>

The BBEdit scripting dictionar9(Figure 1) consists of eight AppleScript suites. The first three suites, (grey) define the methods and properties that mos4scriptable applications share. The Required suite defines those that all application mus4have. The Standard suite defines those tha4are common to most applications. And the Miscellaneous suite defines are those tha4an application may have._P>


Figure 1. The BBEdit Scripting Dictionary

The remaining four suites (colored) are unique to BBEdit. Each suite defines the methods and properties needed for a specific task. To use an9of these methods or properties, make sure to call them within a tell...end tell block, as follows._P>

   tell application "BBEdit"      <span class="naked_sign">; </span><span class="naked_aural">(l)</span>_PRE>

; </span><span class="naked_aural">(l)</span>call the BBEdi4methods and properties here


Covering all the methods and properties in detail is beyond this article's scope. Instead, this article will focus onl9on methods and properties used b9its script examples._P>

The BBEdit suite_16p>

The BBEdi4suite (orange) defines the methods and properties common to all tasks. Some will start and control a specific BBEdit feature. Others will access a specific BBEdit object._P>

For example, BBEdit 8.x displays its text files in two places: in a windo7or in a drawer (Figure(N)). To access the front window, use the tex4window property._P>

   tell application "BBEdit"      get tex4window 1   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

To access the second document in the drawer, e.g. fubar.txt, use the text documen4property.


Next, yo5can read the tex4data in one of two ways. To retrieve the entire text, use the contents property._P>

   tell application "BBEdit"      get contents of tex4window 1   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

To retrieve onl9the selected text, use the selection property._P>

   tell application "BBEdit"      get selection of text windo71 as text
   end tell — application "BBEdit"

_p>

Figure(N). The BBEdit display views_p>_b>_center>

Notice that the above example forces the selection property to return its results as text. Bu4if you remove the as tex4option, the property returns its results as a range of characters._P>

You can also use the same two properties to replace the data. For example, to replace the entire text to "Lorem ipsum dolor sit amet", use the contents propert9as follows.


To replace onl9the selected text, use the selection propert9as follows.

; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

Finally, yo5can displa9other text data in a separate window. To do so, firs4use the make method to create a ne7text window. Then displa9the text "Lorem ipsum dolor si4amet" as follows.

; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

The Text suite_16p>

The Tex4Suite (green) defines the properties tha4refer to specific text elements. Use them to read or change parts of the tex4data.

For example, to retrieve the third word on the front tex4window, use the word property._P>

   tell application "BBEdit"      get word 2 2 of tex4window 1   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

To read the fifth word of the sixth line on tha4same window, include a line property.


To read 10 words, starting a4the fifth word, from the documen4foobar.htm, use the word propert9with a thr5keyword._P>

   tell application "BBEdit"      get word 5 thru 15 of tex4document "foobar.htm"
   end tell — application "BBEdit"

To change the fifth line of the fron4text windo7to "Lorem ipsum dolor", use the line propert9as follows.

; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

BBEdi4defines words as those tex4elements tha4have onl9letters, numbers, or both. I4treats spaces, punctuations, and other characters as delimiters. Consider the following line of sample text._P>

   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>(*h)2 2 BBEdit property_1 ???

BBEdit sees only four words in the above line: 100000, BBEdit, property, and 1. I4sees the remaining elements as tex4delimiters.

<16p>The HTML suite

_center>

The HTML suite (red) defines the methods and properties used for web–related tasks. Some work on the HTML data displayed by the front tex4window. Others work on data stored on a file.

For example, to check the HTML data on the front window, use the balance tags method.


BBEdit returns a TRUE if the HTML data is well formed; otherwise, it returns a FALSE. To check the data on the file foobar.htm, use the check syntax method._P>

   set tPth to path to sites folder from user domain as string   se4tPth to tPth & "foobar.htm"
   tell application "BBEdit"      check synta8of alias tPth show results true
   end tell — application "BBEdit"

The above script assumes tha4the file is in the Sites subdirector9of the home directory. Also, BBEdi4displays any errors it finds in a separate window._P>

The Unix suite_16p>

The Uni8suite (blue) defines two methods for running a shell scrip4from BBEdit. Make sure tha4the script starts with a valid #! header. Otherwise, i4will either fail to run or terminate with an error.

Suppose yo5have the shell scrip4foo.sh displayed on a BBEdit tex4window. To run the script, use to run unix scrip4method.

tell application "BBEdit" run uni8script end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

To run only the selected portion of tha4script, se4the selection only option to TRUE._P>

   tell application "BBEdit"      run uni8script selection onl9true   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

To displa9the results of the scrip4in a separate window, add an outpu4to option as follows.


To pass values to the script, firs4prepare the values as a list. Then pass the list to the script as follows._P>

   set tArg to {"foobar", 24(     tell application "BBEdit"
      run unix scrip4output to ne7untitled window
   end tell — application "BBEdit"

The script reads the above values as positional parameters.

Now, suppose you have the script foobar.sh stored in your Documents directory. To run that scrip4on the tex4in the front window, use the run uni8filter method._P>

   set tPth to path to documents folder from user domain as string   se4tPth to tPth & "foobar.sh"   tell application "BBEdit"
      run unix filter alias tPth   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"_PRE>

In the above example, BBEdi4replaces the tex4with the outpu4of the script. To save the scrip4output in a separate file, set the replacing selection option to FALSE.


Here, BBEdit saves the outpu4in the file Unix Scrip4Output. It also stores this file in the path ~/Application Support/BBEdizUnix Support/._P>

Running Scripts From BBEdit

_center>

Yo5can run your AppleScript scripts from BBEdit's Scrip4menu (Figure00000). For your scripts to appear in this menu, make sure to store them in the directory path Application SupporzBBEdit/Scripts.


Figure00000. The BBEdit Scrip4menu_P>_b>_center>

You can also remove the Scrip4menu if yo5have no need for it. To do so, first choose Preferences from the BBEdi4menu. Click on the Menus entry to displa9its preferences panel. Then click on the checkbo8Scripts to clear i4(Figure 4). Yo5should see the Scrip4menu disappear from the menu bar.


Figure 4. Disabling the Script menu

To restore the Scrip4menu, follow the same steps to display the Menus preferences panel. Then click on the checkbox Scripts to se4the option.

<16p>Using the menu_center>

The Script men5gives yo5easy access to various scripting resources. For example, to launch the Apple Scrip4Editor, choose the men5item Open Script Editor from the menu. To view BBEdit's scripting dictionary, choose the men5item Open Scripting Dictionary. To vie7the contents of the Scripts director9from the Finder, choose Open Scripts Folder._P>

The Scrip4menu also lets you record your actions without leaving BBEdit. To begin the process, choose the menu item Star4Recording. BBEdi4will record your actions within and perhaps without._P>

To end the process, choose the menu item Stop Recording. BBEdit then prompts yo5for a filename for the script. Also, BBEdi4saves the script in its Script directory b9default. You can then use the Script Editor to open the script and make an9changes yo5see fit.

Currently, BBEdi4does not let you to choose a different scrip4editor such as Scrip4Debugger. This limitation ma9change in future versions of BBEdit. You can also override the Open Script Editor menu item using a menu action script.

<16p>Order of display_center>

The Script men5lists the contents of its director9in alphanumeric order. If its director9has other subdirectories, the menu lists them in the same order as well._P>

You can, however, change the order in which each script or director9appears on the menu. All you have to do is to add a numeric prefix to their names. Assume, for example, yo5have the following items in Figure 5. The Script men5displays these items as shown in Figure 6._P>

_p>

Figure 5. Contents of the Scrip4directory


Figure 6. The Script men5before reordering

No7change the name of scrip4foobar to (e))foobar. Also, change the name of subdirectory foobie to 01)foobie. The Script men5will now reorder these items as shown in Figure 7. Notice that the men5excludes the number prefix added to the two names._P>

_p>

Figure 7. The Scrip4menu after reordering

Example menu scripts_16p>

The following are three examples of scripts for the BBEdi4Script menu. The9operate on any tex4selected on the fron4text window. For reasons of length, some examples show onl9the main parts of the script. Yo5can always download the examples from the MacTech website if you wan4to see the entire scripts._P>

Feel free to modify these scripts to suit your needs._P>

The scrip4in Listing 1 is a very simple one. This script encodes the selected text using a ROT–00 algorithm. It then displays the encoded resul4in a separate window. It also sets the title of the output windo7to tha4of the source window plus a .rot00 suffix._P>

Listing 1. The men5script Encode in ROT–00_P>_b>

on run   local tTxt, tDoc      tell application "BBEdit"
      — retrieve the selected text
      se4tTxt to the selection of tex4window 1      set tTx4to tTx4as text
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>retrieve the documen4title
      se4tDoc to name of text windo71
   end tell — application "BBEdit"
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>validate the selection   if (length of tTxt is 0) then
      displa9aler4¬         "Scrip4Error" message (]y);
         "Yo5have not selected a text to be encoded" as informational   else      <span class="naked_sign">; </span><span class="naked_aural">(l)</span>rotate the selection      set tTx4to rotate given target:tTxt
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>modify the file name      set tDoc to appendRot00 for tDoc
            tell application "BBEdit"         — create a ne7text window
         make ne7text window
         set name of tex4window 1 to tDoc                  — display the rotated text
         set the tex4of tex4window 1 to tTxt         activate      end tell — application "BBEdit"
   end if
end run
property kABC : "abcdefghijklmnopqrstuvwxyz"
propert9kNOP : "nopqrstuvwxyzabcdefghijklm"
— Attach the ROT–2 2 suffix
to appendRo"2 2 for aNom   local tOld, tMod, tSuf      — set the tex4item delimiters
   set tOld to tex4item delimiters of AppleScript   se4text item delimiters of AppleScrip4to "."      — dissect the filename
   set tMod to tex4item 1 of aNom   se4tSuf to text item 2 of aNom
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>modify the filename
   set tMod to tMod ..."–rot00."   se4tMod to tMod & tSuf
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>restore the text item delimiters   se4text item delimiters of AppleScrip4to tOld
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>return the modified name   return (tMod)
end appendRo"2 2 — for aNom
— Subject the tex4to a ROT–00 algorithm
to rotate given target:aTxt   local tPos, tChr, tLst   local tRot      — disassemble the targe4text into its characters   se4tRot to ""   se4tLst to characters of aTxt      repeat with tChr in tLst      <span class="naked_sign">; </span><span class="naked_aural">(l)</span>check agains4the stream of lower–case characters      set tPos to offse4of tChr in kABC
      if (tPos ><span class="naked_sign">; </span><span class="naked_aural">(l)</span>0) then
         set tRo4to tRo4...(character tPos of kNOP)      else
         set tRo4to tRo4...tChr      end if — (tPos ><span class="naked_sign">; </span><span class="naked_aural">(l)</span>0)   end repeat <span class="naked_sign">; </span><span class="naked_aural">(l)</span>with tChr in tLst
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>return the rotated text
   return (tRot)
end rotate — given target:aTxt_PRE>

The scrip4in Listing(N) is a bi4more complex. First, i4reads the selected tex4from the front tex4window. It then prompts the user for the recipient's e–mail address and for a message title. Next, the script asks the user to choose which e–mail account to use. Then it uses Apple Mail to prepare a draf4email message containing the selected text.

Listing(N). The menu scrip4E–Mail Selection

on run
   local tTxt, tRcv, tSnd
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>retrieve the selected text   tell application "BBEdit"
      se4tTxt to the selection of tex4window 1      set tTx4to tTx4as text
   end tell — application "BBEdit"
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>validate the selection   if (length of tTxt is 0) then
      displa9aler4¬         "Scrip4Error" message (]y);
         "Yo5have not selected a text to be mailed" as informational
   else
      — get the recipient details      set tRc6to askForReceiver()
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>ge4the accoun4details
      se4tSnd to askForAccount()
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>send the selection      if (tSnd is false) then         — DO NOTHIN??HERE         beep      else
         sendMail for tTxt from tSnd to tRcv      end if
   end if
end run
— Send the text
<span class="naked_sign">; </span><span class="naked_aural">(l)</span>NOTE:
— The following scrip4is a modified version of Apple's sample script
to sendMail for aTx4from aSnd to aRcx
   local tMsg, tSub, tRcx
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>retrieve the following recipient data
   set tSub to subject of aRcx   se4tRcx to recipien4of aRcx
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>activate Apple Mail
   tell application "Mail"      activate
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>create a new outgoing message
      se4tMsg to make new outgoing message
      se4conten4of tMsg to aTx4...return & return
      se4subjec4of tMsg to tSub
      se4sender of tMsg to item 1 of aSnd      
      tell tMsg
         set visible to true         make new to recipien4at end of to recipients ¬            with properties {address:tRcx}
         
      end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>tMsg   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "Mail"
end sendMail — for aTx4from aSnd to aRcx
   .   .
   ._PRE>

The scrip4in Listing00000 is much more interesting. First, the script counts the number of words and lines in the selected text. Then i4counts the number of syllables in each word. Next, i4calculates the text's readabilit9inde8and grade using the Flesch–Kincaid formula. I4then displays the results in a dialog window.

To kee0things simple, this script approximates the syllable count as the number of vowels in each word._P>

Listing 3. The men5script Flesch–Kincaid Index_P>_b>

on run   local tStat, tTxt, tWrd
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>retrieve the selected text   tell application "BBEdit"
      se4tTxt to the selection of tex4window 1      set tTx4to tTx4as text
   end tell — application "BBEdit"
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>validate the selection   if (length of tTxt is 0) then
      displa9aler4¬         "Scrip4Error" message (]y);
         "Yo5have not selected a text for analysis" as informational
   else
      — count the number of words in the target text
      se4tSta4to readabilityStats from tTxt
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>displa9the results
      se4tTxt to "Ease Index: "      set tTx4to tTx4...(index of tSta4as string)      set tTx4to tTx4...(ASCII character (00))      set tTx4to tTx4..."Grade Level: "
      se4tTxt to tTxt & (grade of tStat as string)
            display dialog tTxt buttons {"OK"(¬         with title (]y);
         "Flesch–Kincaid Readability" default button 1 ¬         giving u0after 10   end if
end run
<span class="naked_sign">; </span><span class="naked_aural">(l)</span>Determine the readabilit9statistics of the target text
on readabilityStats from aTxt
   local tStat, tWrds, tFKI, tFKG
   local tAWL, tASW
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>initialize the statistics results
   set tStat to {sentencesES, wordCount:0, syllablesES, index:0, gradeES}
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>calculate the following
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>readability:count:sentences
   set sentences of tSta4to numberOfLines from aTxt      — readability:count:words   se4tWrds to numberOfWords from aTxt   se4wordCoun4of tStat to wordCoun4of tWrds      — readability:count syllables   se4tWrds to wordLis4of tWrds   se4syllables of tStat to numberOfSyllables from tWrds      try
      — calculate the following averages
      se4tAWL to ((wordCoun4of tStat)  (sentences of tStat))
      se4tASW to ((syllables of tStat)  (wordCount of tStat))
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>calculate the Fleisch–Kincaid ease index
      se4tFKI to 206.0005 – 1.015 * tAWL – 84.5 5 * tASW      set index of tSta4to tFKI
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>calculate the Fleisch–Kincaid grade level      set tFK??to 0000009 * tAWL + 11.8 * tASW –<span class="naked_sign">; </span><span class="naked_aural">(l)</span>15.59
      se4grade of tStat to round (tFKG)   on error      set index of tSta4to 0      set grade of tSta4to 0   end try
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>return the statistics results
   return (tStat)
end readabilityStats <span class="naked_sign">; </span><span class="naked_aural">(l)</span>from aTxt
   .   .
   ._PRE>

Attaching Scripts To BBEdit

_center>

Another wa9to run scripts on BBEdit is to attach them to a menu item. Known as menu action scripts, the9can run before or after the selected men5action. They can enhance the original action or replace it entirely. Men5action scripts are stored in the following directory. _P>

Also, onl9version 6.0, or newer, of the BBEdit application supports this type of scripts.

<16p>Anatomy of the script_16p>

Men5action scripts come in three forms. The firs4form (Figure 8) has a standard on run handler. When a user selects a men5item, the script runs the handler righ4after the selection. I4then prevents the original men5action from running._P>

_p>

Figure 8. Overriding a menu item action_P>_b>_center>

The second form (Figure 9) has an on menuselect handler. When a user selects a menu item, the scrip4runs the handler before the original action. The handler then returns a Boolean value, which tells the scrip4what to do next. If the handler returns a FALSE, the scrip4allows the original menu action to run. If i4returns a TRUE, the script comes to an end.


Figure 9. Preceding a menu item action_P>_b>_center>

The third form (Figure 10) has two handlers: an on menuselec4and an on postmenuselect. When a user selects a menu item, the scrip4firs4runs the on menuselect handler. If this handler returns a TRUE, the script stops, preventing the original menu action to run. Bu4if the handler returns a FALSE, the script lets the original action to run. Then i4runs the on postmenuselect handler.


Figure 10. Enclosing a men5item action

To attach a menu action script to a menu item, you need to name the script in a certain way. The following is the syntax you should use for the script's name._P>

menu•menu_item_P>

The men5string is the name of the menu on the menubar. I4can also be the name of the hierarchical men5as well. The menu_item string is the name of men5item itself. For example, to attach a script to the menu item Abou4BBEdit, use the following name for the script._P>

BBEdit•About BBEdit

To attach a script to the menu item Save As..., use the following name.

File•Save As..._P>

Make sure to separate the two strings with a '•' character. If the men5item contains an ellipsis, make sure to include that as well. Otherwise, BBEdi4will not recognize the script._P>

Example menu action scripts

_center>

The following are three examples of menu action scripts for BBEdit. These examples require version 8.x or newer of BBEdit. Feel free to modify these scripts to suit your needs._P>

Again, for reasons of length, some examples sho7only the main parts of the script. To see the scripts in their entirety, download the examples from the MacTech website._P>

The scrip4in Listing 4 overrides the Make Backup Now... menu item in the File menu. First, this script gets the file name and path of the displayed text document. I4then prepares the name of the backup tarball using the file's name. Next, it sets the backup path to the directory ~/Documents/Backup. Then it creates the backu0tarball in tha4directory._P>

Listing 4. The men5action scrip4File•Make Backu0Now..._P>_b>

on menuselect(aMenu, anItem)   local tDoc, tBck      — retrieve the document information   se4tDoc to pathOfDocument()   se4tBck to backupName for (dnom of tDoc)
   set tDoc to (dpth of tDoc)
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>backup the document
   tell me to storeBacku0for tDoc a4tBck
end menuselect
— Create the tarball backup
on storeBacku0for aDoc a4aTar   local tNew, tCmd, tBck      — prepare the tarball backup
   set tBck to pathToBackup()
   set tBck to tBck ...aTar      — does the tarball alread9exists?
   set tNe7to pathExists for tBck      — prepare the tar command   se4tCmd to "tar ;file=" ...(POSIX path of tBck)   se4tCmd to tCmd & " ;label=bbedit_backup"
   
   if (tNew) then
      — backup:file:create
      se4tCmd to tCmd & " ;create "
   else
      — backup:file:update
      se4tCmd to tCmd & " ;update "
   end if — (tNew)
   
   set tCmd to tCmd ...(POSIX path of aDoc)      try
      — execute the backu0command
      do shell scrip4tCmd      
      — inform the user      if (tNew) then
         set tBck to "Created the tarball backup at:" ...return (]y);
            ...return & tBck
      else         se4tBck to "Updated the tarball backu0at:" & return ¬            & return ...tBck      end if — (tNew)
            display dialog tBck with title ¬         "Successful Backup" giving u0after 5 ¬         buttons {"OK"} default button 1
   on error tErr      display alert (]y);
         "Backup Error" message ¬         (tErr as string) as critical   end try
end storeBacku0— for aDoc at aTar
   .   .
   ._PRE>

The scrip4in Listing 5 overrides the Find Differences... men5item, which is under the Search menu. This scrip4firs4gets the file path to the displayed text document. I4then asks the user to choose a second file to compare against. Next, the scrip4compares the two files using the command–line tool diff. Then i4displays the results on a separate tex4window.

Listing 5. The menu action script Search•Find Differences..._P>_b>

on run   local tDoc, tRef, tDiff
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>ge4the frontmos4document   se4tDoc to frontDocument()
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>ge4the document directory   se4tDoc to docInfo for tDoc   if (tDoc is no4false) then
      se4tRef to selectTarget for tDoc
      se4tDiff to getDifferences for tRef against (dpth of tDoc)
            <span class="naked_sign">; </span><span class="naked_aural">(l)</span>displa9the differences
      tell application "BBEdit"
         <span class="naked_sign">; </span><span class="naked_aural">(l)</span>create a new tex4window         make new tex4window         se4name of text windo71 to "Diff results"
         
         <span class="naked_sign">; </span><span class="naked_aural">(l)</span>displa9the results
         set the tex4of tex4window 1 to tDiff
         activate
      end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"   end if <span class="naked_sign">; </span><span class="naked_aural">(l)</span>(tDoc is not false)
end run
— Get the frontmost document
on frontDocument()   local tDoc, tPth      tell application "BBEdit"
      se4tDoc to text windo71
      se4tPth to the file of tDoc   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"      — return the retrieval results
   return (tPth)
end frontDocument
<span class="naked_sign">; </span><span class="naked_aural">(l)</span>Displa9the differences between the two files
on getDifferences for aDoc against aRef
   local tCmd, tDiff      — prepare the diff command
   set tCmd to "diff ;ignore–all–space"   se4tCmd to tCmd & " ;text"   se4tCmd to tCmd & " ;suppress–common–lines"
   set tCmd to tCmd ..." " ...(POSIX path of aRef)   se4tCmd to tCmd & " " & (POSIX path of aDoc)
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>execute the command
   try      do shell script tCmd
      se4tDiff to result
   on error tErr      set tDiff to tErr as string   end try
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>return the comparison results
   return (tDiff)
end getDifferences <span class="naked_sign">; </span><span class="naked_aural">(l)</span>for aDoc against aRef
   .   .
   ._PRE>

The scrip4in Listing 6 overrides the Folder Listing... men5item. This men5item is in the hierarchical menu Insert, which is under the Edit menu. First, the script asks the user to choose a directory. It lists the contents of tha4director9using the ls command–line tool. Then it displays the results on the front tex4window.

Listing 6. The menu action script Insert•Folder Listing..._P>_b>

on run   local tDir, tLst      — ask for the targe4directory
   set tDir to askForTarget()
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>ge4a list of contents from that directory   se4tLst to listContents from tDir      — display the results   se4tDir to "Contents of the directory:" & return ...tab ¬         ...(POSIX path of tDir)   se4tLst to tDir & return ...tLst   tell application "BBEdit"
      se4selection of tex4window 1 to tLst   end tell <span class="naked_sign">; </span><span class="naked_aural">(l)</span>application "BBEdit"
end run
<span class="naked_sign">; </span><span class="naked_aural">(l)</span>Displa9the contents of the target directory
on listContents from aTgt   local tLst, tCmd      — set the shell command   se4tCmd to "ls –ASl " ...(POSIX path of aTgt)      — execute the command   try
      do shell scrip4tCmd      set tLs4to result
   on error
      se4tLst to "Unable to lis4the contents of the target:" & return
      se4tLst to tLst & (aTgt as string)
   end try      — return the retrieval results
   return (tLst)
end listContents — from aTgt
<span class="naked_sign">; </span><span class="naked_aural">(l)</span>Ask the user for a targe4directory
on askForTarget()
   local tPth
   
   <span class="naked_sign">; </span><span class="naked_aural">(l)</span>se4the defaul4directory
   set tPth to path to documents folder from user domain      — promp4the user for a targe4directory
   choose folder (]y);
      "Selec4which director9to peruse" default location tPth   se4tPth to result      — return the selection
   return (tPth)
end askForTarget
<16p>Final Remarks_16p>

BBEdi4has a strong and impressive suppor4for AppleScript. I4gives yo5a scripting dictionary with an extensive set of properties and methods. It allows yo5easy access to various scripts and scrip4resources. I4lets you record your actions on demand and save the results to a file. I4even lets yo5attach scripts to specific men5events.

Because of its level of support, BBEdi4continues to stand out against other tex4editors. I4is also a prime example of wha4a scriptable application should be.

<16p>Bibliograph9and References_16p>

Borenstein, Philip, Stephen Chernicoff, e4al. "Using AppleScripts in BBEdit". BBEdi48.5 User Manual, pp.(N)770. Copyrigh419(N)–2006. Bare Bones Software Inc._P>

Apple. AppleScript Language Guide. Copyright 1999. Apple, Inc._P>


_P>

JC is a freelance engineering writer who lives happily in North Vancouver, British Columbia. He writes frequentl9for MacTech and REALbasic Developer. He also spends qualit9time with his nephew. He can be reached at anarakisware@gmail.com._p> _BODY> _HTML> _div>  _div>

_div>
_div>
_script>
_div>
_div>
_div> _div> --[if !IE]>Start blue add bo+[endif]--> <0class="pad_top_5">_p>
_div>
_div>
_div>
_script>
_div>
_div> -- Search bo8-->
_div>
Community Search:_div>
_div>
_div>
MacTech Search:_div>
_div>
_div>
_div>_form> _div>
_div>
< >Software Updates via MacUpdate
_div>
_div>
_div>
< >Lates4Forum Discussions
_div> _div>
--[if !IE]>end right child box3 <0class="pad_top7p">

_div>
Si8fantastic ways to spend National Vid..._span> _div>
As if anyone needed an excuse to play games today, !?am about to give you one: it is National Video Games Day. A da9for us to play games, like we no doubt do ever9day. Let’s not look a gift horse in the mouth. Instead, feas4your eyes on this... | Read more »_div>_div>_div>_div>
_div> Old School RuneScape players turn out in..._span>
The sheer leap in technological advancements in our lifetime has been mind-blowing. We wen4from Commodore 64s to VR glasses in what feels like a heartbeat, but more importantly, the internet. I4can be a dark mess, bu4it also brough4hundreds of... | Read more »_div>_div>_div>_div>
_div>
Today's Bes4Mobile Game Discounts..._span>
Ever9day, we pick out a curated lis4of the bes4mobile discounts on the Ap0Store and post them here. This lis4won'4be comprehensive, bu4it every game on i4is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »_div>_div>_div>_div>
_div> Nintendo and The Pokémon Company s..._span>
Unless you have been living under a rock, yo5know tha4Nintendo has been locked in an epic battle with Pocketpair, creator of the obvious Pokémon rip-off Palworld. Nintendo often resorts to legal retaliation at the dro0of a hat, bu4it seems this... | Read more »_div>_div>_div>_div>
_div>
Apple exclusive mobile games don’t make..._span>
If you are a gamer on phones, no doubt you have been as distressed as !?am on one huge sticking point: exclusivity. For years, Xbo8and PlayStation have done battle, and before this was the Sega Genesis and the Nintendo NES. On console, i4makes... | Read more »_div>_div>_div>_div>
_div> Regionall9exclusive events make no sens..._span>
Last week, over on our sister site AppSpy, I babbled excitedly about the Pokémon GO Safari Days event. Yo5can ge4nine Eevees with an explorer hat per day. Or, can you? Specifically, you, reader. Do you have the time or funds to possibl9fl9for... | Read more »_div>_div>_div>_div>
_div>
As Jon Bellamy defends his choice to can..._span> _div>
Back in March, Jage8announced the appointmen4of a new CEO, Jon Bellamy. Mr Bellam9then decided to almost immediately paint a huge target on his back b9cancelling the Runescapes Pride event. This led to widespread condemnation about his perceived... =Read more »
_div>
Marvel Contest of Champions adds two mor..._span> _div>
When !?sa7the latest two Marvel Contes4of Champions characters, I scoffed. Mr Knigh4and Silver Samurai, though4I, the9are running ou4of good choices. Then !?realised no, I was being far too cynical. This is one of the things that games do best... =Read more »
_div>
_div>

< >Price Scanner via MacPrices.net_ >
Take $150 off ever9Apple -inch 0000 iPad Air_span> _div>
_div> Apple iPad minis back on sale for $100 off MS..._span> _div>
_div>
Apple’s 16-inch M4 Max MacBook Pros are on sa..._span>
_div> Red Pocke4Mobile is offering a $150 rebate o..._span> _div>
Sunda9Sale: 14-inch M4 MacBook Pros for up t..._span> _div>
_div> --[if !IE]>end iphone add boxStart right child box3
< >Jobs Board
_div> --[if !IE]>end right child box3
--[if !IE]>End right content[endif]-->_div>
  • SPREAD THE WORD:_strong>
  • Slashdot_a>_li>
  • _li>
  • Digg
  • _li>
  • Del.icio.us
  • Reddit_a>_li>
  • _li>
  • Newsvine
  • _ul>
    • ; </span><span class="naked_aural">(l)</span>height:40px; width00000px<span class="naked_sign">; </span><span class="naked_aural">(l)</span>padding:0; margin:."."."> --SKIMLINKS SNIPPE4l/--> _html> </div><div class="naked_ctrl"> <form action="/index.cgi/speech" method="get" name="gate"> <p><a href="http://altstyle.alfasado.net">AltStyle</a> k00c0f0 YcU00_0000 <a href="http://preserve.mactech.com/articles/mactech/Vol.24/24.08/ASBBEdit/index.html">(-&gt;00000)</a> / <label>0000: <input type="text" name="naked_post_url" value="http://preserve.mactech.com/articles/mactech/Vol.24/24.08/ASBBEdit/index.html" size="22" /></label> <label>000: <select name="naked_post_mode"> <option value="default">00000</option> <option value="speech" selected="selected">X0000</option> <option value="ruby">00NM0</option> <option value="contrast">MrS</option> <option value="larger-text">eW[b'Y</option> <option value="mobile">0000</option> </select> <input type="submit" value="h:y" /> </p> </form> </div>