简体   繁体   中英

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

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.

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];

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
);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM