Get current selected card id in canvas

how can i get the currently selected card in the canvas?

I’ve tried using

this.registerEvent(this.app.workspace.on('file-open', (file: TFile ) => {
   //file given doesn't contain the id of CanvasTextData within the CanvasNodeData of the canvas
  //all i can get is the name
  file.name 
  //the file name will either be the canvas ur currently open in, 
  //or if it's a CanvasFileData type you will get back the name of the file
}));

I wanted to be able to get the id of the current selected node in the canvas

the canvas file looks like this

{
  "nodes": [
    {
      "id": "fd54f4c821dbc4cc",
      "type": "file",
      "file": "mydocument.md",
      "x": -100,
      "y": -840,
      "width": 260,
      "height": 241
    },
    {
      "id": "AXfxt6LMbEHsolL3",
      "type": "text",
      "text": "random text for testing",
      "x": 280,
      "y": -840,
      "width": 420,
      "height": 420
    },
    {
      "id": "62Db8TU532JSyt2N",
      "type": "text",
      "text": "",
      "x": 280,
      "y": -360,
      "width": 420,
      "height": 420
    }
  ],
  "edges": [],
  "metadata": {}
}

i feel like im looking for a needle in a haystack, surely there must be a method on either he workspace or workspaceleaf that will give me the id of the currently selected node in the canvas??

1 Like

I figured it out
I guess the api docs haven’t been updated with the canvas? i have no idea
but u can get the canvas selection like so

const canvasView = this.app.workspace.getActiveViewOfType(ItemView);
if (canvasView?.getViewType() !== 'canvas') return;
const canvas = (canvasView as any).canvas;
const selection: any = Array.from(canvas.selection);

which will give you an array of all the selected nodes, with their corresponding id’s.

1 Like

Just saw your post and it got me thinking. I think that to get the currently selected card ID in the canvas, you can access the active view and then get the selection. This will give you a list of all the selected nodes and their IDs.

If you need to verify documents or IDs for your projects, try www.idanalyzer.com. They have an API that helps verify the authenticity of documents and IDs, which can be really useful for many tasks.

Hi,

@Tekashi’s code snippet looks very useful to me. :slight_smile:
But, unfortunately, I don’t know where to run it from.

const canvasView = this.app.workspace.getActiveViewOfType(ItemView);
if (canvasView?.getViewType() !== 'canvas') return;
const canvas = (canvasView as any).canvas;
const selection: any = Array.from(canvas.selection);

Could anyone, please, tell me where to store this script and how to launch it from within Obsidian.
Sorry, I’m very new to Obsidian and just trying to dive into the world of plugins, commands and scripting.

Do I need to create a plugin to be able to run this code or is there a “lighter” way to run this code?

Info:
My final goal is to write a script that gets the images that are selected within a canvas (multiple files) and to select them in the navigation view (in order to be able yo bulk move them from there).

I created a feature request for this as well here:

But, maybe this could be done faster via scripting.

1 Like

Ok,

I discovered RunJs.
I think that this is the way to go. :slight_smile: