【问题标题】:Is there a way to programmatically connect to a remote server from Cocoa?有没有办法以编程方式从 Cocoa 连接到远程服务器?
【发布时间】:2010-07-30 17:56:15
【问题描述】:

是否有 Coca/obj-C API 调用来模拟 Finder 中的“连接到服务器”操作?使用 Automater 是可能的,所以 Finder 似乎在某个地方有一个钩子。

【问题讨论】:

    标签: objective-c cocoa macos finder


    【解决方案1】:

    原来有一个名为 FSMountServerVolumeSync 的旧 Carbon 函数(找不到 Cocoa 等效函数)可以满足我的要求。您可以提供和smb:// URL 和登录凭据。

    File Manager Reference

    OSStatus FSMountServerVolumeSync (
      CFURLRef url,
      CFURLRef mountDir,
      CFStringRef user,
      CFStringRef password,
      FSVolumeRefNum *mountedVolumeRefNum,
      OptionBits flags
    );
    

    【讨论】:

      【解决方案2】:

      一个简单的方法是运行一些applescript 代码。我会告诉你2个选择。第一个是从 applescript 显示 Finder 窗口的标准方式。

      NSString* cmd = @"choose URL";
      

      生成的窗口是简单的,所以你可以用这个命令打开 Finder 的窗口...

      NSString* cmd = @"tell application \"Finder\" to activate\ndelay 0.2\ntell application \"System Events\" to keystroke \"k\" using command down";
      

      选择任一“cmd”字符串后,您可以使用此命令执行该 AppleScript 代码...

      NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:cmd];
      [theScript executeAndReturnError:nil];
      [theScript release];
      

      【讨论】:

      • 这可行,但您的脚本在“延迟 0.2”之后输入错误“应用程序”。
      【解决方案3】:

      这可能不是最好的方式,但你不能只使用mount吗?

      【讨论】:

      • 对于远程服务器(如smb://)?我很确定mount 仅适用于本地设备。
      • 至少在 Linux 上工作 - 你将它指定为 //server.name/share/path/to/folder
      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多