Re: robust handling of return values
Re: robust handling of return values
- Subject: Re: robust handling of return values
- From: has <email@hidden>
- Date: Mon, 14 Nov 2005 13:45:32 +0000
patrick machielse wrote:
>I've noticed that some AppleScript commands don't return values of 1 predefined type, but can return an 'array' of different things. [...]
>Now on to my question: how do I robustly check the result value of the 'add' command?
>Awkwardly. Performing a coercion on the command's immediate result seems to work (touch-wood):
>
> tell application "iTunes" to set L to (add {}) as list
> L --> {}
>
>It looked like a winner, but it doesn't work. In script editor L is displayed as {}, but in fact it seems to be { current application } (which is how L is displayed in Automator...)
Bah. Scratch that then. Turns out that L is actually a list containing one item - the result of AS's standard scalar-to-single-item-list coercion. That item is a null descriptor, which AppleScript may or may not display depending on context. In some situations it's used by AS to indicate 'no value', in which case it won't display it for obvious reasons. In others, it's used by AS to indicate 'current application', in which case it will display the 'current application' constant.
>I never imagined you could coerce 'nothing' into list. That shouldn't be what coercing is supposed to be. It feels like creating matter out of nothing... Welcome to the AppleScript twilight zone!
To you, the user, it's nothing. To the AppleScript interpreter, it's an internal object representing 'nothing', but as a user you should never need to know that. The trouble starts when those internal-use-only values leak out into user-space, resulting in weird, undocumented behaviours as you see. (Leaky abstractions are not uncommon in AS.)
As I say, you don't have this mess in something like Python because Python functions _always_ return a value:
from appscript import *
print app('iTunes').add([])
# --> None
This is how AppleScript _should_ have done it, but I suppose to the original designers it seemed like a good idea at the time.
Anyway, here's a workaround that should do the job:
tell application "iTunes" to set L to add {}
try
set L to L as list
on error number -2753 -- catch 'variable L is not defined' errors
set L to {}
end try
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden