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 > Applescript variable scope woes...

Applescript variable scope woes...
Thread Tools
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 01:53 PM
 
I've been working on my first Applescript, you can see the code at

http://forums.macnn.com/90/mac-os-x/160622/how-pass-parameter-terminal-applescript-osascript/

Now, wanted to modify this script to return all matches. Since I have not figured out a way to send multiple separate results to stdout, I wanted to change the first "set result" to a "set result to result &" to have it cat all results into one long string. I would then move the "return result" line outside of the repeat loop, outputting the results in one fell swoop.

But I'm having variable scope issues. Seems that "result" is not defined globally enough so that is lives outside the repeat (possibly not even outside the if)

I've been rummaging thru the Apple on-line docs to no avail. Have tried many places for global and parameter declarations with no success.

thanks for any suggestions,
Mike
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
May 15, 2003, 03:14 PM
 
The word result is a reserved word in AppleScript, and i suspect that is causing you some of your grief as you appear to be trying to use it like a normal variable. For example, one might use result as follows:
Code:
count words in paragraph 3 set numWords to result
The problem with this kind of usage is that the value of result usually doesn't persist for very long, and needs to be used (e.g. captured) almost immedately (i.e. on the next line) as it will likely change or disappear by the next AppleScript statement (although there are some exceptions).

In general, avoid using result whenever you can. For example, something like set numWords to (count of words in paragraph 3) is a better construct (and indeed you appear to be using it). On rare occassions, the use of result is unavoidable, however (even when one would think it should be).

Regarding your code, i think if you replace all occurances of result with something like theResult, it'll work.
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 04:38 PM
 
I was kinda wondering if I was using a reserved word ....but changing didn't seem to help. Revised code.....
Code:
#!/bin/sh # filename: find_me # usage from Terminal prompt: find_me name_search_string osascript -e "set the_name to \"$1\"" -e 'global theResult on find_me (the_name) tell application "Address Book" global theResult set the_people to every person repeat with this_name in the_people if name of this_name contains the_name then try set theResult to theResult & name of this_name & "\n" end try repeat with e_info in emails of this_name set theResult to theResult & value of e_info & " " end repeat set theResult to theResult & "\n" repeat with p_info in phones of this_name set theResult to theResult & value of p_info & " " end repeat end if end repeat return theResult end tell end find_me' -e 'find_me(the_name)'
I get
>find_me Mike
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
execution error: The variable theResult is not defined. (-2753)
(this has never been more approriate)

Granted there are 2 global declarations in there now, but a) neither seems to help, and b) wow, there is no complaint about re-definition either.

Cheers,
Mike
     
Fresh-Faced Recruit
Join Date: Sep 2000
Status: Offline
Reply With Quote
May 15, 2003, 05:20 PM
 
Zim,

I don't think you need the global declarations; get rid of them.

What you DO need, though, is to initialize theResult as a string with an assigned value of null in a statement like...

set theResult to ""

...before you enter the loop and start appending values to it. You can't append a value to something that has no value.
(Last edited by edgewise; May 15, 2003 at 05:35 PM. )
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
May 15, 2003, 06:10 PM
 
You know, you might want to debug this scrpt using Script Editor 2.0 before dropping it into a osascript call. You don't have to do it this way, but it ought to be easier.

edgewise is absolutely correct about the variable initialization, and at least the AppleScript global declaration (not sure about the first one). The Script Editor would have told you the initialization problem.
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 15, 2003, 09:33 PM
 
Originally posted by edgewise:
Zim,

I don't think you need the global declarations; get rid of them.

What you DO need, though, is to initialize theResult as a string with an assigned value of null in a statement like...

set theResult to ""

...before you enter the loop and start appending values to it. You can't append a value to something that has no value.
(and there was light...)

indeed this was the key... odd tho b/c I _could_ get away with it when I was using result as long as I returned from within the inner "if".

One unanswered question... is there a way for me to be printing each match/line as a go, vs. building up the cat'd string?

Many thanks!
Mike
     
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
May 15, 2003, 10:44 PM
 
One unanswered question... is there a way for me to be printing each match/line as a go, vs. building up the cat'd string?
I think you're looking for some kind of callback mechanism to the shell and, as far as i know, there is no way to do this. My impression is that communication between shells and AppleScripts are crudely implemented.
     
Fresh-Faced Recruit
Join Date: Sep 2000
Status: Offline
Reply With Quote
May 15, 2003, 11:37 PM
 
Originally posted by Zim:
(and there was light...)

indeed this was the key... odd tho b/c I _could_ get away with it when I was using result as long as I returned from within the inner "if".
Glad we could help.

I can't explain why it worked with result before.

One unanswered question... is there a way for me to be printing each match/line as a go, vs. building up the cat'd string?
I agree with Rainy Day on this question. You can't do it from inside a looping AppleScript handler, because you can't pass the data out to the shell without exiting the loop and the handler.

(BTW, far as I can tell, the try and end try statements in your script aren't doing anything constructive, so you can remove them if you'd like.)

edgewise
(Last edited by edgewise; May 16, 2003 at 12:02 AM. )
     
Zim  (op)
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
May 16, 2003, 06:28 AM
 
I agree with Rainy Day on this question. You can't do it from inside a looping AppleScript handler, because you can't pass the data out to the shell without exiting the loop and the handler.

(BTW, far as I can tell, the try and end try statements in your script aren't doing anything constructive, so you can remove them if you'd like.)

edgewise [/B]
Doh, missed that answer the 1st time, thanks.

Yep, I had removed the "try" statements along with the globals.

nice and simple now, just as it should've been from the beginning

Again, many thanks!
Mike
     
   
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:17 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