We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › call appleScript
Page Index Toggle Pages: 1
call appleScript (Read 2685 times)
call appleScript
Dec 18th, 2007, 3:34pm
 
hei there!

i'd like to call an apple script via processing - is this possible?

f.e.

tell application "iTunes"
  playpause
end tell

...


thanks! mias
Re: call appleScript
Reply #1 - Dec 18th, 2007, 4:21pm
 
this will do the job, press p to play, press s to pause.

Code:

import com.apple.cocoa.foundation.*;

void setup() {
size(400,400);
}

void keyPressed() {
switch(key) {
case('p'):iTunes("play");break;
case('s'):iTunes("pause");break;
}
}

void draw() {}


void iTunes(String theCommand) {
String script = "tell application \"iTunes\""+"\n" + theCommand + "\n" + "end tell";
executeScript(script);
}


void executeScript(String script) {
NSAppleScript myScript = new NSAppleScript(script);
NSMutableDictionary errors = new NSMutableDictionary();
myScript.execute(errors);
}

Re: call appleScript
Reply #2 - Dec 19th, 2007, 11:03am
 
thanks sojamo! it works great!

what i'm working on now should be rather simple, but somehow i can't figure out, where's the problem (i posted this question in the "other libaries" folder - but i don't think it belongs there...)

i'd like to work with the apple sms libary. what i'm trying to do is to recognise a "shock" - therefor i'd i'm trying to compare to pieces of data in an array:

import sms.*;

void setup() {
 size(200,200);
}

void draw() {
 int i=0;
 int[] vals = Unimotion.getSMSArray();
 int[] listener = new int[5];
 while(i<5) {
   listener[i] = vals[0];
   i  = i + 1;
}
 println( listener[0] + " " + listener[1] + " " + listener[2]);
}

but it won't work. is that, because the while structure operates within frameRate?

i'm a little confused - i think it's an total dork-question!

thanks! mias

Re: call appleScript
Reply #3 - May 30th, 2008, 10:41am
 
Thanks sojamo for the example.

Looking from the example:
 String script = "tell application \"Finder\" to say \"Hello world how are you.\"";

I've been trying to do the following, but am having difficulty: OpenURL in Firefox.

Here is the applescript version:

/////////////
tell application "Firefox"
  OpenURL "http://www.processing.org/"
end tell
/////////////

And here is the code that I tried.
 String script = "tell application \"Firefox\" OpenURL \"http://www.processing.org\"";

Any help would be appreciated.
Re: call appleScript
Reply #4 - May 30th, 2008, 12:03pm
 
Based on the previous example, this seems to work (at least in Leopard):
Code:

import com.apple.cocoa.foundation.*;

void setup() {
size(400,400);
}

void keyPressed() {
switch(key) {
case('p'):firefox("OpenURL \"http://www.processing.org/\"");break;
case('g'):firefox("OpenURL \"http://www.google.com/\"");break;
}
}

void draw() {}


void firefox(String theCommand) {
String script = "tell application \"Firefox\""+"\n" + theCommand + "\n" + "end tell";
println(script);
executeScript(script);
}


void executeScript(String script) {
NSAppleScript myScript = new NSAppleScript(script);
NSMutableDictionary errors = new NSMutableDictionary();
myScript.execute(errors);
}

Not sure what went wrong with your version...
Re: call appleScript
Reply #5 - May 30th, 2008, 12:28pm
 
Hi Mias,

Thank you for your quick reply.

It's a simple error. I forgot to use backward slashes as path separators:

ie:

case('g'):firefox("OpenURL "http://www.google.com/"");break;

versus

case('g'):firefox("OpenURL \"http://www.google.com/\"");break;

Thanks!
Re: call appleScript
Reply #6 - Sep 16th, 2008, 1:21pm
 
Have a look at this you can add Speech Recognition to your processing apps using applescript, it is all working but Im having problems with the return result, syntax problem. Can anyone give me a hand? here is the processing code:

---PROCESSING CODE----


import com.apple.cocoa.foundation.*;

void setup() {
 size(400,400);
}

void keyPressed() {
 switch(key) {
   case('p'):iTunes("play");break;
   case(' '):iTunes("pause");break;
   case('n'):iTunes("next track");break;
   case('s'):speech();break;
 }
}

void draw() {}


void iTunes(String theCommand) {
 String script = "tell application \"iTunes\""+"\n" + theCommand + "\n" + "end tell";
 executeScript(script);
}

 
void executeScript(String script) {
 NSAppleScript myScript = new NSAppleScript(script);
 NSMutableDictionary errors = new NSMutableDictionary();
 NSAppleEventDescriptor result = myScript.execute(errors);  
 
 println(NSAppleEventDescriptor.stringValue(result));
}

void speech() {
 
 String script = "tell application \"SpeechRecognitionServer\"\n set result to (listen continuously for {\"Call me\", \"Call anyone except me\"} with prompt {\"Who would you like to call?\"} with identifier \"Calling\" with section title \"Phone\")\nreturn result\nend tell";
 executeScript(script);
}


----APPLESCRIPT CODE -----
Here is the applescript you can drop into applescript editor to get what is should return:

tell application "SpeechRecognitionServer"

set result to (listen continuously for {"Call me", "Call anyone except me"} with prompt {"Who would you like to call?"} with identifier "Calling" with section title "Phone")

return result
end tell


footnote: speech recognition example taken from mac os X hints entry:

http://forums.macosxhints.com/showthread.php?t=27918
Re: call appleScript
Reply #7 - Feb 2nd, 2009, 2:28pm
 
Is it just me, or doesn't
Code:
import com.apple.cocoa.foundation.*;  


work anymore since Processing 1.0? It used to work fine, but now I get an error saying I "might miss some library".
Even old scripts using this code don't seem to work anymore (even in Processing 0156).

update:
Just found this topic with a workaround:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=LibraryProblems;action=display;num=1225564736
Page Index Toggle Pages: 1