OS X:如何在 macOS 上获取桌面目录的路径?

问题描述 投票:0回答:3

如何在 macOS 上以字符串形式获取桌面目录的文件路径。 我需要用纯 C 或一些 C 级框架来完成它。

c macos
3个回答
0
投票

如果您坚持只使用 C(为什么?),那么您唯一的选择就是使用已弃用的 API:

#include <limits.h>
#include <CoreServices/CoreServices.h>

...

FSRef fsref;
UInt8 path[PATH_MAX];

if (FSFindFolder(kUserDomain, kDesktopFolderType, kDontCreateFolder, &fsref) == noErr &&
    FSRefMakePath(&fsref, path, sizeof(path)) == noErr)
{
    // Make use of path
}

如果你需要一个

CFURL
而不是一个路径,你可以使用
CFURLCreateFromFSRef()
而不是
FSRefMakePath()

实际上,在研究这个的时候,我发现了一个我不知道的API。显然,你可以使用它,它显然来自 Cocoa 但只使用 C 类型:

#include <limits.h>
#include <NSSystemDirectories.h>

char path[PATH_MAX];
NSSearchPathEnumerationState state = NSStartSearchPathEnumeration(NSDesktopDirectory, NSUserDomainMask);
while (state = NSGetNextSearchPathEnumeration(state, path))
{
    // Handle path
}

API的形式是它可能返回多个结果(循环的每次迭代一个),但你应该只得到一个用于这里的特定用途。在这种情况下,您可以将

while
更改为和
if

请注意,使用此 API,用户域中目录的返回路径可能使用“~”而不是用户主目录的绝对路径。你必须自己解决这个问题。


0
投票

这是一个简短的功能,它适用于更多基于 Unix 的系统,而不仅仅是 macOS,并返回 current 用户的桌面文件夹:

#include <limits.h>
#include <stdlib.h>
/**
 * Returns the path to the current user's desktop.
 */
char *path2desktop(void) {
  static char real_public_path[PATH_MAX + 1] = {0};
  if (real_public_path[0])
    return real_public_path;
  strcpy(real_public_path, getenv("HOME"));
  memcpy(real_public_path + strlen(real_public_path), "/Desktop", 8);
  return real_public_path;
}

路径只会计算一次。

如果多次调用该函数,将返回旧结果(不是线程安全的,除非第一次调用受到保护)。


0
投票

我以这样的方式结束了 Objective-C 的使用:

//
//  main.m
//  search_path_for_dir
//
//  Created by Michal Ziobro on 23/09/2016.
//  Copyright © 2016 Michal Ziobro. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    if(argc != 3)
        return 1;

    @autoreleasepool {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(atoi(argv[1]), atoi(argv[2]), YES);
        NSString *path = [paths objectAtIndex:0];
        [path writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil];
    }
    return 0;
}

比起命令行,我可以用那种方式执行这个程序:

./search_path_for_dir 12 1 

12 - NSDesktopDirectory

1 - NSUserDomainMask

我在 C 中使用脚本从命令行执行这个程序并检索它的输出。

这是调用此迷你 Cocoa 应用程序的 C 示例:

CFStringRef FSGetFilePath(int directory, int domainMask) {

    CFStringRef scheme = CFSTR("file:///");
    CFStringRef absolutePath = FSGetAbsolutePath(directory, domainMask);

    CFMutableStringRef filePath = CFStringCreateMutable(NULL, 0);
    if (filePath) {

        CFStringAppend(filePath, scheme);
        CFStringAppend(filePath, absolutePath);

    }

    CFRelease(scheme);
    CFRelease(absolutePath);

    return filePath;
}

CFStringRef FSGetAbsolutePath(int directory, int domainMask) {

    char path_cmd[BUF_SIZE];
    sprintf(path_cmd, "./tools/search_path_for_dir %d %d", directory, domainMask);
    char *path = exec_cmd(path_cmd);

    return CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingUTF8);
}
© www.soinside.com 2019 - 2024. All rights reserved.