PDA

View Full Version : Run Applescript from java




Eraserhead
Apr 11, 2006, 06:19 AM
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);'

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?



jeremy.king
Apr 11, 2006, 09:44 AM
What's the problem?

Is /System/Library/Java in the java classpath?

Eraserhead
Apr 11, 2006, 10:04 AM
Is /System/Library/Java in the java classpath?
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...

jeremy.king
Apr 11, 2006, 10:29 AM
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...

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.

jeremy.king
Apr 11, 2006, 10:32 AM
I have typed this into Terminal
'javac -classpath "/System/Library/Java/:." Test.java' and the same error occurs.


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.

Eraserhead
Apr 11, 2006, 10:51 AM
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.
My class has a main method, sorry I should have included it :(, the code is

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.

jeremy.king
Apr 11, 2006, 11:16 AM
don't forget the -classpath on your java command too.

this worked fine for me.

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

Eraserhead
Apr 11, 2006, 01:11 PM
OK thanks, that appears to work fine!

ajoso
May 14, 2006, 10:43 PM
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

Eraserhead
May 15, 2006, 04:31 AM
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.)
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:

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. :)

jeremy.king
May 15, 2006, 09:45 AM
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

read post #7 above...