0
votes

I have to create an application that will automatically open a powerpoint file, let it play through, and then close it. Not only do I need to figure out HOW to close it, but I also must detect when it closes or stops. First option: I know how long each powerpoint will play for, so I can hardcode when to close the file. I just need to know how to do that. There are no methods in the desktop class (that I could find) for closing. Second option: If someone knows a microsoft powerpoint api that lets me open powerpoints and use java to progress through the slideshow and get the state or something, that'd be great. I wouldn't have to go into each presentation and count the number of slides and the transition timer on each slide.

The opening, letting it play, and closing it is a small part of the app I need to create. But here is what I have so far with regards to THIS problem:

    File myfile = new File("PowerPoint.ppsx");
    try {
        Desktop.getDesktop().open(myfile);
    } catch (IOException ex) {
        Logger.getLogger(Sc.class.getName()).log(Level.SEVERE, null, ex);
    }
2
So I should create a new process and kill that process to close the external program? I think I get it but just one question: Runtime.getRuntime().exec(KILL + serviceName); I understand that this exec method essentialy...I guess prints the string parameter into window's cmd, right?user2316667

2 Answers

1
votes

Probably this is the solution how to close external program: http://www.java-forums.org/new-java/59691-close-another-program.html#post285956

If you want to detect when program has stopped running then you can start new thread with loop which from time to time will check if the program process is still running, using the same method as mentioned in link.

This is solution only for one (Windows) platform, Java is not the best choice for such tasks.

1
votes

Here a solution using JNA. First we get the handle, we search using the "class name" of the window. You can determine the class name for a specific program (in this case Powerpoint) with a special utility like Spy++ (included with Visual Studio). It's possible to make the search more precise using the class name and the window caption (but here I use only the class name) so if you have more than one presentation running ... you may not close the good one!.

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinDef.HWND;

// https://github.com/twall/jna#readme
//    you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar

public class KillMyPP {
    public static void main(String[] args) {
        HWND hwnd = User32.INSTANCE.FindWindow("screenClass", null);
        if (hwnd == null) {
           System.out.println("PPSX is not running");
        }
        else {
           User32.INSTANCE.PostMessage(hwnd, WinUser.WM_QUIT, null, null);  
        }
    }
}