How to get Template or Templater reference to system device name

What I’m trying to do

I’d like to add a property to most (/all) of my templates that will resolve to the device name of the device the note was created from. I’m getting used to Templater and browsed through it’s documentation. But I didn’t spot (or didn’t understand enough) to spot the answer. Unless perhaps it could be done using tp.obsidian, but I didn’t really see enough to understand how to use that.

Any help appreciated. Thanks :slight_smile:

(For clarification, I want the note’s property value to be the same as the text in the Device Name Obsidian settings property for Obsidian Sync.)

I like automation and hacking – with limited skills, granted.

Two points:

  • I don’t know how you would manage to add this information to files created in the past. I think that’s out.
  • I am interested to know who will tell you on this forum how to use the Obsidian API related info that is needed for the Templater javascript script. Maybe it is somewhere in the Developer documentation?

In the meantime, what you could try: make different templates for mobile and PC (or whatever config folders corresponding to whatever platforms you have Obsidian installed on). So the Templater plugin data.json files must be different on your various installations. I don’t own Obsidian Sync so I’m not sure how you can do that. At a push, you can have different set of plugins in configurations for mobile, PC, etc. (You make copies of .obsidian and work out what plugin and other settings are good/needed for each platform and rename these dot folders and launch Obsidian with them.)

Then in the templates, you add a key/property defining:
created on: PC
created on: iOS,
whatever.

There are other ways, which may not involve different sets of plugin data.json files, but that’s how I would do it.

@guycalledxan like this:

<% app.internalPlugins.plugins.sync.instance.deviceName %>

You can find things like this by typing app (then press Enter) in the Developer console in Obsidian (Ctrl + Shift + I), and then browsing around.

As a side note, I’d never considered doing this, but it’s quite a cool idea. It also got me thinking that I could check if I’m on my home network and then set a property of home or not. Lots of interesting metadata you could add.

1 Like

Can you set the alarm clock a few mins earlier, mate?:slight_smile:

What time is down under?

1 Like

This is how I invoke and prompt my chat robot ver.10.0 – “living tissue over a metal endoskeleton” – free of charge. :slight_smile:

1 Like

The device name thing got me thinking about checking the local network name to see whether I’m at home when I create a note - like adding an automatic context.

I can’t see an easy way to find the network name, but Obsidian can find your IP address easily enough and you can identify your location that way.

Obviously an address like 192.168.0.x is not very unique; that would apply to lots of networks. But if you set your home wifi network range to be something more unique like 10.87.14.x then you can use Templater to tell if you’re at home or not:

<%*
const os = require('os')
const networkInterfaces = os.networkInterfaces()

if (networkInterfaces?.WiFi?.[0].address.startsWith('10.87.14.')) {
  // This note was written at home.
  // Now you could update the frontmatter or whatever.
}
%>

edit: Doesn’t seem to be supported on Android. You could execute a shell command, but I’m not sure if that works on mobile either.

1 Like

On your home network you could give it a static address, even on the 192.168.0.x network, so that it could be identified that way.

Thanks for the quick replies.

@AlanG 's solution worked perfectly.

There’s one small gotchya that I’ll note for anyone else reading this later:

The Device Name property in Sync needs to be filled in. It has grayed out default text that holds the correct system name and it states “if left blank it’ll use this default value”. However, the app.internalPlugins.plugins.sync.instance.deviceName property will return “” if the Device Name property is blank. You can see that in the developer console. If you fill in the Device Name property (probably with the same string as the system name) then this snippet of templater script works perfectly.

There is another cool thing you can do when you add the Device Name string though, kinda to make it worth while. If you write “[[device_name]]” in the field (with the “” either side), then when the note is created, you get a link to a note for device_name. I end up jumping between a few different terminals, so I’ve used this to help remind future-me which terminal is/was which.

I don’t know where else to get this data from if you don’t have Sync though, so that might be something still left to solve. The property does get that default value somewhere, and I poked around in the terminal a bit, but not extensively enough to work out where from / how.

Yeah, I resigned myself to ignoring older notes, or setting an “unknown” value for all older notes. Thankfully I’m only a few months into this vault.

I did set up different templates for the Daily note on Mobile and one PC but it got cumbersome across other note types and as I started using Obsidian on more PCs. It’s a nice solution for specific sets of note-location combos though as there’s lot that can be done programmatically with templater. I use <% tp.file.cursor() %> on templates for PC, for example, but don’t on templates for iOS as it doesn’t seem to work.

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