3

我找到了一种使用 AppleScript 和 Objective-C 的方法......

我所做的是这个 AppleScript:

tell application "System Events"
tell current location of network preferences
    try
        set VPN to service "MyVPN" -- your VPN name here
    on error
        return "NOT A CORRECT VPN FOUND ON THIS COMPUTER"
    end try
    if exists VPN then
        connect VPN
    end if

    delay 15

    if current configuration of VPN is connected then
        return "OK"
    else
        return "CONNECTION ERROR"
    end if
end tell

结束告诉

然后以这种方式在我的 Cocoa 项目中执行:

SString* path = [[NSBundle mainBundle] pathForResource:@"myScript" ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    NSAppleEventDescriptor *result = [appleScript executeAndReturnError:&errors];

    NSLog(@"result = %@",result);

我希望这很有用,但如果有人知道在 Objective-C 中完成这一切的方法,那就更好了!

再次感谢。

这些链接可能会有所帮助:

https://superuser.com/questions/358513/start-configured-vpn-from-command-line-osx

在 Obj-C 中获取 AppleScript 返回值

4

0 回答 0