2014-03-01 128 views
0

首先,我已經創建了下面的代碼一個AppleScript應用程序(文件名:UnloadTest.scpt):使用NSAppleScript創建並調用Objective C中的AppleScript應用程序具有不同的結果和相同的代碼?

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

它可以正常工作,但它有一個問題:如果我想在另一臺計算機上運行我的APP,我需要將UserName和MyPASSWORD更改爲其他人的「UnloadTest.scpt」文件,這很愚蠢!所以我想走另一條路:用「 - (ID)initWithSource:(的NSString *)源代碼」功能,從代碼中創建一個AppleScript,我可以從我的應用程序接口發送用戶名&密碼:

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沒有獲得特權,但我不知道如何糾正它。

有沒有人可以幫助我理解這個問題?

+0

*從不*硬編碼用戶的登錄信息;這是一個巨大的安全違規。使用一個'do shell腳本'set -e; cmd1; cmd2; cmd3; ...「帶有管理員權限的命令,這將在繼續之前自動提示用戶驗證一次。 – foo

+0

但這對我並不好,因爲我的應用程序需要每次調用AppleScript。如果我按照你的說法這樣做,它會提示用戶每次驗證一次,而不是一次驗證。 – Raniys

+0

您的應用程序需要多長時間運行一次該腳本?您的原始文章不提供任何背景信息。 – foo

回答

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

謝謝大家!

+0

使用字符串消除將參數傳遞給腳本是不好的做法。在將其輸入到字符串之前未能對輸入進行消毒處理是一種不好的做法,並且完全不安全。 (如果文本包含雙引號或反斜槓會發生什麼?)[這是正確的方法來做到這一點。](http://stackoverflow.com/questions/19759149/cocoa-run-applescript-that-already-contains-double-報價/ 19774696#19774696) – foo

相關問題