# -*- coding: utf-8 -*- ''' :Title Make Sub-page Link :Author Flying Meat :Website http://flyingmeat.com/ :Planguage Python :Requires VoodooPad 3.5+ :Description What this script does is make a new page based off of the current page name and the currently selected text. For example, let's say you are on a page named "Social Experiments", and you had the text "Method" selected. Running this script would create a new page named "Social Experiments.Method", and a custom link to the word "Method" EOD ''' VPScriptMenuTitle = "Make Sub-page Link" import AppKit def main(windowController, *args, **kwargs): page = windowController.currentPage() textView = windowController.textView() document = windowController.document() # grab the selected text out of our text view. selectedText = textView.string().substringWithRange_(textView.selectedRange()) # remove any whitespace on it. selectedText = selectedText.strip() if len(selectedText) <= 0: print("There was no text selected") return # make our new page name, based on the current page and the selected text newPageName = page.displayName() + u"." + selectedText # tell the document to make a new page, with our new name. newPage = document.createNewPageWithName_(newPageName) # This is the voodoopad item url scheme. We're going to add a custom # link to the selected text with it, but first we have to make the link link = "x-voodoopad-item://" + newPage.uuid() # this helps with undo. First we ask the text view if it's ok to change the selection if textView.shouldChangeTextInRange_replacementString_(textView.selectedRange(), None): # and then set the link. textView.textStorage().addAttribute_value_range_(AppKit.NSLinkAttributeName, link, textView.selectedRange()) # if we wanted to then open the page, we'd do this: # document.openPageWithTitle_(newPageName)