更改使用 NetFSMountURLSync 安装的共享的 Finder 显示名称

Change Finder display name of share mounted using NetFSMountURLSync

我正在尝试编写命令行应用程序以连接到另一个 Mac 上的共享驱动器。下面的代码连接共享,但我似乎无法找到一种方法来操作出现在 Finder 左侧面板的“共享”部分中的主机名。假设远程计算机的主机名是 computer,我希望它显示 computer 而不是像使用此代码那样显示 computer.local。要求使用 MDNS 主机名进行连接。我试过设置 kNetFSServerDisplayNameKeykNetFSDisplayNameKey 键,但它们似乎不会影响 Finder 中显示的内容。

代码:

NSString *target = @"smb://computer.local/share";
NSString *mountPath = @"/Volumes/share";
CFStringRef servername = (CFStringRef) @"computer";

CFMutableDictionaryRef mount_options = CFDictionaryCreateMutable( NULL, 0, NULL, NULL);
CFDictionarySetValue(mount_options, kNetFSSoftMountKey, kCFBooleanTrue);
CFDictionarySetValue(mount_options, kNetFSMountAtMountDirKey, kCFBooleanTrue);
CFDictionarySetValue(mount_options, kNetFSServerDisplayNameKey, servername);
CFDictionarySetValue(mount_options, kNetFSDisplayNameKey, servername);
CFArrayRef mountpoints = NULL;

CFStringRef login = (__bridge CFStringRef) @"login";
CFStringRef pass = (__bridge CFStringRef) @"pass";

OSStatus err = NetFSMountURLSync(
                                 (__bridge CFURLRef) [NSURL URLWithString: target], // URL to mount, e.g. nfs://server/path
                                 (__bridge CFURLRef) [NSURL URLWithString: mountPath],  // Path for the mountpoint
                                 login,                                             // Auth user name (overrides URL)
                                 pass,                                             // Auth password (overrides URL)
                                 NULL,                                             // Options for session open (see below)
                                 mount_options,                                    // Options for mounting (see below)
                                 &mountpoints);                                    // Array of mountpoints

我通过使用 Finder 安装后检查卷上的信息来解决这个问题。当您通过 Finder 安装共享时,卷的“获取信息”中的服务器字段为 smb://computer._smb._tcp.local/share。在上面的代码中使用此 URL 而不是 smb://computer.local/share 会导致 computer 显示在 finder 的左侧共享面板中,而不是 computer.local.