2

I open a lot of new documents in Textwrangler/BBedit and I would like them to always have the date printed at the top. I would like this to be automatic so that I don't have to remember to run a script each time.

I'm new to BBEdit but I really like Textwrangler and have used it for years. I read some of the documentation on BB and I think that attaching some Applescript to an event might be the way to go. However, none of the listed events seem quite right, and I don't really want to add dates to existing documents.

I found the following page which was a good starting point: http://bbeditextras.org/wiki/index.php?title=Scripting_and_Automation

I also found these relevant hooks from the BB docs: App attachment points

  • applicationDidFinishLaunching: called when the application has completed startup.
  • applicationShouldQuit: called when you choose the Quit (or the application receives a ‘quit’ event for any other reason).
  • applicationDidQuit: called when the application has finished shutting down and is about to exit.
  • applicationDidSwitchIn: called when BBEdit has been brought to the foreground.
  • applicationWillSwitchOut: called when BBEdit is being put into the background.

Document attachment points

  • documentDidOpen: called when a document has been opened and is ready for use. (Since BBEdit supports multiple types of documents, your script should allow for the argument to be a document of any type.)
  • documentShouldClose: called when the application is preparing to close a document.
  • documentDidClose: called when the application has closed a document.
  • documentShouldSave: called when the application is trying to determine whether a given document should be saved.
  • documentWillSave: called when the application is about to begin saving a document. (note that this will only be called after a successful return from a ‘documentShouldSave’.
  • documentDidSave: called after a document has been saved successfully.
  • documentWillUnlock: called when BBEdit is going to make a document writeable. (For example, when you click the pencil to unlock a document)
  • documentDidUnlock: called when BBEdit has successfully made a document writeable.
  • documentWillLock: called when BBEdit is going to make a document read-only.
  • documentDidLock: called when BBEdit has successfully made a document readonly.

I don't know if any of those really fit, though. I could also try adding some scripts into the startup folder, but I'm not sure how I would go about say, adding a date to all open documents. I've never done applescript before so it's a little trial and error.

I have this code that I've tried running by itself, and it works fine:

tell application "BBEdit"
tell text window 1
select insertion point after (last character)
set selection to ((current date) as string)
end tell
end tell

I'm just a little lost as to how to get the above code to execute on file creation.

CGanote
  • 93
  • 6

2 Answers2

0

Open Script Editor and paste the following code in a new script document:

use BBEdit : application "BBEdit"
use scripting additions

on documentDidOpen(doc)
    set n to the doc's name
    set t to the doc's text as string

    if n does not start with "untitled text" then return
    if t's length > 0 then return

    set the contents of the doc to (the (current date) as text) ¬
        & linefeed & linefeed
end documentDidOpen

Save it as type script (extension .scpt), and name it Document.documentDidOpen.scpt. Either save it directly, or move it subsequently, to the folder ~/Library/Application Support/BBEdit/Attachment Scripts/; if the folder doesn't exist, create it.

Restarting BBEdit ought not to be necessary, but also couldn't hurt. Now, whenever you create a new document (of any type), it will be headed with the current date and time.

CJK
  • 4,587
  • 1
  • 4
  • 21
  • I fear not; I created the folder and saved the code, but it doesn't look like it's invoking. Any advice on debugging? – CGanote Jan 01 '19 at 20:50
  • Edit the script's `documentDidOpen` handler and insert a line at the very start of it that reads `return display alert "Hello"`. You can edit the script from its current location, then simply save it. Now either open a document in _BBEdit_ or create a new one. Does the alert appear ? Also, can you please tell me what versions of macOS and _BBEdit_ you are using ? – CJK Jan 01 '19 at 20:56
0

Try using BBEdit's 'Attaching Scripts to Menu Items' feature (p 295 of the v11 User Manual). In a nutshell, if you save a script to the Menu Scripts folder with a name based on the menu/command, then this script will run when that menu item is selected. So in your scenario:

Save the script below to BBEdit's Menu Scripts folder with the filename 'New•Text Document'.

tell application "BBEdit"
    set cDate to ((current date) as text)
    make new document with properties {contents:cDate}
end tell

As an aside, you can generally avoid using selection with the insertion point, for example:

tell document 1 of application "BBEdit" to set text of ¬
    first insertion point of text 1 to ((current date) as text)

In your second 'question' scenario, you could probably cycle through all existing windows with that, for example:

tell application "BBEdit"
    set tdCount to count of text documents
    repeat with i from 1 to tdCount
    set text of first insertion point of text 1 of ¬
        text document i to ((current date) as text) & linefeed
    end repeat
end tell
Mockman
  • 292
  • 2
  • 8