Add Task via LaunchBar

LaunchBar is my primary launcher, so I wanna add task into TaskPaper via LaunchBar. If possible, the task should appear at the bottom of project “Inbox”.

So I write a javascript for LaunchBar, it works in Script Editor but alert me that “can’t find variable Application” when I run it in LaunchBar. Here is my script and the error message:

// LaunchBar Action Script

function run(argument) {
    if (argument == undefined) {
        // Inform the user that there was no argument
        LaunchBar.alert('No argument was passed to the action');
    } else {
        // Return a single item that describes the argument
        return [{ title: '1 argument passed'}, { title : argument }];
    }
}

var TaskPaper = Application('TaskPaper')

TaskPaper.documents[0].evaluate({
	script: TPContext.toString(),
	withOptions: {text:"- " + argument }
});

function TPContext(editor, options) {
	var outline = editor.outline;
	var inbox = outline.evaluateItemPath("//Inbox:")[0];
	var items = ItemSerializer.deserializeItems(options.text, outline, ItemSerializer.TEXTMimeType);
	
	if (!inbox) {
		inbox = outline.createItem("Inbox:");
		outline.root.appendChildren(inbox, outline.root.firstChild);
	}
	
	inbox.appendChildren(items, inbox.firstChild);
}

The codes mostly from @jessegrosjean in this page and do work well in Apple Script Editor. I can’t find out where the problem is. Anyone can help me?

To be honest, It tribbles me for the whole weekends😂

I’m not sure, but you might have better luck asking in LaunchBar forum… though would know more about differences in their script context vrs the standard ScriptEditor context.

A LaunchBar JavaScript rather than TaskPaper issue, I think. The Application library object is only present in Apple’s ‘JavaScript for Automation’ (JXA) JavaScript evaluation contexts, but my understanding is that LaunchBar has its own internal JavaScript context in which the Application object would not be available.

It’s a while since I looked at Launchbar actions, but you may get more help on application launching within LaunchBar Javascript on in a LaunchBar scripting forum.

1 Like

Thank you for your pointing the differences, I am reading the LaunchBar documents……hope I can solve it :muscle:

Yes, I think the problem may caused by the different variable name in LaunchBar. When I update my script I would post it here :D……only if I can……

Your script almost works. You were just missing the function that takes the input from LaunchBar:

// LaunchBar Action Script

function handle_string(newTask) {
    var TaskPaper = Application('TaskPaper');
    TaskPaper.documents[0].evaluate({
        script: TPContext.toString(),
        withOptions: {text:"- " + newTask }
    });
}

function TPContext(editor, options) {
    var outline = editor.outline;
    var inbox = outline.evaluateItemPath("//Inbox:")[0];
    var items = ItemSerializer.deserializeItems(options.text, outline, ItemSerializer.TEXTMimeType);

    if (!inbox) {
            inbox = outline.createItem("Inbox:");
            outline.root.appendChildren(inbox, outline.root.firstChild);
    }

    inbox.appendChildren(items, inbox.firstChild);
}

Since this is JXA you have to save it as .scpt (not as .js). That is, when creating the script from within LB’s Action Editor choose “AppleScript” as script type.

And set the Action to require an argument and to accept a string argument, like this:

Here is the complete Action: TaskPaper-AddTask.lbaction.zip (5.1 KB)

5 Likes

It surprised me that I can run a javascript in “AppleScript” type……

Your reply helps a lot .Thanks!:laughing:

With JXA, it’s not that you can, it’s rather that you have to, as far as I know. Maybe it helps to see JXA (JavaScript for Automation) as an improved variant of AppleScript. If you open Script Editor you will notice that you can choose from AppleScript and JavaScript:

If you choose JavaScript the file type (extension) will not change. A bit confusing, yes.


Concerning the functions/handlers:

If you write more LaunchBar Actions then you really should read through the LaunchBar Developer Documentation. It gives you all the information about how to connect the script to LaunchBar’s input/output.

For the functions in JXA you have to use the AppleScript function names, like handle_string in the above script. (LB’s JS function runWithString does not work in JXA, only in regular JavaScript.)

For more complex Actions I would write the main script in JavaScript (.js) and delegate the automation tasks to sub-scripts written in AppleScript or JXA (.scpt).
This allows you to use LaunchBar’s extra features for JavaScript, which are not available in JXA or AppleScript.

Yes, I hardly noticed the difference as I just write my first “total LaunchBar Action”, but since these Documentation I would think more in the LaunchBar way :smiley:

I just noticed this thread. You can use my TaskPaper action found in my repository: GitHub - raguay/MyLaunchBarActions: This is my public repository for LaunchBar 6 Actions.

Try it out and see if it works for you. I use it just fine.

1 Like

This action works smoothly and I like it. Though I’ve updated my action with some similar functions, I can learn some different thoughts in your codes and them did help.

Thanks a lot.:star_struck:

I’m glad it was useful. It works great for me.