Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
NSURL & path additions
Objective-C Shell

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
KSFileUtilities.xcodeproj
TestKSFileUtilities
.gitignore
KSFileWrapperExtensions.h
KSFileWrapperExtensions.m
KSPathUtilities.h
KSPathUtilities.m
KSURLFormatter.h
KSURLFormatter.m
KSURLUtilities.h
KSURLUtilities.m
KSUniformType.h
KSUniformType.m
KSWebLocation.h
KSWebLocation.m
KSWebLocationPasteboardUtilities.h
KSWebLocationPasteboardUtilities.m
KSWorkspaceUtilities.h
KSWorkspaceUtilities.m
README.mdown

README.mdown

Relative Strings

NSURL is fully equipped to resolve relative strings against another URL, but sadly there's no API to go in the opposite direction. KSURLUtilities to the rescue!

- (NSString *)ks_stringRelativeToURL:(NSURL *)URL;
- (NSURL *)ks_URLRelativeToURL:(NSURL *)URL;

How's this work? Well, everyone loves examples right?

ReceiverrelativeToURL:Return Value
http://karelia.com/foo/bar/http://karelia.com/foo/bar/
http://karelia.com/foo/example.pnghttp://karelia.com/foo/example.png
http://karelia.com/foo/bar/http://example.com/http://karelia.com/foo/bar/
http://karelia.com/http://karelia.com/foo/bar/../../
http://karelia.com/baz/index.xmlhttp://karelia.com/foo/bar/../../baz/index.xml

Relative Paths

KSPathUtilities has an equivalent for operating on path strings: -ks_pathRelativeToDirectory:

Query Parameters

It's common for URLs to split their query string up into a dictionary-like series of parameters. e.g.

http://en.wikipedia.org/w/index.php?title=Main_page&action=raw

KSURLUtilities has an easy solution for getting that into a more Cocoa-friendly form:

- (NSDictionary *)ks_queryParameters;

There are also APIs for creating/deriving new URLs from a dictionary:

- (NSURL *)ks_URLWithQueryParameters:(NSDictionary *)parameters;

+ (NSURL *)ks_URLWithScheme:(NSString *)scheme
                       host:(NSString *)host
                       path:(NSString *)path
            queryParameters:(NSDictionary *)parameters;

URL Paths

Mac OS X 10.6 gave us a bunch of new URL methods like -URLByDeletingLastPathComponent. If you wanted access to this behaviour before, it necessitated much tedious mucking about with NSString path methods, or using CFURL functions. KSURLUtilities gives simple Cocoa APIs for all these tasks on 10.5 and earlier:

- (NSString *)ks_lastPathComponent;
- (NSString *)ks_pathExtension;
- (NSURL *)ks_URLByAppendingPathExtension:(NSString *)pathExtension;
- (NSURL *)ks_URLByDeletingLastPathComponent;
- (NSURL *)ks_URLByDeletingPathExtension;
- (BOOL)ks_hasDirectoryPath;
- (NSURL *)ks_URLByAppendingPathComponent:(NSString *)pathComponent isDirectory:(BOOL)isDirectory;

Subpaths

To test if one URL/path contains another:

- [NSURL ks_isSubpathOfURL:]
- [NSString ks_isSubpathOfPath:]

Host

Nothing fancy here, just a few additions to the built-in -[NSURL host] method.

- (NSURL *)ks_hostURL;

Strips a URL down to nothing but its scheme and host. e.g. http://karelia.com/foo/bar/ becomes http://karelia.com/

- (NSArray *)ks_domains;

Splits the host up into its domains. e.g. http://www.karelia.com gives (www, karelia, com)

Incrementing a Path

On occasion there is a need to try to find the best available filename/path for a task. e.g. Choosing a filename that won't conflict with ay existing file. To simplify this, -[NSString ks_stringByIncrementingPath:] is offered. Given the path example.png, it will cycle through like so:

example.png
example-2.png
example-3.png
exampke-4.png
…

More information on the implementation and use of this method is on Mike's blog

KSURLFormatter

A basic NSFormatter subclass for handling URLs.

  • Non-complete URLs like example and example.com generate full http://example.com/ URL
  • Host URLs have a slash appended to them. e.g. http://example.com is displayed/interpreted as http://example.com/
  • Convenience +URLFromString method that takes care of unescaped characters

KSWebLocation

A simple class that represents a URL, optionally with a title attached. Features:

  • Handles Web Location files (e.g. those created by dragging a URL from Safari)
  • NSCoding
  • NSCopying

You can pull in the WebKit.framework-dependent KSWebLocationPasteboardAdditions too, to (unsurprisingly) gain support for reading and writing Web Locations from/to the pasteboard.

Development

There are two branches in this repository: the master branch and the tests branch. The master branch just has the classes which can easily be used as a submodule in other repositories.

The tests branch has a project with a single target - a unit test bundle.

If the tests branch is currently checked out, changes from master can be brought over by doing a simple merge.

If the master branch is currently checked out, changes from tests can be brought over by cherry-picking the commits that change the main class files. Of course, with this method it is important to keep the changes to the main class files in their own commits that do not include changes from the test project or unit testing classes. This isn't usually a problem as long as commits are kept small and frequent.

Sometimes, there may be artefacts left over when switching branches. These are files that are ignored by git (in the .gitignore file) so they are not created or destroyed when switching branches. If they are distracting they can be cleaned up using the standard command:

git clean -dxf

This deletes all files that are not tracked by git.

Something went wrong with that request. Please try again.