rb-appscript

2. The MacTypes::Alias class

The Alias class represents a persistent reference to a filesystem object. Aliases keep track of filesystem objects even if they're renamed or moved to another location on the same disk.

Methods

Alias -- a persistent reference to a filesystem object
    Constructors:

        Alias.path(path) -- make Alias object from POSIX path string

        Alias.hfs_path(path) -- make Alias object from HFS path string

        Alias.url(url) -- make Alias object from a local file:// URL string
    
        Alias.desc(desc) -- make Alias object from an AE::AEDesc
                            of TypeAlias

    Methods:

        ==

        hash
    
        inspect
    
        path -- returns POSIX path string to the object's current location
    
        hfs_path -- returns HFS path string to the object's current location

        url -- returns file:// URL string to the object's current location

        desc -- returns AE::AEDesc of TypeAlias

        to_s -- synonym for #path
    
        to_alias -- returns self
    
        to_file_url -- returns a MacTypes::FileURL object

Examples

require "appscript"

f = MacTypes::Alias.path('/Users/foo/some file')

puts f.to_s
# /Users/foo/some file

puts f.url
# file://localhost/Users/foo/some%20file

puts f.inspect
# MacTypes::Alias.path("/Users/foo/some file")

Appscript.app('TextEdit').open(f)
# opens document in TextEdit

MacTypes::Alias.path('/some/non/existent/location')
# File "/some/non/existent/location" not found.
#     (MacTypes::FileNotFoundError)

Notes

Comparing an Alias object against a FileURL object always returns false, even if both point to the same location.

Remember that aliases can change when the corresponding filesystem object is moved, so take care when using Alias objects in situations that involve comparing or hashing them (e.g. using aliases as Hash keys).

MacTypes::FileNotFoundError

FileNotFoundError is a subclass of RuntimeError. It is raised by Alias and FileURL objects when an operation that only works for existing filesystem objects/locations fails. For example:

require "appscript"

MacTypes::Alias.path('/some/non/existent/location')
# raises FileNotFoundError