2

I mounted the hard drive by using the GUI in Mac.

However, I want to mount the hard drive by using the terminal commands.

How can I execute a terminal command mount_smbfs from my Objective-C Cocoa application?

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath:@"/sbin/mount_smbfs"];
[task setArguments:[NSArray arrayWithObjects:@"//user:50000@smb://192.168.2.1/Share",@"Volumes/C$/upload", nil]];
[task launch];

Here is my edited with my code Could you please help me?

4

2 回答 2

3

您可以包装对 in 的调用以mount_smbfsNSTask您的 Obj-C 程序执行它:

NSTask* task = [NSTask new];
[task setLaunchPath:@"/sbin/mount_smbfs"];
[task setArguments:[NSArray arrayWithObjects:@"//myUser:myPassword@SERVER/share", @"mountPath", nil]];

在 setArguments 中,您提供一个包含至少 2 个元素的数组:共享路径和挂载点。

还要检查man mount_smbfs更多参数选项。

于 2012-12-26T14:08:11.260 回答
0

使用 AppleScript 更简单:

- (BOOL) mount {
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
                             @"tell application \"Finder\"\n"
                             "  mount volume \"smb://server.domain/SomeMountPoint\"\n"
                             "end tell"];
    if (!script) {
        NSLog(@"Error creating AppleScript object");
        return NO;
    }

    NSDictionary *errorMessage = nil;
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];
    return (BOOL)result;
}

有一些限制:

  1. 您必须NSAppleScript在主线程上使用。
  2. 当 Finder 尝试挂载卷时,您的应用程序不会响应任何事件。
  3. 如果挂载失败,您将无法控制 Finder 如何显示错误消息。
于 2012-12-27T00:34:47.590 回答