BBEditLib: an object oriented AppleScript library for BBEdit

Using BBEditLib to create your own scripts.

The BBEditLib library was written in such a way that it could be reused by others. You can for example get rid of all the warning dialogs and the dialogs that require an input field and directly act via AppleScript on the frontmost BBEdit's disk browser window.

The BBEditLib contains for now 6 scripts objects which are in fact classes in object-oriented vocabulary.

The generalmessages and errormessages classes contains only properties which are the strings used in the dialog with the user. The dialog class contains subroutines, which are in object-oriented parlance "methods", that generate dialog windows for errors and user inputs. The unixpath class contain a method to translate Mac path (':') to Unix path ('/'). The diskBrowserWindow class is the most elabored class of the whole file since it contains up to 8 methods for dealing with BBEdit's disk browser windows. And finally, the textWindow class contains only one method for dealing with BBEdit's text windows.

Installation top

Download the BBEdit Disk Browser Suite and decompress the sit archive, then put the folder Disk Browser in the folder Script in the folder BBEdit Support in the BBEdit application's folder. Secondly, put the BBEditLib's file in your home Library (~/Library/BBEditLib).

How to build a script with BBEditLib top

Since BBEditLib is object-oriented, you need to wrap your mind around object-oriented thinking. I'll show you here how to create an object of the diskBrowserWindow class that duplicate the selected file or folder in the frontmost BBEdit's disk browser window. I will take for example the script Duplicate File in the Disk Browser Suite that I have slightly modified to make it more clearer.

Duplicate File listing


1 property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

2 set diskBrowserWindowObject to BBEditLib's diskBrowserWindow



3 tell diskBrowserWindowObject

4       set sel to getSelection ({})

5       if sel = false then return

6       duplicateFile ({thisSelection:sel})

7 end tell

The line 1 set the property BBEditlLib to the script objects contained in the BBEditLib file in the home Library.
The line 2 create an object (instantiate in object-oriented jargon) that will be allowed to access methods of the diskBrowserWindow class of BBEditLib. To instantiate the object to the diskBrowserWindow class of BBEditLib, the script could have also used the form diskBrowserWindow of BBEditLib instead of BBEditLib's diskBrowserWindow.

To talk to these methods, we have first to talk to the object. This is what you see in line 3. This put the methods in the diskBrowserWindow class in the name space of the diskBrowserWindowObject. On line 4 the script use the getSelection method of the diskBrowserWindow class with an empty record parameter to check if the selection in the frontmost window is a selected file in a BBEdit's disk browser window. If the selection is valid, we use it in line 6 as value for the thisSelection record that we pass as a parameter to the duplicateFile method of the diskBrowserWindow class.

To learn more on script objects, check the section "Script Objects" in the AppleScript Language Guide for AppleScript 1.3.7 from Apple.


dialog Class top


displayError (errormsg)

Action

Display an error message to the user with only a "OK" button.

Parameters

A string.

Return

true

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set dialog to BBEditLib's dialog



tell dialog

	displayError("An error occured.")

end tell



  --> true

		

displayErrorWithCancel (errormsg)

Action

Display an error message to the user with a Cancel and OK buttons.

Parameters

A string.

Return

true, false if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set dialog to BBEditLib's dialog



tell dialog

	displayErrorWithCancel("An error occured. Continue processing?")

end tell



  --> true, false if the user cancelled the dialog.

		

input (inputMsg, defaultInput)

Action

Display a dialog with an input field.

Parameters

inputMsg: A string containing the message to display to the user.

defaultInput: A string set as defaut in the input field.

Return

true, false if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set dialog to BBEditLib's dialog



tell dialog

	input("What is your prefered text editor?", BBEdit)

end tell



  --> {text returned:BBEdit, button returned:"OK"}, false if the user cancelled the dialog.

		

unixpath Class top


getPath (macpath)

Action

Get the Unix path of a Mac path.

Parameters

A string or an alias containing a Mac path to a file or a folder.

Return

A string containing the Unix path.

Note

This method doesn't handle accented characters.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set unixpath to BBEditLib's unixpath



tell unixpath

	getPath("HardDrive:Users:emmanuel:Radio UserLand:")

end tell



  --> "/HardDrive/Users/emmanuel/Radio\ UserLand/"

		

diskBrowserWindow Classtop


getSelection ({[warning:false, maxSelection:number]})

Action

Get the selection in the files pane of the disk browser window and enforce that!
Pass an empty list if there is no arguments like, for example, getSelection ({}).

Parameters in the record

warning: optional, default to true if missing.

maxSelection: optional, default to no limits if missing.

Return

A list of alias.

false if the selection is not valid.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	getSelection({warning: false, maxSelection: 1})

end tell



  --> {alias "HardDrive:Users:emmanuel:Radio UserLand:"}, false if the selection is not valid.

		

deleteFile ({thisSelection: selection, [warning: false, tryHarder: false]})

Action

Move to trash the selected file(s) and folder(s).
If a file like '.DS_Store' can't be deleted by the Finder, try the unix shell with 'rm sel' (and no, it will never use 'rm -rf sel').
In the latter case, the file will be completly deleted.

Parameters in the record

warning: optional, default to true if missing.

tryHarder: optional, default to true if missing (will use the unix shell with 'rm sel').

Return

a record: {deletedSel:list of alias, deletedTryHarder:list of unix paths strings}
If there is an error, deletedSel or/and deletedTryHarder lists can be empty.

false if there is an error of if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({})

	if sel = false then return

	deleteFile({thisSelection:sel})

end tell



  --> {deletedSel:{alias "HardDrive:Users:emmanuel:Trash:mytext.txt"},/

          deletedTryHarder:{"/HardDrive/emmanuel/Radio\\ UserLand/.DS_Store"}},

          empty record(s) if an error occured.

		

duplicateFile ({thisSelection: selection, [warning: false]})

Action

Duplicate the selected file or folder. Accept only one selection.

Parameters in the record

thisSelection: required, an alias.

warning: optional, default to true if missing.

Return

A list of alias of duplicated file(s).

false if there is an error.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({})

	if sel = false then return

	duplicateFile({thisSelection:sel})

end tell



  --> {alias "HardDrive:Users:emmanuel:Radio UserLand:www:#prefs copie 1.txt"},

         false if an error occured.

		

nameBrowser ({thisSelection: oneSelection, [warning: false, browserName: argString, inputMsg: argString, defautlInput: argString, defautlInputToContainerName: false, openBrowser: false, getParentContainer: false]})

Action

