AppleScript or JavaScript to place clipboard contents into a project


#1

Hello all,

I am working on a script, and I need to take the contents of the Mac system clipboard and place it into a project in TaskPaper.

Any ideas?


#2

I think this will do it:

function TaskPaperContext(editor, options) {	
	editor.replaceRangeWithString(0, 0, options.value);
}

var taskPaper = Application('TaskPaper')
taskPaper.includeStandardAdditions = true;  
var value = taskPaper.theClipboard();

taskPaper.documents[0].evaluate({
  script: TaskPaperContext.toString(),
  withOptions: { value: value }
});

#3

Hi Jesse,

Thanks for your help!

I tried putting your code after my script (currently an AppleScript set up in Keyboard Maestro). I put it into it’s own section (Execute JavaScript For Automation).

Do I need to customize your script? Like change value to a project? Thus far, I can’t get it to work.

Without changing your script, TaskPaper crashes when I run my script.

Keyboard Maestro reports the following:

/var/folders/ss/kyfs2xgd3wqft8cpz0rnhwcw0000gn/T/Keyboard-Maestro-Script-D3A6B0C9-48F9-49FB-B435-B6C43C5C0ABB:227:260: execution error: Error on line 9: Error: Connection is invalid. (-609)

#4

If you are already using Keyboard Maestro, you could do something like this (adds to the Top of the document):


#5

Yep, I tried that (although I used ⌘-⇧-H instead of going to the Outline menu)).

As I mentioned in the original post, I want to place the clipboard contents in a project (not at the top of the document).


#6

Yes, I expect so. My script was just demonstrating how to read a string from the clipboard and pass it into the TaskPaper API. Instead of doing editor.replaceRangeWithString you’d likely need to:

  1. Turn the string into items using the ItemSerializer

  2. Locate the project item that you want to add to and then add those deserialized items to that project.

Note all the public scripting API is documented here (questions still welcome of course).