0

首先,我使用以下代码创建了一个 AppleScript 应用程序(文件名:UnloadTest.scpt):

-- Set user name and password
set UserName to "MyAccount"
set MyPASSWORD to "MyPassword"
-- unload FTDIUSBSerialDriver
do shell script "sudo kextunload /System/Library/Extensions/FTDIUSBSerialDriver.kext" user      name UserName password MyPASSWORD with administrator privileges
-- Copy driver to the used location
do shell script "sudo cp ./libftd2xx.1.0.2.dylib /usr/local/lib" user name UserName    password MyPASSWORD with administrator privileges
do shell script "sudo cp ./libd2xx_table.dylib /usr/local/lib" user name UserName password MyPASSWORD with administrator privileges
do shell script "cd /usr/local/lib" user name UserName password MyPASSWORD with administrator privileges
do shell script "sudo ln -sf libftd2xx.1.0.2.dylib libftd2xx.dylib" user name UserName password MyPASSWORD with administrator privileges

并使用 NSAppleScript 类调用它,代码如下:

NSString * strScript = [[NSBundle mainBundle] pathForResource:@"UnloadTest" ofType:@"scpt"];
NSAppleScript * appScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strScript] error:nil];
[appScript executeAndReturnError:NULL];
[appScript release];

它可以正常工作,但它有一个问题:如果我想在另一台计算机上运行我的应用程序,我需要将“UnloadTest.scpt”文件中的用户名和我的密码更改为其他人的,这很愚蠢!所以我想换一种方式:使用“- (id)initWithSource:(NSString *)source”函数从代码创建一个AppleScript,我可以从我的APP界面发送用户名和密码:

    NSString        *strScriptUnload    = [NSString stringWithFormat:@"\
                                           set UserName to \"%@\"\
                                           set MyPASSWORD to \"%@\"\
                                           do shell script \"sudo kextunload /System/Library/Extensions/FTDIUSBSerialDriver.kext\" user name UserName password MyPASSWORD with administrator privileges\
                                           do shell script \"sudo cp ./libftd2xx.1.0.2.dylib /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\
                                           do shell script \"sudo cp ./libd2xx_table.dylib /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\
                                           do shell script \"cd /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\
                                           do shell script \"sudo ln -sf libftd2xx.1.0.2.dylib libftd2xx.dylib\" user name UserName password MyPASSWORD with administrator privileges",[m_textUserName stringValue], [m_textPassword stringValue]];
    NSAppleScript   *appUnloadScript    = [[NSAppleScript alloc] initWithSource:strScriptUnload];
    returnDescriptor = [appUnloadScript executeAndReturnError:nil];
    [appUnloadScript release];

但它对我来说失败了,看起来像

sudo ln -sf libftd2xx.1.0.2.dylib libftd2xx.dylib

不成功。我认为这是AppleScript没有获得特权,但我不知道如何纠正它。

有没有人可以帮助我理解这个问题?

4

1 回答 1

1

我已经找到了导致这个问题的原因,只是在 AppleScript 代码行之后缺少“\n”。下面的代码是成功的:

NSString        *strScriptUnload    = [NSString stringWithFormat:@"\
                                       set UserName to \"%@\"\n\
                                       set MyPASSWORD to \"%@\"\n\
                                       do shell script \"sudo kextunload /System/Library/Extensions/FTDIUSBSerialDriver.kext\" user name UserName password MyPASSWORD with administrator privileges\n\
                                       do shell script \"sudo cp ./libftd2xx.1.0.2.dylib /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\n\
                                       do shell script \"sudo cp ./libd2xx_table.dylib /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\n\
                                       do shell script \"cd /usr/local/lib\" user name UserName password MyPASSWORD with administrator privileges\n\
                                       do shell script \"sudo ln -sf libftd2xx.1.0.2.dylib libftd2xx.dylib\" user name UserName password MyPASSWORD with administrator privileges\n",[m_textUserName stringValue], [m_textPassword stringValue]];
NSAppleScript   *appUnloadScript    = [[NSAppleScript alloc] initWithSource:strScriptUnload];
returnDescriptor = [appUnloadScript executeAndReturnError:nil];
[appUnloadScript release];

谢谢大家!

于 2014-03-05T02:42:42.177 回答