 |
 |
Applescript variable scope woes...
|
 |
|
 |
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
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
|
|
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
|
|
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.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|