The Flying Meat Wiki : VoodooPadAddToComposersPythonScript

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register
This script will take the word that the cursor is currently on, and add it to a page named "Composers". The idea is to quickly append a single word to a known page. If you hit "Grab file" it will be downloaded as a .py.txt file. Please delete the .txt off the end and just use the .py file extension.

This script requires the Python Plugin Enabler plugin.

AddtoComposers.py
# This script goes in:
# ~/Library/Application Support/VoodooPad/Script PlugIns/
# make sure the filename is "AddtoComposers.py"

VPScriptSuperMenuTitle = "Composer"
VPScriptMenuTitle = "Add to Composers Page"

pageNameToAddTo = "Composers"

def main(windowController, *args, **kwargs):
   
    NSSelectByWord  = 1
    document        = windowController.document()
    textView        = windowController.textView()
   
    # let's find out what the range of the word at our current cursor location is.
    wordRange       = textView.selectionRangeForProposedRange_granularity_(textView.selectedRange(), NSSelectByWord)
   
    # catch the case where we've hit a space after our last word...
    if (wordRange.location > 0) and (wordRange.length == 1):
        wordRange.location = wordRange.location - 1
        wordRange = textView.selectionRangeForProposedRange_granularity_(wordRange, NSSelectByWord)
   
    if (wordRange.length == 1):
        # bah.  we've got nothing.
        return
   
    # let's grab the text that we want to append to our page
    textToAdd = textView.textStorage().mutableString().substringWithRange_(wordRange)
   
    # Let's create the page we want to add to.
    # it'll just do nothing if the page name already exists.
    document.createNewPageWithName_(pageNameToAddTo)
   
    # grab the page out of the document
    page = document.pageForKey_(pageNameToAddTo)
   
    # grab the contents of the page.
    attributedString = page.dataAsAttributedString()
   
    # append a newline and our new text to the page
    attributedString.mutableString().appendString_("\n" + textToAdd)
   
    # save that data back to the page.
    page.setDataAsAttributedString_(attributedString)
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.3
Page was generated in 0.0616 seconds