Using a script to fill the search box


#1

Is there a way to set the search box in TaskPaper 3 through a script? I could do this in version 2 by using Applescript to tell TaskPaper 2 to:

set search field string to whateverText

This was handy because I could then use Alfred to trigger a search of my task list. I’m trying to figure out how to accomplish the same thing in TP3 preview. I appreciate any suggestions on how to go about this.


#2

For the moment, one way might be to run a JavaScript for Automation script like this:

( Change "//@done" – the argument at the end of the script – to whatever search string you need, preserving single or double quotes )

(function (strQuery) {
    'use strict';

    function fnSetSearch(editor, options) {
        return editor.itemPathFilter = options.query;
    }
    
    var ds = Application("TaskPaper").documents;
    return ds.length ? ds[0].evaluate({
        script: fnSetSearch.toString(),
        withOptions: {
            query: strQuery
        }
    }) : false;

})("//@done");

( In Script Editor, change the language selector at top left from AppleScript to JavaScript )


#3

To call the same function from AppleScript:

property pstrQuery : "//@done"

tell application "TaskPaper"
	
	set strJS to "
		function fnSetSearch(editor, options) {
			return editor.itemPathFilter = options.query;
		}
	"
	set lstDocs to documents
	if lstDocs ≠ {} then
		evaluate item 1 of lstDocs script strJS with options {query:pstrQuery}
	end if
	
end tell

#4

Thank you! I had been trying to figure out how to use the first script within Alfred, but your second post that demonstrated how to call the same function using Applescript worked perfectly. It runs well within Alfred (v1) as a tag search and gives me a starting point for adapting other scripts.

on alfred_script(q)
tell application "TaskPaper"
	set strJS to "
		function fnSetSearch(editor, options) {
			return editor.setQuery(
				options.query
			);
		}
	"
	set lstDocs to documents
	if lstDocs ≠ {} then
		evaluate item 1 of lstDocs script strJS with options {query:"//@" & q}
	end if
end tell
end alfred_script

#5

When I tried running complexpoint’s script in Script Editor, I got the following error message:

"TypeError: editor.setQuery is not a function. (In ‘editor.setQuery(
options.query
)’, ‘editor.setQuery’ is undefined)

fnSetSearch
evaluateScript@file:///Applications/TaskPaper.app/Contents/Resources/dist/birch.js:49467:16

Use the Help > SDKRunner to debug"

Any idea what this message means and how I can fix it?

Thanks!


#6

Thanks for catching that – there have been a few adjustments to the scripting interface in this last build of TaskPaper 3 – I will update it in the (EU) morning.

UPDATE done

(The earlier .setQuery() method is now replaced by the R/W .itemPathFilter property)


#7

Thank you so much!