在AppleScript中解析CSV文件并选择列

时间:2014-01-15 00:17:06

标签: 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

1 个答案:

答案 0 :(得分:0)

你在代码中有一些东西对我来说没有必要向你展示你问题的解决方案,所以我暂时忽略这些东西。

此示例假定您将csv文件作为文本读取为applescript,并且在读取文件时不使用分隔符。我正在使用" csvtext"模拟那个。我也假设你的"组"变量是一个项目列表。

所以,鉴于这些事情,我将如何确定"其他" if语句为true时的csv列。您需要在代码中指明的位置发出mount语句。祝你好运。

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