- Joined
- Aug 3, 2011
- Messages
- 6
- Reaction score
- 0
- Points
- 1
The Problem
-----------------------------------------------
Basically I've created a class in java to restart my application by writing a applescript to file called aScriptRestart.scpt, running the applescript, and deleting the applescript upon restart or open if the file exists.
The problem I am having is that the code that runs the applescript is only working 50%, meaning that it quits the application but does not reopen (or reactivate) the application.
The Code:
-----------------------------------------------
Any Solutions??
-----------------------------------------------
However, if I simply run the script from Script Editor it works perfectly, but within the application it appears to close the script as soon as the application closes.
Is there anyway to do this? Keep in mind, I cannot use the com.apple.cocoa.foundation or similar imports due to this being developed as part of an open-source cross-platform application and is mostly being developed on Windows. I could but the .jar files for com.apple.cocoa.* do not exist, therefore unable to use them.
The goal is to make the open-source application more Mac friendly, as we use the osxappbundle-maven-plugin for our releases.
Any help would be greatly appreciated.
Thanks in advanced!;D
-----------------------------------------------
Basically I've created a class in java to restart my application by writing a applescript to file called aScriptRestart.scpt, running the applescript, and deleting the applescript upon restart or open if the file exists.
The problem I am having is that the code that runs the applescript is only working 50%, meaning that it quits the application but does not reopen (or reactivate) the application.
The Code:
-----------------------------------------------
Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author iwebdevelop
*/
public class RestartMacApp {
private String script = "tell application \"myApp\"\n"
+ "quit\n"
+ "delay 2 -- Wait for myApp to close\n"
+ "tell application \"myApp\" to activate\n"
+ "end tell";
private File file = new File("aScriptRestart.scpt");
private String pathToFile;
public void runScript() { //works 50%, closes but does not restart because script closes before it
ScriptToFile();
try {
pathToFile = file.getCanonicalPath();
} catch (IOException ex) {
Logger.getLogger(RestartMacApp.class.getName()).log(Level.SEVERE, null, ex);
}
String[] commandStrings = null;
Process results = null;
try {
commandStrings = new String[]{"/usr/bin/osascript", pathToFile};
results = Runtime.getRuntime().exec(commandStrings);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteScript() { //works
if (file.exists() && file.isFile()) {
file.delete();
} else {
System.out.println("File not found");
}
}
private void ScriptToFile() { //works
script.replaceAll("\n", System.getProperty("line.separator"));
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(script);
writer.close();
} catch (IOException ioe) {
System.err.format("IOException: %s%n", ioe);
}
}
}
Any Solutions??
-----------------------------------------------
However, if I simply run the script from Script Editor it works perfectly, but within the application it appears to close the script as soon as the application closes.
Is there anyway to do this? Keep in mind, I cannot use the com.apple.cocoa.foundation or similar imports due to this being developed as part of an open-source cross-platform application and is mostly being developed on Windows. I could but the .jar files for com.apple.cocoa.* do not exist, therefore unable to use them.
The goal is to make the open-source application more Mac friendly, as we use the osxappbundle-maven-plugin for our releases.
Any help would be greatly appreciated.
Thanks in advanced!;D