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.