Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > pass variable from C to Applescript?

pass variable from C to Applescript?
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2002
Status: Offline
Reply With Quote
Jan 18, 2003, 11:51 AM
 
Hi all,

I'm trying to pass a variable from some standard C code to an Applescript but i can't get it to work.
I have a variable called "buf" and want to display it in a finder alert.

this c code works:

Code:
printf("buffer contains: %s", buf);
Here is the snippet that doesn't work:

Code:
system("osascript -e 'tell application \"Finder\" display dialog \"buffer contains: \" & buf & \" \" end tell'");
The is the error i get: "execution error: The variable buf is not defined. (-2753)"

I appreciate any help

Cocoaholic
     
Mac Elite
Join Date: May 2001
Location: ~/
Status: Offline
Reply With Quote
Jan 18, 2003, 01:14 PM
 
Osascript is running the Applescript element of your program, not the program itself. To osascript buf does not exist anywhere. As of yet osascript does not allow you to pass arguments into a script which is what you would need to do in order to have this whole thing work properly.

If you want to use the AppKit framework from an app without using Interface Builder and all that jazz it is possible. Encapsulate your C code inside a cheapo Obj-C wrapper and you can display all kinds of cool stuff in a window.

Start here for building C-callable wrappers to Cocoa objects http://developer.apple.com/techpubs/...Functions.html.
     
Fresh-Faced Recruit
Join Date: Aug 2002
Status: Offline
Reply With Quote
Jan 18, 2003, 01:33 PM
 
Thanks, i'll see if i can figure out how to use this.

I'm not a real programmer, just trying to code

Are there any code samples out there using this technique?
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jan 19, 2003, 02:01 AM
 
Try this:
Code:
const char *command = "osascript -e 'tell application \"Finder\"\ndisplay dialog \"buffer contains: "; strcat(command, buf); strcat(command, "\"\nend tell'"); system(command);
The general idea here is to first build up the command in your program, which has access to buf, and then pass the whole thing to osascript.
[vash:~] banana% killall killall
Terminated
     
Fresh-Faced Recruit
Join Date: Aug 2002
Status: Offline
Reply With Quote
Jan 19, 2003, 10:03 AM
 
Thank you, i was looking for something like that! (i saw a similar perl example)

It didn't work for me though, had to change it to:

Code:
char command[256] = "osascript -e 'tell application \"Finder\"\ndisplay dialog \"buffer contains: "; strcat(command, buf); strcat(command, "\"\nend tell'"); system(command);
I have a very basic server (unix tool) which listens for incoming data, when it receives the right commands it has to open a cocoa application and pass the received data to that app.
I could do this by making this cocoa app Applescriptable but maybe this C-callable wrapper stuff would be better/easier?

Maybe there is a even better way?

Cocoaholic
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 02:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2