Name the name of frontmost disk browser window to whatever you want. The suggested name default to the name of the enclosing folder.
You can select in BBEdit one file or one folder to change the name of the disk browser window.
You can also create a new disk browser window if openBrowser is set to true.
It will create only one disk browser if you use it from the Finder (see the script Open in BBEdit's Browser).

Parameters in the record

thisSelection: required, an alias.

warning: optional, default to true if missing.

browserName: optional, default to false if missing, if set to true or a string, inputMsg and defaultInput will be disgarded, this bypass the dialog for asking the new name.
If is set to true, this will set the disk browserName to the parent container of the selected file or folder.
If set to a string, the disk browser window will be renamed to string.

inputMsg: optional, default to generalmessages's askDiskBrowserName if missing.

defaultInput: optional, default to an empty string ("") if missing.

defautlInputToContainerName : optional, default to the name of the enclosing folder if true or missing.

getParentContainer: optional, get the enclosing folder of the file or folder, default to true if missing

Return

The new name of the disk browser window.

false if there is an error or if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({maxSelection:1})

	if sel = false then return

	nameBrowser({thisSelection:sel, browserName: "DEV"})

end tell



  --> "DEV", false if an error occured.

		

newFile ({thisSelection: oneSelection, [warning: false, newFileName: argString, inputMsg: argString, defautlInput: argString, createNewFolder: true, openAfterCreated: true]})

Action

Create a new file or new folder.
If a file is selected, create the new file/new folder in the same folder.
If a folder is selected, create the new file/new folder in the selected folder.
Can create only one file/folder at a time.

Parameters in the record

thisSelection: required, an alias.

warning: optional, default to true if missing.

newFileName: optional, default to false if missing, if set, inputMsg and defaultInput will be disgarded, this bypass the dialog for asking the new file/folder name.

inputMsg: optional, default to generalmessages's askFileName or to generalmessages's aksFolderName if createNewFolder is true.

createNewFolder: optional, default to false if missing.

openAfterCreated: optional, default to false if missing, ignored if createNewFolder is true.

Return

An alias to the new file.

false if there is an error or if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({maxSelection:1})

	if sel = false then return

	newFile({thisSelection:sel, newFileName:"myDoc.txt"})

end tell



  --> alias "HardDrive:Users:emmanuel:Radio UserLand:www:myDoc.txt", false if an error occured.

		

renameFile ({thisSelection: oneSelection, [warning: false, newFileName: argString, inputMsg: argString, defautlInput: argString]})

Action

Rename a file or a folder.
Can rename only one file/folder at a time.

Parameters in the record

thisSelection: required, an alias.

warning: optional, default to true if missing.

inputMsg: optional, default to generalmessages's askFileName if missing.

createNewFolder: optional, default to false if missing.

openAfterCreated: optional, default to false if missing, ignored if createNewFolder is true.

Return

An alias to the new file.

false if there is an error or if the user cancelled the dialog.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({maxSelection:1})

	if sel = false then return

	renameFile({thisSelection:sel, newFileName:"myDoc2.txt"})

end tell



  --> alias "HardDrive:Users:emmanuel:Radio UserLand:www:myDoc2.txt", false if an error occured.

		

revealFile ({thisSelection: oneSelection, [warning: false]})

Action

Reveal in the Finder the selected file or folder.
Accept only one file in the selection.

Parameters in the record

thisSelection: required, an alias.

warning: optional, default to true if missing.

Return

A list of alias to the revealed file(s)

false if there is an error.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set diskBrowserWindow to BBEditLib's diskBrowserWindow



tell diskBrowserWindow

	set sel to getSelection({maxSelection:1})

	if sel = false then return

	revealFile({thisSelection:sel})

end tell



  --> alias "HardDrive:Users:emmanuel:Radio UserLand:www:myDoc.txt", false if an error occured.

		

textWindow Class top


isTextWindow ({})

Action

Check if the frontmost window is a text window.

Parameters

None, pass an empty list.

Return

A record: {allTestsOK: true or false [filepath: alias to a file, containerpath: alias to a container, errormsg: an error message]}
allTestOK: true if class of window 1 is text window, false otherwise.
filepath: alias to the file if allTestOK is true.
containerpath: alias to the container of the file if allTestOK is true.
errormsg: an error message if allTestOK is false.

Example

property BBEditLib : (load script file ((path to "dlib" from user domain as string) & "BBEditLib"))

set textWindow to BBEditLib's textWindow



tell textWindow

  isTextWindow()

end tell



  --> {allTestsOK:true, filepath:alias "UNO:Users:mem:Radio UserLand:myText.txt",/

          containerpath:alias "UNO:Users:mem:Radio UserLand:"}, if false return

         {allTestsOK:false, errormsg:"Sorry, this is not a text window."}

   

		

BBEditLib Listing top

BBEditLib listing (open a new window).

Version History top

Version 1.0.3 - February 4 2003

Fixed a small bug in the deleteFile method of the diskBrowserWindow class of BBEditLib.

First release: Version 1.0.2 - February 3 2003

Tested on OS X 10.2.3 with BBEdit 7.0.1.

Requirements top

Download top

BBEdit Disk Browser Suite

Comments top

If you want to add a comment, please do so on my weblog.