2

Good Morning everyone. Is it possible to open a file given the path, if it is closed, and if it is already open, putting the window in the foreground? This is the code to open the file:

package openapp;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.lang.IllegalArgumentException;
import java.util.Scanner;


public class OpenApp{

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);

        try {
            if(!Desktop.isDesktopSupported()){
                System.out.println("The desktop is not supported");
                return;
            }

            Desktop desktop = Desktop.getDesktop();

            System.out.println("Enter the path to the file to open");
            String path = input.nextLine();
            File file = new File(path);
            desktop.open(file);

            //il file non esiste
        } catch (IllegalArgumentException e) { 
            System.out.println("the file does not exist"); 
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
}

So the real question is: Is it possible to check if a file / app is already running and you can put a window in the foreground? (As an operating system I consider windows)

Niccolò Caselli
  • 783
  • 2
  • 10
  • 28
  • 1
    I think it depends on settings of application that is desired to open your file. – Mariusz Mączkowski Sep 02 '18 at 12:41
  • 1
    On a Mac I don't think you would need to bother, the OS will move it to the foreground wether it is already open or not. I guess it is the same on Windows. What is your question really about, what happens when you run your code? – Joakim Danielson Sep 02 '18 at 13:45
  • 1
    Sorry @Joakim Danielson, maybe I expressed myself badly. My program the only thing it does is open an application or file with the default application. If, however, I open a txt document and then, keeping the document running, I move to work on another program, if I launch my program saying to open the same document will open again the document and then there will be two documents equal in progress. What I want instead is that the open application above the document leaves room for the document called. So the first question to ask is: Can I see if an application / file is already in progress? – Niccolò Caselli Sep 02 '18 at 14:06
  • 1
    This seems to be a Windows thing then, when I run your code on my Mac and follows the steps you describe it brings the already open document to the front rather than opening a new copy of it. Maybe [checking if the file is locked](https://stackoverflow.com/a/1500521/9223839) could work but I doubt it. – Joakim Danielson Sep 02 '18 at 14:43
  • @Joakim Danielson you're right. In fact, I have noticed that it depends on the application. For example, for imicrosoft word or photoshop everything works correctly, while for Atom or notepad is reopened by the app. Excuse me for wasting your precious time. So I think the problem is unresolvable. – Niccolò Caselli Sep 02 '18 at 18:19
  • Similarlyy to you I have not come up with a solution. Did you find any more information on this task? The precisely same problem, however I can guarantee that my users run Windows 10. – Koenigsberg Jun 17 '20 at 09:24

0 Answers0