开发者

Can you get full paths from the pasteboard when drag and dropping?

开发者 https://www.devze.com 2023-01-29 04:24 出处:网络
I am supporting a drag-and-drop operation on an IKImageBrowserView. In my drag and drop destination delegate I accept NSFilenamesPboardType drops. The weird part is that the filepath strings I get bac

I am supporting a drag-and-drop operation on an IKImageBrowserView. In my drag and drop destination delegate I accept NSFilenamesPboardType drops. The weird part is that the filepath strings I get back all start with "/Users/..." instead of "file://localhost/..." as I get when using an NSOpenPanel. When I convert these to NSURLs for use by the IKImageBrowserView to show images, it gets confused because it only understands full paths starting with "file://localhost/...". I can hack the prefix in, as I've done in the snippet below开发者_开发知识库, but I was wondering if there was a programatic way to get full paths instead?

if ([[pasteboard types] containsObject:NSFilenamesPboardType])
{
    NSData* data = [pasteboard dataForType:NSFilenamesPboardType];
    if (data)
    {
        NSString* errorDescription;
        NSArray* filenames = [NSPropertyListSerialization
            propertyListFromData:data
            mutabilityOption:kCFPropertyListImmutable
            format:nil
            errorDescription:&errorDescription];

        for (id filename in filenames)
        {   
            NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"file://localhost%@", filename]];

            NSLog(@"Adding URL: %@", url);


To create a file URL from a full path, don't add file:// prefix yourself. Just use

    NSURL* url=[NSURL fileURLWithPath:pathString];

See the documentation.

From the point of view of the operating system,

   /Users/myname/file.txt

is the full path of a file in the system. In a higher level API which also allows various network access, the local file is distinguished by putting file:// protocol. Those pboard APIs predates the prevalent usage of URL in the API, that's why it returns paths as full paths, not as file URLs.

0

精彩评论

暂无评论...
验证码 换一张
取 消