0

In an electron app, I want to open a document from the user's computer, and place that file in a specific position on the user's screen. For example, open a locally saved txt file, and display it at the top right of the screen.

Hoping for a cross-platform solution, but I'd be ok starting with something for mac.

To open a file with electron, I can use the shell API, like this:

const { shell } = require('electron')

function openTheDoc(){
  shell.openPath(documentPath)
}

Once it's opened, is there a way to place the doc in a specific location on the screen?

One potential solution would be to wrap the doc in an electron window (then I could control the placement of the electron window). Is it possible to wrap docs in electron windows? I see this question from a few years back saying no, but I'm wondering if things have changed.

SeanRtS
  • 1,005
  • 1
  • 13
  • 31
  • Your _shell_ tag assumes that the question is about POSIX shell. However there is no shell-related code in your question. In addition, you didn't specify which OS you are using. – user1934428 May 19 '21 at 07:40
  • Electron is cross-platform. The question therefore pertains to cross-platform (mac, windows, linux), as most questions on stack overflow about electron do. The shell code is a potential basis for an answer, because it accomplishes the first part (opening the document) of what is required. Additionally, the shell referenced is the Electron API shell, not something specific to unix (if that is what POSIX shell is). – SeanRtS May 19 '21 at 11:56
  • Please look at the tag-description for _shell_ : Unless you explicitly specify a tag for the concrete shell (i.e. _bash_ or _zsh_), it refers to the [POSIX shell](https://steinbaugh.com/posts/posix.html), which would make sense if you want to run an external command using your API. You could see that an OS could be an issue, since the only answer you received so far, suggests a solution which only works on Windows. You could add to your question that you are searching for an OS-independent answer to your problem, if this is important for you. – user1934428 May 19 '21 at 12:20
  • Got you on the "shell" tag originally used. I have removed that tag. – SeanRtS May 19 '21 at 12:52

1 Answers1

0

You need to use an external program like nircmd.
To execute such shell program Node JS has the spawn function on child_process core module.
Electron shell API doesn't seem to have something similar.

From your electron app execute something like this once you opened your external document.

> nircmd win setsize ititle "myExternalDocumentWindow" 0 0 800 600.

This will set your window to the upperleft corner of the screen and make it of size 800 x 600.
Tweak the parameters to suit your needs.

Take a look at this question explaining spawn function.

cachique
  • 1,150
  • 1
  • 12
  • 16
  • Edit: Do you mean I would use the spawn process to run nircmd? Or do you mean I could use spawn to control window position directly? I need a solution that works for mac too. I am able to run spawn as a child_process in electron. But I don't know how to use spawn to control a window position. Do you have a sense of how to do that? – SeanRtS May 18 '21 at 21:17
  • Use spawn function, from nodejs, to run nircmd with appropriate parameters. – cachique May 18 '21 at 21:22
  • Thanks. I don't think that quite works, as I need something that can run on mac. – SeanRtS May 18 '21 at 21:25
  • Thanks for the reference to an explanation for the spawn function. I'll take a look. – SeanRtS May 18 '21 at 21:31
  • AppleScript seems to do the job. Execution of a script file shouldn't be different on a Mac. – cachique May 18 '21 at 21:32
  • Goal is something that works for both mac and windows. But if that doesn't work, starting with mac is ok. Is there a production-ready way to do applescript with electron? – SeanRtS May 18 '21 at 21:36