• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Coercing document list to alias list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coercing document list to alias list


  • Subject: Re: Coercing document list to alias list
  • From: has <email@hidden>
  • Date: Sun, 10 Dec 2006 22:12:57 +0000

Shane Stanley wrote:

"as alias list" isn't really a coercion

This is roughly correct. To explain a bit more:


While the 'as' keyword is AppleScript's built-in coercion operator, it can *also* be used an optional parameter within application commands that support it, e.g. the Finder's 'get' command.


For example, if you write:

    tell application "Finder" to get home as alias
    --> alias "d1:Users:has:"

the 'as alias' parameter is passed to Finder's 'get' handler, which then returns an alias value as its result.


Where things can get a bit problematic is that AppleScript subsequently performs its own coercion on the returned value if the desired type is one that AppleScript recognises. This makes a certain amount of sense, in that not all application commands support an 'as' parameter, so if a return value isn't already of the desired type then there's still an opportunity for AS to [try to] coerce the value that is given to the right type itself. Now, this "coerce it again just to be sure" behaviour doesn't cause a problem with the above example, since the application result is already an alias value. However, if the result is a list of alias values, then the subsequent to-alias coercion will fail for anything but a single-item list, e.g.:


-- Finder returns a single-item list containing an alias value; AS then coerces this to an alias value:
tell application "Finder" to get (every item of home whose name is "Library") as alias
--> alias "d1:Users:has:Library:"


-- Finder returns a list containing multiple alias values; AS then tries to coerce this to an alias value:
tell application "Finder" to get (every item of home) as alias
--Error: Can't make {alias "HD:Users:foo:Desktop", alias "HD:Users:foo:Documents", ...} into type alias.


This is what happens when your language doesn't have enough parentheses in it.<g> e.g. You don't get this dual-purpose ambiguity in Python or Ruby:

#!/usr/local/bin/ruby
require "appscript"
p Appscript.app('Finder').home.items.get(:result_type=>:alias) # works just fine
# [MacTypes::Alias.path("/Users/foo/Desktop/"), MacTypes::Alias.path("/Users/foo/Documents/"), ...]



So anyway... in an attempt to workaround AppleScript's ham- handedness, the Finder dictionary defines its own custom 'alias list' type for use as the 'get' command's 'as' parameter. This somehow tricks AppleScript's normal coercion behaviour into letting the returned list of aliases through untouched. Except where it's a single-item list, which AS first coerces to a single item (since it always knows how to do that coercion), then tries to coerce to the 'alias list' type (which fails as 'alias list' isn't a type that AS knows itself). AS is just too damn clever for its own good sometimes. So users end up having to apply a further workaround to deal with that case, e.g.:


    tell application "Finder"
        tell some_reference
            if (count) = 1 then
                set the_list to {it as alias}
            else
                set the_list to it as alias list
            end if
        end tell
    end tell


HTH

has
--
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan


_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email@hidden
  • Prev by Date: Find the intersection of two groups in Address Book.
  • Next by Date: Is there a more efficient way to do this?
  • Previous by thread: Re: Coercing document list to alias list
  • Next by thread: Re: Coercing document list to alias list
  • Index(es):
    • Date
    • Thread