Run Applescript from java

Discussion in 'Mac Programming' started by Eraserhead, Apr 11, 2006.

  1. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    I am trying to run the following code, when the RunAppleScript method is launched it comes up with the following error:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/apple/cocoa/foundation/NSAppleScript
    at Copying.RunApplescript(Copying.java:141)
    at Copying.main(Copying.java:161)
    line 141 is 'NSAppleScript myScript =new NSAppleScript(script);'

    Code:
    import com.apple.cocoa.foundation.*;
    import com.apple.cocoa.application.*;
    public class test{
    	public static void RunApplescript(String script){
    		// This creates a new NSAppleScript
    		// object to execute the script
    		NSAppleScript myScript =new NSAppleScript(script);
    		
    		// This dictionary holds any errors that are
    		// encountered during script execution
    		NSMutableDictionary errors =
    			new NSMutableDictionary();
    		
    		// Execute the script!
    		NSAppleEventDescriptor results=myScript.execute(errors);
    	}
    }
    What's the problem?
     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    Is /System/Library/Java in the java classpath?
     
  3. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    I have tried adding

    I have typed this into Terminal
    'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.

    As I am using XCode I have also tried putting '-classpath /System/Libray/Java/' in the Build Tab of the inspector for Test.java

    I don't know if I'm doing it correctly...
     
  4. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #4
    I can't speak to XCode, but I'm not sure you need the trailing slash in your javac command classpath.

    guess I could try really quick.
     
  5. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
    Also make sure your class name and your file name are the SAME, case included. In your code example, your filename should be test.java not Test.java since the class name is test

    You also don't have a main method, make sure you include one in your class.
     
  6. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    My class has a main method, sorry I should have included it :(, the code is

    Code:
    import com.apple.cocoa.foundation.*;
    import com.apple.cocoa.application.*;
    
    public class Test {
    	public static void RunApplescript(String script){
    		// This creates a new NSAppleScript
    		// object to execute the script
    		NSAppleScript myScript =new NSAppleScript(script);
    		
    		// This dictionary holds any errors that are
    		// encountered during script execution
    		NSMutableDictionary errors =
    			new NSMutableDictionary();
    		
    		// Execute the script!
    		NSAppleEventDescriptor results=myScript.execute(errors);
    	}
    	public static void main (String args[]) {
    		String cmd="tell application \"Finder\" \n"+"make new folder in folder \"Matthew's HD\" with properties{name:\"testing\"} \n"+"end tell";
    		RunApplescript(cmd);
    	}
    }
    
    the class and the file are both called Test.java, and I use 'javac -classpath "/System/Library/Java:." Test.java' to compile it in the command line.
     
  7. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #7
    don't forget the -classpath on your java command too.

    this worked fine for me.
    Code:
    javac -classpath /System/Library/Java Test.java
    sudo  java -classpath "/System/Library/Java:." Test
    You'll need sudo for access to the Window Server to create the folder
     
  8. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    OK thanks, that appears to work fine!
     
  9. macrumors newbie

    Joined:
    May 14, 2006
    #9
    Dear All,

    I've experienced the same problem. Is there any other way to solve it?

    I have typed this into Terminal
    'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.

    Thank you
     
  10. thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #10
    You're probably having a problem as the script is set up only for me as it creates a folder on my disk which is called "Matthew's HD" ;) try changing that to something else and that should work.

    To be perfectly honest though running applescript commands from java in this way is very slow. It is much quicker, more reliable, and importantly doesn't require a password to save the script as an applescript file somewhere memorable (eg. the desktop, but for real use you'd want to save it somewhere else.)
    Code:
    set thePath to path to desktop
    tell application "Finder"
    	make new folder in folder thePath with properties {name:"testing"}
    end tell
    then invoke the command line from the java file and use the 'osascript' command from there, this is shown below:
    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class Test2 {
    		public static String UnixComm(String cmd){
    		try{
    			Process Results=Runtime.getRuntime().exec(new String[] {
    				"/bin/sh","-c",cmd});
    			//Results.waitFor(); //- Add this line if and only if you aren't collecting any data but need the command line stuff to have finished before continuing.
    			//If you don't need either then you can remove 'Process Results=' from the line above.
    			BufferedReader br=new  BufferedReader(new InputStreamReader(Results.getInputStream()));
    			String s=br.readLine();
    			br.close();
    			br=null;
    			Results=null;
    			return s;
    		}
    		catch(Exception e){
    			return "Error with UNIX Command";
    		}
    	}
    	public static void main (String args[]) {
    		String cmd="osascript ~/Desktop/Test.scpt";
    		UnixComm(cmd);
    	}
    }
    This returns any values from the unix command as a string so you can manipulate them if you wish.

    For more advanced use (eg you want to pass parameters to the script), feel free to ask about that. :)
     
  11. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #11
    read post #7 above...
     

Share This Page