Get content of current page? Or current user selection?

Is it possible to either get the entire content of the current page, or the content the user has selected?

Preferably with space script, but I’d be okay with it in a plug as well.

Basically I’m wanting to write some functions/commands that can take either a whole note or part of it and send it to an api (or external shell command), have it do some stuff and return new content to replace the selected portion.

I saw space.readPage exists to read the content of a page, but I’m not sure how a command can figure out the name of the page currently open.

I gave up too quickly and didn’t realize there was a whole list of syscalls in editor.ts :sweat_smile:

For future reference, this works to get the currently selected text:

async function getSelectedText() {
  const selectedRange = await syscall("editor.getSelection");
  console.log("selectedRange:", selectedRange);
  const pageText = await syscall("editor.getText");
  return pageText.slice(selectedRange.from, selectedRange.to);
}

These are not documented on Space Script - should they be, or are they not intended to be used in space script?

Right. These syscalls don’t appear on that page because they’re not available on the server, just on the client. Therefore they can only be used for some use cases like commands (which run in the client), but not custom functions used in templates (rendered on the server). The reason they don’t appear on that list is that that list is rendered by a template, which queries for all available syscalls… on the server.

Whats the best way to tell if it’s server only or client only? I’m still confused about what is considered safe to use or not

All syscalls under editor.* are client only, the rest work on both I think.

So is this right?

  • custom functions - run on server by default, unless in offline mode
  • custom commands - always run on client
  • templates - run on server by default, unless in offline mode
  • editor.* syscalls - always run on client
  • other syscalls - server or client, depending on the context of where they were triggered from (function vs command)?

Yes, that’s how you can think about it. The only remark here is that editor.* are simply not available on the server, so they cannot be run there. You’ll get an error.

1 Like