Quick Save NVAlt Note from BBEdit

I'm new to Keyboard Maestro and so I'm sharing this macro for feedback, and in case anyone might find it useful.

I use NVAlt as a hub to my notes, organising them in Dropbox, but also syncing to Simplenote (not sure why these days…). While NVAlt is akin to my notes database, I don't like starting new notes in NVAlt, and prefer to start them in BBEdit.

I wrote this macro to simplify saving a new file as a note in the style where the note title begins as the first line (à la Simplenote), but ultimately is saved as the file name on disk.

I have this macro mapped to ⌘S since I often save new files as notes. The AppleScript determines whether the file is on disk. If it is, it exits, and KBM triggers the normal BBEdit 'Save' menu. If it is not, it saves the text of the first line, then prompts whether to save as a note or normal file.

In the case of a note, first an AppleScript deletes the first two lines of the file, then KBM triggers the 'Save As…' menu item, types a ~ to get the Go To panel. It enters the Notes folder of my Dropbox, then sets the name of the file to the first line/title with a .txt extension. The user is then left to press the final 'Save' button.

Any feedback would be greatly appreciated; there's still tons left for me to learn with Keyboard Maestro. Cheers.

Save Note.kmmacros (10.5 KB)

Hello Ryan,

I would go about that a little differently myself.

This will instantly save as a note-file if unsaved — if already on-disk it will save.

There is error-checking to prevent overwriting an existing document.

# Auth: Christopher Stone
# dCre: 2015/08/30 12:55
# dMod: 2015/08/30 13:07 
# Appl: BBEdit
# Task: Save as note file for nvAlt.
# Tags: @Applescript, @Script, @BBEdit, @Save, @Note, @nvAlt

  set dropBoxNoteFolder to "~/Dropbox/Notes/"
  set homeFolderPosix to POSIX path of (path to home folder)
  if dropBoxNoteFolder starts with "~/" then set dropBoxNoteFolder to homeFolderPosix & (text 3 thru -1 of dropBoxNoteFolder)
  if not exTant(dropBoxNoteFolder) then
    do shell script "mkdir -p " & quoted form of dropBoxNoteFolder
  end if
  tell application "BBEdit"
    tell front document
      if on disk then
        set line1Text to contents of line 1
        if length of line1Text > 70 then set line1Text to text 1 thru 70 of line1Text
        set line1Text to line1Text & ".txt"
        set fileCheckPath to dropBoxNoteFolder & line1Text
        if exTant(fileCheckPath) of me then
          error "A file at this path already exists:" & return & return & fileCheckPath
          save it to POSIX file fileCheckPath
          delete text of (lines 1 thru 2)
        end if
      end if
    end tell
  end tell
on error e number n
  set e to e & return & return & "Num: " & n
  if n ≠ -128 then
      tell application (path to frontmost application as text) to set ddButton to button returned of ¬
        (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
          default button "OK" giving up after 30)
      if ddButton = "Copy Error Message" then set the clipboard to e
    end try
  end if
end try

on exTant(_path) # Takes an HFS, Posix, or ~/Posix path as input.
    if _path is "~" or _path is "~/" then
      set _path to (POSIX path of (path to home folder as text))
    else if _path starts with "~/" then
      set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
    end if
    if _path starts with "/" then
      alias POSIX file _path
      alias _path
    end if
    return true
  on error
    return false
  end try
end exTant

I would give it a hotkey like Cmd-Shift-Ctrl-N (for note) instead of hijacking the Cmd-S key.

Instead of a simple error-check for an existing document name I would use a date-time stamp to create a unique new document name.


Thanks for the feedback! I’m relatively new to AppleScript, so I can learn a lot from your script; I appreciate it.