New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Restarting Application using Applescript from Java?


Post Reply New Thread Subscribe

 
Thread Tools
iwebdevelop

 
Member Since: Aug 03, 2011
Posts: 6
iwebdevelop is on a distinguished road

iwebdevelop is offline
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:
-----------------------------------------------
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!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,827
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
I'm going to guess (but I don't know for sure) that when the application quits, the script that you've executed also quits. My hunch, in other words, is that by closing the Java application, you also close any processes that are still executing that were launched from your app. Like I said, I don't know this for sure but it sounds as if this is the case.

Take a look here - it looks as if someone has written a method to do what you're looking to do.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
iwebdevelop

 
Member Since: Aug 03, 2011
Posts: 6
iwebdevelop is on a distinguished road

iwebdevelop is offline
Quote:
Originally Posted by vansmith View Post
I'm going to guess (but I don't know for sure) that when the application quits, the script that you've executed also quits. My hunch, in other words, is that by closing the Java application, you also close any processes that are still executing that were launched from your app. Like I said, I don't know this for sure but it sounds as if this is the case.

Take a look here - it looks as if someone has written a method to do what you're looking to do.
I agree, I believe that is exactly what it is doing... I'll take a look there and see.

If not, there's got to be a solution to this problem, b/c I've used many cross-platform applications with restart functions on mac.

Thanks again.
QUOTE Thanks
iwebdevelop

 
Member Since: Aug 03, 2011
Posts: 6
iwebdevelop is on a distinguished road

iwebdevelop is offline
Quote:
Originally Posted by vansmith View Post
I'm going to guess (but I don't know for sure) that when the application quits, the script that you've executed also quits. My hunch, in other words, is that by closing the Java application, you also close any processes that are still executing that were launched from your app. Like I said, I don't know this for sure but it sounds as if this is the case.

Take a look here - it looks as if someone has written a method to do what you're looking to do.
OK, that link with the code (as seen below) works, but only with .jar files.
Code:
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.myApp.Main;

/**
 * @author http://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application
 */
public class RestartMacApp {

