DevonThink Pro - adding entry as link in TaskPaper 3

DevonThink Pro has a script Add as To Do to TaskPaper. This works for TaskPaper 2 and not in TaskPaper 3. It adds to the Inbox: in Taskpaper an entry with a link to the relevant entry in DevonThink Pro.

The script is at the end. According to the information when I posted on the DevonThink Pro site, the following line:

set theProject to make new «class TPpr» with properties {name:“Inbox”}

fails, with the following error message:

Can’t make class «class TPpr».

Full details on the problem at: Script: Add as todo to TaskPaper - Tips - DEVONtechnologies Community

Would be possible to amend the script to make it work with TaskPaper 3. I have not skill/knowledge in AppleScript/JavaScript so cannot fix myself. Any help would be gratefully received.

Code for script:

– Script to add a selected record to Task Paper as a to do
– Written by Eric Böhnisch-Volkmann, Version 1.0.1, Jan 28, 2010
– © 2011-2014 DEVONtechnologies, LLC
– Adapted by The Geek Inside thegeek@thegeekinside.net
– based in the Add ToDo to TaskPaper.

– Set properties
property pTags : “devonthink”

try
– Get the selection
tell application id “DNtp” to set thisSelection to the selection

– Error handling
if thisSelection is {} then error localized string “Please select a document or group, then try again.”
if (length of thisSelection) > 1 then error localized string “Please select only one document or group, then try again.”

– Get and format the data we need
tell application id “DNtp”
set thisItem to first item of thisSelection
set theSummary to (name of thisItem) as string
set theURL to (reference URL of thisItem) as string
end tell

– Add the tags to the summary, a speciality of Task Paper
set theSummary to (theSummary & " /" & (localized string pTags)) as string

tell application “TaskPaper”
tell front document
try
set theProject to «class TPpr» named “Inbox”
on error err
set theProject to make new «class TPpr» with properties {name:“Inbox”}
end try
tell theProject
set taskEntry to make new «class TPer» with properties {name:"- " & theSummary}
tell taskEntry
make new «class TPtg» with properties {name:pTags}
make new «class TPer» with properties {name:theURL}
end tell
end tell
end tell
end tell

on error errMsg

display alert (localized string “Error when adding item to Task Paper”) message errMsg

end try

1 Like

Here is a first draft of an updated version for TaskPaper 3 which:

  • Allows you to select one or more items in DEVONthink for copying
  • Displays a notification of results
  • Has a couple of options at the end of the script:
    • Name of inbox project (defaults to Inbox:)
    • Name of any @tag to add to the imported links

To test and install, see http://guide.taskpaper.com/using_scripts.html

Copy the whole of the source below, and paste it into Script Editor:


// Select one or more items in DEVONThink
// and run this script to copy them (name and link back to DT)
// into an inbox project in the active TaskPaper document

// Draft 0.03
// Rob Trew  Twitter: @complexpoint

(function (dctOptions) {
    'use strict'

    // TASKPAPER CONTEXT
    function TaskPaperContext(editor, options) {
        'use strict'

        // projectFoundOrCreated :: String -> Item
        function projectFoundOrCreated(strProjectName) {
            var outline = outline || editor.outline,
                lstExisting = outline.evaluateItemPath(
                    '//@type=project and ' + strProjectName
                ),
                found = lstExisting.length > 0 ? (
                    lstExisting[0]
                ) : undefined,
                created = !found && outline.createItem(strProjectName + ':');

            return found || (
                outline.root.appendChildren(created),
                created
            );
        }

        // concatMap :: (a -> [b]) -> [a] -> [b]
        function concatMap(f, xs) {
            return [].concat.apply([], xs.map(f));
        }

        var outline = editor.outline,
            inboxProject = projectFoundOrCreated(
                options.inboxProject || 'Inbox'
            ),
            strTag = options.tag || '',
            lstItems; // Descriptions and indented links to be created

        outline.groupUndoAndChanges(function () {
            // Alternate description items and url notes
            lstItems = concatMap(
                function (dct) {
                    return [
                        outline.createItem('- ' + dct.name +
                            (strTag ? ' @' + strTag : '')),
                        outline.createItem(dct.url)
                    ];
                },
                options.dtItems
            );

            lstItems.forEach(function (item, i, lst) {
                //parent is alternately Inbox or previous item (for url)
                (i % 2 ? lst[i - 1] : inboxProject)
                .appendChildren(item)
            })
        });

        return lstItems.map(function (x) {
            return x.bodyString;
        });
    }


    // JAVASCRIPT FOR AUTOMATION CONTEXT

    // Selection(s) in DevonThink
    var lstSelns = Application("com.devon-technologies.thinkpro2")
        .selection()
        .map(function (x) {
            return {
                name: x.name(),
                url: x.referenceURL()
            }
        });

    if (lstSelns.length > 0) {

        var tp3 = Application('TaskPaper'),
            ds = tp3.documents;

        if (ds.length) {
            dctOptions.dtItems = lstSelns;

            var lstResult = ds[0].evaluate({
                script: TaskPaperContext.toString(),
                withOptions: dctOptions
            });

            if (lstResult instanceof Array) {
                var a = Application.currentApplication(),
                    n = lstResult.length,
                    strSuffix = (n > 1 ? 's' : '');

                tp3.activate();

                (a.includeStandardAdditions = true, a)
                .displayNotification(lstResult.join('\n'), {
                    withTitle: 'Link' + strSuffix +
                        ' copied from DevonThink to TaskPaper 3',
                    subtitle: n + ' link' + strSuffix +
                        ' in ' + (dctOptions.inboxProject ||
                            'Inbox') + ' project'
                })
            }
        }
    }
})({
    tag: 'devonthink',
    inboxProject: "Inbox"
})
1 Like

