0

This is my first ever post on here, and I'm coming here because I've hit a really big roadblock, and after days and days of trying to research possible answers, I've unfortunately come up short. I've also just started to learn how to program about 2 weeks ago, so please excuse me in advance if I say something incorrectly. :^)

So here's the crux: I've trying to open a particular file (a save file exported from a PS2 Save Builder) using a Java program (a .jar file) through a Python script I've created. At first, I couldn't the script to open the file, but I was finally (!) able to do so after creating an Apple .app file with a Unix executable where I could then open the .jar file as if it were an .app file. Now, I'm able to open the program using my script on its own, but I can't get it to open the save file using that program as well. Here is the code I'm currently working with:

import subprocess
import time

File = (absolute path to Save File)
Editor = (absolute path to Program)

a = subprocess.Popen([Editor, File], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)

time.sleep(10)

a.terminate()

This code opens the program for me, waits for 10 seconds, and then closes the program all perfectly. But for some reason, I can't seem to find a way to also open the file! I've tried subprocess.run(), playing with inputs, and it won't work. I've tested the code out with a spreadsheet and Excel, and a text document and Word, and those work just fine.

I assume I'm missing a piece where I have to tell the computer that the File is indeed a file I'd like to open with the program, but I haven't been able to find the style to write that.

Any help will be greatly appreciated! Thank you in advance. :D

(Working on a macOS Big Sur computer.)

  • Impossible to know without knowing how the Java app is meant to work. How does it treat the "save file" if you run it with `java -jar the.jar`? – g00se May 31 '22 at 16:53
  • Hey thank you for your comment :) upon opening it, you are prompted with an "Open" window where you have to go to where the file is located in your computer. You click on it, click on "Open", and the program boots up with the information from the file. The .jar program is basically a way of looking at the information from your save file in the game in a nice user-friendly way. – JupiterOne May 31 '22 at 17:18
  • User-friendly but programmer-unfriendly ;) If you want to automate that, then you're either going to have to rewrite it such that it accepts the file path as an argument, or write some kind of gui-robot to do the human clicking – g00se May 31 '22 at 17:26
  • Ahh I see :) By rewriting do you mean the Java program? Or the Python script I wrote above? – JupiterOne May 31 '22 at 17:27
  • Yes, the Java. That is of course if I've understood you correctly and you want to fully automate it – g00se May 31 '22 at 17:30
  • Yes, I'd like to automate the entire process (opening the program, and opening the file). Alright, I'll go and see how to do that! Thanks a lot. :D – JupiterOne May 31 '22 at 17:33
  • You'll need the source. You do have it..? – g00se May 31 '22 at 18:30
  • I have the .jar file downloaded, and I have all the decompiled -class files as well, if that's what you mean. – JupiterOne May 31 '22 at 22:53
  • Ah OK. That's not the same as having source but if you're lucky you might be able to recompile it. If you're not so lucky, some rewriting will have to be done – g00se May 31 '22 at 22:57
  • Sounds good. Thank you for your help. I'll update this post as soon as I've found a solution! :) – JupiterOne Jun 01 '22 at 00:05

2 Answers2

0

there could be multiple reasons why the file isn't opening as first thing

  1. try the comand that subprocess.Popen is calling on the terminal. if it works than python is the problem if not coud be a wrong path or permissions issues

if the problem is python you coud try somethin like this:

import subprocess
import time

File = (absolute path to Save File)
Editor = (absolute path to Program)

a = subprocess.Popen(f"./{Editor} {File}", shell=True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)

time.sleep(10)

a.terminate()
  • Hi thank you for your answer. :) The more likely scenario is that it's not a problem with Python, but rather having to configure the Java program itself. – JupiterOne May 31 '22 at 17:49
  • I undestand, so try excuting directly the jar file with 'java -jar file.jar file_to_load ' and see if it has the same behaviour. – Damiano Nardi May 31 '22 at 18:04
  • Unfortunately, I behaves the same way (opens the program, but not in conjunction with the file). I wrote above a little bit on how opening a file with the program works. – JupiterOne May 31 '22 at 18:11
  • I undestand, I don't know the program that are you using but maybe it for passing a file from command line needs a flag like -f 'java -jar file.jar -f file_to_load ' try to read the program documentation. It could also be the case that the program does not support passing the file from command line – Damiano Nardi May 31 '22 at 18:24
  • Is there any more information I can provide that can help you? – JupiterOne May 31 '22 at 22:56
0

I couldn't find a way to open the program with the file using subprocess.Popen(), so I figured I would have to find a way to open it another way. And I was able to do so using PyAutoGUI!

https://pyautogui.readthedocs.io/en/latest/

Highly recommend if you need to tediously click a lot of things.