2014-01-15 35 views
0

我想從CSV文件讀取並將AppleScript中的變量與CSV文件的內容進行比較。下面的文件和變量的例子。在AppleScript中解析CSV文件並選擇列

CSV文件:

GroupName, DriveLetter, Path 
Test1 , E:   , \\networkshare\test1 
Test4 , G:   , \\networkshare\test4 

的AppleScript變量包含:

Test1 
Test2 
Test3 

如果變量和CSV文件(如Test1的),我需要選擇具有相關聯的路徑之間的匹配Test1行並將其保存到一個新變量,然後使用該變量映射一個卷。

這裏是腳本的一個片段:

set current_user to system attribute "USER" as text 
    set command to do shell script ("dscl '/Active Directory/DOMAIN/All Domains' -read /Users/" & current_user & " dsAttrTypeNative:memberOf") 
    set group to trim_line(command, "dsAttrTypeNative:memberOf:", 0) 
    mount volume "smb://servername/netlogon/" 
    set DrivesFile to (read "/volumes/netlogon/drives.csv" using delimiter {",", lf}) 
    repeat with aline in DrivesFile 
    if aline is in group then "this is where I need to select the other CSV column and then mount a volume" 
    end repeat 
+0

'trim_line'是一個自定義函數嗎?它返回什麼? 「Test1」等?我是否正確理解變量「group」包含在您的問題中標記爲「CSV文件」的數據?如果仔細觀察trim_line,您應該能夠理解行和分隔符如何工作。如果沒有人比我快,我會嘗試發表一個例子。 – Mark

回答

0

你必須在代碼是沒有必要的,我告訴你一個解決你的問題有些東西,所以我會忽略的東西現在。

此示例假定您將csv文件作爲文本讀入applescript,並且在讀取文件時不使用分隔符。我使用「csvtext」來模擬。我也假設你的「組」變量是一個項目列表。

因此,當你的if語句爲真時,我將如何確定「其他」csv列。您需要在代碼中指明的地方發出您的安裝語句。祝你好運。

set csvText to "GroupName, DriveLetter, Path 
Test1 , E:   , \\\\networkshare\\test1 
Test4 , G:   , \\\\networkshare\\test4" 
set group to {"Test1", "Test2", "Test3"} 

set theParagraphs to paragraphs of csvText 

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "} 
repeat with i from 2 to count of theParagraphs 
    set thisParagraph to item i of theParagraphs 

    set lineList to text items of thisParagraph 
    if word 1 of (item 1 of lineList) is in group then 
     set thePath to item 3 of lineList 
     -- mount the volume at thePath 
    end if 
end repeat 
set AppleScript's text item delimiters to tids 
+0

regulus6633 - 感謝您的回覆以及上面給出的很好的例子。我不得不稍微調整一下重複函數代碼,以獲得我需要的值,但現在它正在工作。 – user3196166