    public void restartApplication() {
        final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
        final File currentJar;
        try {
            currentJar = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
            /* is it a jar file? */
            if (!currentJar.getName().endsWith(".jar")) {
                return;
            }

            /* Build command: java -jar application.jar */
            final ArrayList<String> command = new ArrayList<String>();
            command.add(javaBin);
            command.add("-jar");
            command.add(currentJar.getPath());

            final ProcessBuilder builder = new ProcessBuilder(command);
            builder.start();
        } catch (IOException ex) {
            Logger.getLogger(RestartMacApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(RestartMacApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        System.exit(0);
    }
Granted I could just release a .jar version for mac users, but I am releasing a .app version, which is built using Jar Bundler or the osxappbundle-maven-plugin. The issue is, as soon as you wrap the .jar application to a .app, the code above no longer works on .app version and the application just closes and doesn't restart.

I guess the question is, how to do this for an wrapped .app version (Mac OS X Native Executable Version)?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,827
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
I can't give you the Java code for this (I last touched Java in any real capacity about 6 years ago) but instead of having the app execute java with the -jar switch, simply have it execute something like the following:
Code:
open /path/to/application
It looks like instead of building up the command variable by adding bits related to executing a jar file, code in something so that you can execute the above (obviously replacing /path/to/application with the actual path to the app bundle).

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
iwebdevelop

 
Member Since: Aug 03, 2011
Posts: 6
iwebdevelop is on a distinguished road

iwebdevelop is offline
Quote:
Originally Posted by vansmith View Post
I can't give you the Java code for this (I last touched Java in any real capacity about 6 years ago) but instead of having the app execute java with the -jar switch, simply have it execute something like the following:
Code:
open /path/to/application
It looks like instead of building up the command variable by adding bits related to executing a jar file, code in something so that you can execute the above (obviously replacing /path/to/application with the actual path to the app bundle).
Given it a shot, probably use something like "/usr/bin/open", pathToFile similar to the first code posted. Will post the results when done.

Thanks again.
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,827
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
You shouldn't need to use the full path to open as /usr/bin/ will be in the user's path. Its not wrong to add it but it shouldn't be needed.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
iwebdevelop

 
Member Since: Aug 03, 2011
Posts: 6
iwebdevelop is on a distinguished road

iwebdevelop is offline
I would just like to say thanks to everyone that responded. I have found the solution.

The Code && Solution:
---------------------------------
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;

/**
 * This class Restarts Java Bundled Native Mac Applications with the .app extension
 * To use put the following where you want to call a restart, such as in a JMenuItem:
 * <pre>
 *      RestartMac restart = new RestartMac(0);
 * </pre>
 * Then put the following in your main() method to delete the files created:
 * <pre>
 *      RestartMac restart = new RestartMac(1);
 * </pre>
 * @author Paul Barton
 */
public class RestartMac {

    /* AppleScript */
    private String restartScript = "tell application \"yourApplication\" to quit\n"
            + "tell application \"System Events\"\n"
            + "repeat until not (exists process \"yourApplication\")\n"
            + "delay 0.2\n"
            + "end repeat\n"
            + "end tell\n"
            + "tell application \"yourApplication\" to activate";
    /* AppleScript FileName */
    private final File restartFile = new File("yourApplicationRestart.scpt");
    /* Created Application FileName
     * Is created when the AppleScript is Compiled
     */
    private final String restartApp = "yourApplicationRestart.app";
    /* String[] used to Compile AppleScript to Application */
    private final String[] osacompileString = new String[]{"/usr/bin/osacompile", "-o", restartApp, restartFile.toString()};
    /* String[] used to Open created Application */
    private final String[] openString = new String[]{"/usr/bin/open", restartApp};
    /*
     * String used to Delete created Application
     * VERY DANGEROUS IF THIS STRING IS CHANGED
     */
    private final String deleteString = "rm -rf " + restartApp;
    /* Compiles AppleScript to Application */
    private Process osacompile = null;
    /* Opens created Application */
    private Process open = null;
    /* Deletes created Application */
    private Process delete = null;
    /* Arguments for Constructor */
    private int argv;

    /**
     * Restarts YourApplication.app on Mac OS X
     * @param argc 
     */
    public RestartMac(int argc) {
        this.argv = argc;
        if (argc == 0) { //Use 0 when you call a restart, such as in FileMenuItem
            compileAppleScript();
            openApp();
        } else { //Use 1 in main, so on restart it removes the files created
            deleteScript();
            deleteApp();
        }
    }

    /*
     * Write AppleScript to a File
     */
    private void scriptToFile() {
        restartScript.replaceAll("\n", System.getProperty("line.separator"));
        try {
            BufferedWriter restartWriter = new BufferedWriter(new FileWriter(restartFile));
            restartWriter.write(restartScript);
            restartWriter.close();
        } catch (IOException ioe) {
            System.err.format("IOException: %s%n", ioe);
        }
    }

    /*
     * Compiles AppleScript to Application
     */
    private void compileAppleScript() {
        scriptToFile();
        try {
            osacompile = Runtime.getRuntime().exec(osacompileString);
            osacompile.waitFor(); //everything must wait until this process is completed
        } catch (InterruptedException ex) {
            Logger.getLogger(RestartMac.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RestartMac.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * Opens created Application
     */
    private void openApp() {
        try {
            open = Runtime.getRuntime().exec(openString);
        } catch (IOException ex) {
            Logger.getLogger(RestartMac.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * Deletes AppleScript if found
     */
    private void deleteScript() {
        if (restartFile.exists() && restartFile.isFile()) {
            restartFile.delete();
        }
    }

    /*
     * Deletes Created Application if found
     */
    private void deleteApp() {
        try {
            delete = Runtime.getRuntime().exec(deleteString);
        } catch (IOException ex) {
            Logger.getLogger(RestartMac.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
I'm sure this code needs to be refactored but it works. If any one can refactor this code, please post the refactored code here as a reply.
QUOTE Thanks

Post Reply New Thread Subscribe


« How to link an external dynamic library in Xcode 4? | Possible memory leak?... help »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Launching an application while executing an applescript? Commander_FarkU OS X - Development and Darwin 0 06-13-2010 09:32 PM
How to download older version of Java for mac aarthi Switcher Hangout 3 05-11-2010 05:32 PM
Firefox, Passwords & an AppleScript Brown Study OS X - Apps and Games 0 03-03-2007 12:22 AM
Java update 1.4.1 is out rman OS X - Operating System 1 04-30-2006 01:05 PM
Apple Releases Java 1.4.1 Graphite Apple Rumors and Reports 5 03-10-2003 07:52 PM

All times are GMT -4. The time now is 04:44 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?