Is there a way to programmatically connect to a remote server from Cocoa?

六眼飞鱼酱① 提交于 2020-01-04 06:50:21

问题


Is there an Coca/obj-C API call to mimic the "Connect to Server" action in Finder? It's possible with Automater, so it seems like Finder has a hook somewhere.


回答1:


Turns out there's an old Carbon function (can't find a Cocoa equivalent) called FSMountServerVolumeSync which does what I was looking for. You can supply and smb:// URL and login credentials.

File Manager Reference

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



回答2:


An easy way is to just run some applescript code. I'll show you 2 choices. This first one is the standard way to show that Finder window from applescript.

NSString* cmd = @"choose URL";

The resulting window is bare-bones though, so you can actually open the Finder's window with this command...

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

After choosing either of the "cmd" strings, you can execute that applescript code with this...

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



回答3:


This might not be the best way, but can't you just use mount?



来源:https://stackoverflow.com/questions/3374160/is-there-a-way-to-programmatically-connect-to-a-remote-server-from-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!