Templater to execute user system commands?

What I’m trying to do

I am trying to execute a templater user system command that calls the mac command line utility iCalBuddy. However since this only works on desktop, I am trying to embed some conditional logic into the template to only call this command if the template is run on the desktop

Things I have tried

The getEvents() user command is working fine when executed directly using
<% tp.user.getEvents() %>
However when I embed some logic to test for whether the template is executed from the desktop as below, the script returns nothing. Tested the conditional logic and that part is working fine too. Other tp.* commands like tR=tp.date.now() seem to work fine tR=tp.user.getEvents() returns [object Promise]

if (!app.isMobile) { tp.user.getEvents() } 

Somehow can’t get this to work. Really appreciate any suggestions!

Whenever you get a promise in return, you should have added await in front of that call. That’s the easy explanation, so try await tp.user.getEvents(), and if you’ve written that function there is some function within it which needs to be awaited too.

Thanks @holroy!
But the template I shared above returns nothing when inserted (no errors) ; with or without await added.

if (!app.isMobile) { tp.user.getEvents() } 

The promise was your first majir error. When it still fail you need to take steps to verify if the template itself runs by adding extract stuff to be output from inside and outsideb of the if statement.

You might also want to add some debug output from within your function to verify its being called. This you need to repeat until you find out why your logic isn’t functioning.

From what you’ve given us there are two obvious cases why it wouldn’t output anything:

  • There is no output given if your if test fails
  • There is no mention of setting tR in this block (and we don’t have any clue as to what happens inside that function)

Sorry for leaving out those details. But your tips indeed helped me resolve this!
Here is the working snippet that detects first if it is being run on desktop (non-mobile)

if (!app.isMobile) { tR = await tp.user.getEvents(); } 

Since I already typed this out, for the curious, my User System Command function defined in my templater settings is:

/opt/bin/icalBuddy -npn -ps "/ : /" -iep "datetime,title" -po "datetime,title" -df "%a"  eventsToday

It works fine when executed in my dailyNote template via <% tp.user.getEvents() %> I wanted to get the conditional logic to work and now am a happy camper! TY!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.