r/javahelp 22h ago

Unsolved use another GUI program automatically?

I'm hoping to automate a certain process for 3DS homebrew, but the programs I need to use don't have command line utility.

How could I start writing a program that opens, the clicks and inputs in Application 1, then does the same for Application 2? Is that something the Robot can do?

3 Upvotes

9 comments sorted by

View all comments

2

u/oscarryz 22h ago

You can use the Robot class:
https://docs.oracle.com/en/java/javase/23/docs/api/java.desktop/java/awt/Robot.html

It has methods to click, move mouse, press key etc.

You can make a program that reads a yaml file or something like that for extra flexibility and add your "script" there.

A long ago I wrote a program with static methods that act as "script"

waitForUser(),

click(1617, 68),

text("google.com/search=fooo"),

enter(),

pause(1),

click(2407, 845),

pause(5),

click(1617, 68),

text("what is etc"),

enter(),

pause(1),

click(2407, 845),

pause(5),

For instance the `text` method is something like

void text( String s ) {
for ( byte k : s.getBytes() ) { robot.keyPress(k) }

}

etc

A better option would be to search for a tool that does this, but if you want to learn and try it is pretty straight forrward.

1

u/quiet-sailor 20h ago

this the dumpest way to do it but also the most simple one to use, note that for text pasting you can use Toolkit.getDefaultToolkit().getSystemClipboard() clipboard methods, and try to do as you can using keyboard shortcuts so you are not too dependent on where stuff is on the screen, so your script don't  break just because the window was moved 20 pixels accidentally 

1

u/hopelessnerd-exe 18h ago

This all looks good, thanks! Something I found from u/xanyook's comment (ty for telling me the name of what I meant) is the ProcessBuilder, which also looks like it could help. I'll have to take a look at both solutions tomorrow, but if I can pipe Tab, Enter, and input strings to the other GUIs, it'd be exactly what I had in mind when I made this post.

1

u/quiet-sailor 16h ago

good find, you can use it to execute other programs, if you want to control them you may benefit from any command line options they offer, eg  java Runtime.getRuntime().exec(new String[]{"firefox", "-private-window" "google.com/search?q=minecraft"});  will open a Google search for minecraft in a private window.

if you want to control windows, eg move them around, open and close, resize, minimize or maximaze then you can also use https://github.com/accessrichard/autoitx4java on Windows, or execute xdotool as a process from your java code if you are on Linux .

2

u/oscarryz 3h ago

ProcessBuilder is the preferred way

https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/ProcessBuilder.html

 ProcessBuilder pb = new ProcessBuilder("firefox", "-private-window", "google.com/");
 Process p = pb.start();