Use NSAppleScript creates and calls AppleScript app in the Objective C have different result with the same code?

StackOverflow https://stackoverflow.com/questions/22113825

Pergunta

Firstly, I have been created an AppleScript app with the below code(File name: 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

And use NSAppleScript class to calls it, the code as below:

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

It can be work fine, but it has a problem: if I want run my APP on another computer, I need change the UserName and MyPASSWORD to other person's in "UnloadTest.scpt" file, it's stupid! So I want to go another way: use "- (id)initWithSource:(NSString *)source" function to create an AppleScript from code, I can send the username&password from my APP interface:

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

But it work failed for me, looks like

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

does not work successful. I think it's the AppleScript did not get the privileges, but I don't know how to correct it.

Is there anyone can help me to understand this problem?

Foi útil?

Solução

I have found the reason what caused this problem, just missing "\n" after the row of the AppleScript code. Below code is success:

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

Thanks all!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top