从地址簿注释字段中逐行获取Apple脚本

时间:2010-12-25 02:48:41

标签: applescript automator

我的地址簿注释栏中有两行

Test 1
Test 2

我想将每一行作为单独的值或从notes字段中获取最后一行。

我试过这样做:

tell application "Address Book"
 set AppleScript's text item delimiters to "space"
 get the note of person in group "Test Group"
end tell

但结果是

{"Test 1
Test 2"}

我正在寻找:

{"Test1","Test2"}

我做错了什么?

1 个答案:

答案 0 :(得分:5)

您的代码存在一些问题。首先,你从来没有真正要求注释的文本项目:-)你只需要获取原始字符串。第二个是set AppleScript's text item delimiters to "space"将文本项分隔符设置为文字字符串space。因此,例如,运行

set AppleScript's text item delimiters to "space"
return text items of "THISspaceISspaceAspaceSTRING"

返回

{"THIS", "IS", "A", "STRING"}

其次,即使你有" "而不是"space",这会将你的字符串拆分为空格,不是换行符。例如,运行

set AppleScript's text item delimiters to " "
return text items of "This is a string
which is on two lines."

返回

{"This", "is", "a", "string
which", "is", "on", "two", "lines."}

如您所见,"string\nwhich"是一个列表项。

要做你想做的事,你可以使用paragraphs of STRING;例如,运行

return paragraphs of "This is a string
which is on two lines."

返回所需的

{"This is a string", "which is on two lines."}

现在,我并不完全清楚完全你想做什么。如果你想为特定的人获得这个,你可以写

tell application "Address Book"
    set n to the note of the first person whose name is "Antal S-Z"
    return paragraphs of n
end tell

你必须将它分成两个语句,因为我认为paragraphs of ...是一个命令,而第一行的所有内容都是属性访问。 (说实话,我经常通过反复试验来发现这些事情。)

另一方面,如果您希望为组中的每个人获取此列表,则稍微困难一些。一个大问题是没有笔记的人会得到missing value的笔记,这不是字符串。如果你想忽略这些人,那么以下循环将起作用

tell application "Address Book"
    set ns to {}
    repeat with p in ¬
        (every person in group "Test Group" whose note is not missing value)
        set ns to ns & {paragraphs of (note of p as string)}
    end repeat
    return ns
end tell

every person ...位正如它所说的那样,得到了相关人员;然后我们提取他们的音符段落(在提醒AppleScript后note of p确实是一个字符串)。在此之后,ns将包含{{"Test 1", "Test 2"}, {"Test 3", "Test 4"}}

之类的内容