0

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?

Raymond Liao
  • 1,799
  • 3
  • 20
  • 32
  • *Never* hardcode users' login details; it is a huge security violation. Use a single `do shell script "set -e;cmd1;cmd2;cmd3;..." with administrator privileges` command, which will automatically prompt the user to authenticate once before proceeding. – foo Mar 03 '14 at 10:09
  • But that's not good for me, because my app need to calls the AppleScript every time. If I do that as you said, it will be prompt the user to authenticate every time, not once. – Raymond Liao Mar 05 '14 at 02:34
  • How often does your app need to run this script? Your original post doesn't provide any background information. – foo Mar 06 '14 at 14:13

1 Answers1

1

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!

Raymond Liao
  • 1,799
  • 3
  • 20
  • 32
  • Using string munging to pass parameters to a script is bad practice. Failing to sanitize your inputs before inserting them into the string is _very_ bad practice, and totally unsafe. (What happens if the text contains double quotes or backslashes?) [Here's the correct way to do it.](http://stackoverflow.com/questions/19759149/cocoa-run-applescript-that-already-contains-double-quotes/19774696#19774696) – foo Mar 06 '14 at 14:19