Thank you very, very much!

1 Like

Seconded! :grinning:

1 Like

Updated to 0.02 above. (bug fix)

1 Like

Update works a treat, thanks again.

1 Like

Psyched to find this, but it’s not quite working for me — I’m getting an error, “Application can’t be found,” and it’s TP3 the script is failing to find. But I think the reference name is right: “com.hogbaysoftware.TaskPaper3”. Or am I mistaken? I’m running TP 3.6 (v. 283).

Change the reference to “TaskPaper” (more universal) or “com.hogbaysoftware.TaskPaper3.direct” should do the trick

Thanks - that right, and I’ve updated it above (Ver 0.03)

Should work with all recent builds of TaskPaper 3 now, I think.

(Just using Application("TaskPaper") is one approach, assuming you don’t have TaskPaper 2 installed)

Thanks to both you and @GuiB — that indeed did it. Sweet! I’ve used TP since v1, but TP3 is the first to become my main project management app… and the forum’s support certainly helps.

I copied the script exactly into Script Editor - as far as my limited knowledge goes it seems ok. I saved it and placed it in the DEVONthink scripts folder. Clean reboot.

But when I select a file in DEVONthink, and then select the TaskPaper script, DEVONthink just freezes.

I don’t know much about scripts, (have managed a couple which worked ok - one example was DEVONthink to Things), but only very limited actual editing.

I have OS X 10.12.2 and DEVONthink 2.9.8

I wondered if maybe I have some security settings which prevent Javascript from running?

Ideally what I would like is the same capability as the below script, but for TaskPaper instead of Things. (This script works very well, and te idea behind it may appeal to others.

– Script to automatically scan a text file, extract task items and send them to Things
– Written by Luc Beaulieu, Version 1.1, March 12, 2015
– Re-use portions of a script “Add as To Do to Things” written by Eric Böhnisch-Volkmann, Version 1.0.1, Jan 28, 2010

– Set properties
property pTags : "DEVONthink"
property pPrefix : “” – Prefix for the created to do item

try

-- Get the selection
tell application id "DNtp"
	set thisSelection to the selection
	set theRecord to the first item of (selection as list)
	set theType to the type of theRecord
	set pType to theType as string
end tell

-- Error handling: do not continue if these occurs
if thisSelection is {} then error localized string "Please select a document, then try again."
if (length of thisSelection) > 1 then error localized string "Please select only one document, then try again."
if pType is equal to "group" then error localized string "Trying to parse a Group/Folder. Select a file, then try again"


-- Now time to get and format the DATA we need	
set pLocalizedTags to localized string of pTags
tell application id "DNtp"
	set theText to plain text of theRecord
	set theLines to paragraphs of theText
	set thisItem to first item of thisSelection
	set theSummary to (name of thisItem) as string
	set theURL to ("[url=x-devonthink-item://" & uuid of thisItem & "]" & name of thisItem & "[/url]") as string
end tell

-- Iterating through each line, one by one, for the string delimeter "ACTION:"
-- and create a new task in Things global Inbox if appropriate
set textDelim to "ACTION:"
set nTask to 0

repeat with eachLine in theLines
	set nextLine to eachLine
	set finalTask to ""
	if nextLine contains textDelim then
		set AppleScript's text item delimiters to textDelim
		set theTask to item 2 of every text item of nextLine
		set AppleScript's text item delimiters to ""
		set finalTask to finalTask & theTask
		-- set finalTask to finalTask & theTask & return
	end if
	if finalTask is not equal to "" then
		set nTask to nTask + 1
		tell application id "Thgs"
			-- create task entry in Inbox with a link back to the meeting notes
			make new to do with properties {name:finalTask, notes:theURL, tag names:pLocalizedTags}
		end tell
	end if
end repeat

display dialog (nTask as string) & " were created!"

on error errMsg

display alert (localized string "Error extracting task") message errMsg

end try

Confirmed.

The script does, in fact work. Occasionally, DEVONthink has a problem with some scripts that are added to its private script folders in Application Support. If you compile the script and add it to ~Library/Scripts/DEVONthink (the global OS X scripts folder for your user account) then you can invoke the script successfully from the OS X scripts menu. Over here I’ve done that and can verify the script operates as expected.

Thank you Edvard - works fine now. It’s a good tip to know this for the future.

That script written by Luc Beaulieu is very useful if you take notes on an iPad.

He writes his notes in a text file on his iPad, and prefixes any Task items by the term “ACTION:”

Then once copied across to DEVONthink on his Mac, he runs the script on this text file and it automatically scans, extract the task items and sends them as individual tasks to Things.

Would anyone here see the value in copying this script for TaskPaper? I’d love to do it but while having a (very) limited knowledge of AppleScript, I have zero knowledge of Javascript.