• 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: dealing with alias files, finding URL to target file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dealing with alias files, finding URL to target file


  • Subject: Re: dealing with alias files, finding URL to target file
  • From: Quincey Morris <email@hidden>
  • Date: Fri, 15 Apr 2011 12:01:14 -0700

On Apr 15, 2011, at 11:10, Sean McBride wrote:

> _this_ is a PITA:
>
> - (NSURL*)URLByResolvingSymlinksAndAliases
> {
> 	NSURL* resultURL = [self URLByResolvingSymlinksInPath];
>
> 	NSError* error = nil;
> 	NSNumber* isAliasFile = nil;
> 	BOOL success = [resultURL getResourceValue:&isAliasFile
> 										forKey:NSURLIsAliasFileKey
> 										 error:&error];
> 	if (success && [isAliasFile boolValue])
> 	{
> 		NSData* bookmarkData = [NSURL bookmarkDataWithContentsOfURL:resultURL
> 															  error:&error];
> 		if (bookmarkData)
> 		{
> 			BOOL isStale = NO;
> 			NSURLBookmarkResolutionOptions options =
> (NSURLBookmarkResolutionWithoutUI |
> 													  NSURLBookmarkResolutionWithoutMounting);
>
> 			NSURL* resolvedURL = [NSURL URLByResolvingBookmarkData:bookmarkData
> 														   options:options
> 													 relativeToURL:nil
> 											   bookmarkDataIsStale:&isStale
> 															 error:&error];
> 			if (resolvedURL)
> 			{
> 				resultURL = resolvedURL;
> 			}
> 		}
> 	}
>
> 	return resultURL;
> }

I'll ask the dumb question: what happens if you invoke bookmarkDataWithContentsOfURL on a non-alias file? If it correctly detects an error, your PITA code could be reduced to:

> - (NSURL*)URLByResolvingSymlinksAndAliases
> {
> 	NSURL* resultURL = [self URLByResolvingSymlinksInPath];
>
> 	NSData* bookmarkData = [NSURL bookmarkDataWithContentsOfURL:resultURL error: NULL];
> 	if (bookmarkData)
> 	{
> 		NSURL* resolvedURL = [NSURL URLByResolvingBookmarkData:bookmarkData
> 									options:(NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithoutMounting)
> 									relativeToURL:nil
> 									bookmarkDataIsStale:NULL
> 									error:NULL];
> 		if (resolvedURL)
> 			resultURL = resolvedURL;
>
> 	}
>
> 	return resultURL;
> }


which is a *lot* less unpleasant.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: dealing with alias files, finding URL to target file
      • From: Sean McBride <email@hidden>
References: 
 >dealing with alias files, finding URL to target file (From: Sean McBride <email@hidden>)
 >Re: dealing with alias files, finding URL to target file (From: Ken Thomases <email@hidden>)
 >Re: dealing with alias files, finding URL to target file (From: Sean McBride <email@hidden>)
 >Re: dealing with alias files, finding URL to target file (From: Ken Thomases <email@hidden>)
 >Re: dealing with alias files, finding URL to target file (From: Sean McBride <email@hidden>)

  • Prev by Date: Re: renaming directories and moving files into them (NSFileManager)
  • Next by Date: Re: dealing with alias files, finding URL to target file
  • Previous by thread: Re: dealing with alias files, finding URL to target file
  • Next by thread: Re: dealing with alias files, finding URL to target file
  • Index(es):
    • Date
    • Thread