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 > Mac OS X > How to pass parameter in Terminal to Applescript (osascript)??

How to pass parameter in Terminal to Applescript (osascript)??
Thread Tools
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 14, 2003, 12:35 PM
 
I want to have a simple Applescript (called from Terminal) that will go in to Address Book and print out a record for a person with name matching the passed-in parameter

ie. osascript myScript person_name

I have the basics of such a script working with the name hard-coded... am coming up empty so far on how to pass in this paramater.

As a side note... other than "return" is there a way to print to the Terminal (stdout). I've tried "say" and other things without luck.

LOL, or should I give up and hack the db with a perl script instead?

Thanks,
Mike
(Last edited by Zim; May 14, 2003 at 12:46 PM. )
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 14, 2003, 08:40 PM
 
Originally posted by Zim:
I want to have a simple Applescript (called from Terminal) that will go in to Address Book and print out a record for a person with name matching the passed-in parameter

ie. osascript myScript person_name

I have the basics of such a script working with the name hard-coded... am coming up empty so far on how to pass in this paramater.

As a side note... other than "return" is there a way to print to the Terminal (stdout). I've tried "say" and other things without luck.

LOL, or should I give up and hack the db with a perl script instead?

Thanks,
Mike
From the man page for osascript in OS 10.2.6:

Code:
BUGS osascript does not yet provide any way to pass arguments to the script.
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
May 14, 2003, 09:31 PM
 
Great idea for a command-line app.

There's a workaround:
call osascript -e 'set thename to '$1 -e 'load script "/path/to/script"' -e 'look_up_name(thename)'

i.e., dynamically compile a short script that sets a variable with the value you want to pass. Then load your script (hafta check; i don't remember that syntax exactly), and call a subroutine with the variable you created.

It's imperfect, but it works, and I've used it before. Loading the precompiled script is much faster than recompiling the whole applescript each time you run the script.


But really, you should be writing a command-line C or objective-C program to do this.

See this topic at cocoaddev and this macdevcenter article for lots of help doing that.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 14, 2003, 11:32 PM
 
Originally posted by Zim:
I want to have a simple Applescript (called from Terminal) that will go in to Address Book and print out a record for a person with name matching the passed-in parameter

ie. osascript myScript person_name
Any particular reason why you want this called from the Terminal?
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 08:38 AM
 
Originally posted by Brass:
Any particular reason why you want this called from the Terminal?
b/c I am very often remotely logged in over an SSH connection.

And also b/c I don't really need a reason

Mike
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 08:54 AM
 
Originally posted by Mithras:
Great idea for a command-line app.

There's a workaround:
call osascript -e 'set thename to '$1 -e 'load script "/path/to/script"' -e 'look_up_name(thename)'

i.e., dynamically compile a short script that sets a variable with the value you want to pass. Then load your script (hafta check; i don't remember that syntax exactly), and call a subroutine with the variable you created.

It's imperfect, but it works, and I've used it before. Loading the precompiled script is much faster than recompiling the whole applescript each time you run the script.


But really, you should be writing a command-line C or objective-C program to do this.

See this topic at cocoaddev and this macdevcenter article for lots of help doing that.
Thanks for the tips. I'm coming at this with a good background in perl/cgi scripting, but NO experience at Applescript, and having not written Apple-specific code since, um, 1992 (helped with Arashi a little)

But I think I understand where you are going with the suggestion and will give that a shot.

I considered this a reasonable hack to do my first Applescript. (a language is just a language after a certain point, and to some extent you get lazy about it b/c you know what you EXPECT it to be able to do, the frustration is in finding the correct syntax, not in figuring out the algorithm)

One unanswered question still... for a script called from a shell... how does one send the equivalent of prints back to stdout? So far I am only using "return", but that will allow me only one shot, and is not the equivalent of multiple "print 'reached point A'".. "print 'reached point B'" debug statements. I've been skimming the online docs, and the impression I get is that support for GUI output is much stronger (ie. I could be popping up dialog boxes if I intended to run this sitting in front of my Mac, but I intend to be running it remotely over a text-based ssh connection). I think I've tried "say" and "display" so far without luck.

Off to do some more research

Cheers,
Mike
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 10:27 AM
 
Ok, success at the "alpha" stage.

Code:
#!/bin/sh # filename: find_me # usage from Terminal prompt: find_me "Last First" osascript -e "set the_name to \"$1\"" -e 'on find_me (the_name) tell application "Address Book" set the_people to every person repeat with this_name in the_people if name of this_name contains the_name then set result to name of this_name & "\n" repeat with e_info in emails of this_name set result to result & value of e_info & " " end repeat set result to result & "\n" repeat with p_info in phones of this_name set result to result & value of p_info & " " end repeat return result end if end repeat end tell end find_me' -e 'find_me(the_name)'
(constructive suggestions welcomed)

Now to pretty up (and add more to) the output.

Alas the Apple on-line docs all seem to be from 1999

Mike
(Last edited by Zim; May 15, 2003 at 10:45 AM. )
     
   
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 08:16 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