Open/Close Disk Player (Vbs)

Asked

Viewed 162 times

0

I am using a code to "open/close the disk drawer" however it creates a file on the desktop and would like to arrange some other way to resolve this.

package controller;

import java.awt.Desktop;
import java.io.File;
import java.io.PrintWriter;

public class disc_1 {

    public static void main(String[] args) {
        try {
            //********Start VBScript code to open cd tray************  
            String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n"
                    + "Set colCDROMs = oWMP.cdromCollection" + "\n"
                    + "For d = 0 to colCDROMs.Count - 1" + "\n"
                    + "colCDROMs.Item(d).Eject" + "\n"
                    + "Next" + "\n"
                    + "set owmp = nothing" + "\n"
                    + "set colCDROMs = nothing" + "\n"
                    + "wscript.Quit(0)";
            //********End VBScript code to open cd tray************  

            File myCdTrayOpener = new File("OpenCdTray.vbs");
            PrintWriter pw = new PrintWriter(myCdTrayOpener);
            pw.print(a);
            pw.flush();
            pw.close();
            Desktop.getDesktop().open(myCdTrayOpener);
            myCdTrayOpener.deleteOnExit();

        } catch (Exception exception) {
            exception.printStackTrace();

        } finally {

            try {
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                //********Start VBScript code to open cd tray************  
                String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n"
                        + "Set colCDROMs = oWMP.cdromCollection" + "\n"
                        + "For d = 0 to colCDROMs.Count - 1" + "\n"
                        + "colCDROMs.Item(d).Eject" + "\n"
                        + "colCDROMs.Item(d).Eject" + "\n"
                        + "Next" + "\n"
                        + "set owmp = nothing" + "\n"
                        + "set colCDROMs = nothing" + "\n"
                        + "wscript.Quit(0)";
                //********End VBScript code to open cd tray************  

                File myCdTrayOpener = new File("OpenCdTray.vbs");
                PrintWriter pw = new PrintWriter(myCdTrayOpener);
                pw.print(a);
                pw.flush();
                pw.close();
                Desktop.getDesktop().open(myCdTrayOpener);
                myCdTrayOpener.deleteOnExit();

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

    }

}
  • Why don’t you create the file somewhere else (user temp directory maybe?) and then delete it afterwards?

1 answer

2


One of the builders of the class File allows sending as argument the directory where the file should be created. Having this in mind, just pass a String containing the directory path. For example:

// Estando em ambiente Windows, 'diretorio' terá algo como "C:\Users\FULANO"
String diretorio = System.getProperty("user.home");

File myCdTrayOpener = new File(diretorio, "OpenCdTray.vbs"); 

In that case, the idea of using user.home came because you were calling the method deleteOnExit. As the file will be deleted when the virtual machine is finished, I believe that you have no problem keeping the file temporarily in this location.

It is also possible to obtain the program data directory, if applicable.

  • Really! It takes away a doubt how do I put a code inside the VBS waiting time? Thank you!

  • 1

    I don’t know, I answered the question about Java.

  • Yes, he did. Now what I asked is another case. Thank you!

  • I think you’d better create a new question to this @kholyphoenix1, instead of dealing in the comments of a reply ^.^

  • Exactly. Thank you! : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.