尝试获取列表并使用Applescript将其作为CSV写入文件

时间:2012-04-07 19:47:19

标签: list applescript export-to-csv

找到更新解决方案如果感兴趣,请参阅帖子的底部 看起来很简单,并且对于列表的大部分内容都可以使用,但在某些时候它会很糟糕。列表的数据来自InDesign中的资产,但这是我的最终结果,可以替换为测试AS中定义的列表。

tell application "Adobe InDesign CS5.5"
 set myagents to (name of properties of every asset of library "Agents.indl" as list)
end tell

set agentlist to ""
repeat with currentagent in myagents
 set agentlist to agentlist & currentagent & ","
end repeat

set x to count agentlist
set agentlist to characters 1 thru (x - 1) of agentlist as string
my write_agents(agentlist)


on write_agents(this_list)
set the agent_log to ("/Volumes/Agents/agents.txt" as POSIX file)
try
    set eof of agent_log to 0
    open for access file the agent_log with write permission
    write (this_list) to file the agent_log starting at eof

    --set read_data to (read the_file as list) as text
    close access file the agent_log
on error
    try
        close access file the agent_log
    end try
end try
end write_agents

而不是Indesign告诉我们可以使用的顶部进行测试,结果是相同的

set myagents to {"Lois Chase Johnson", "Tammy Newton", "Team Schmidt", "Tanya Kerr", "Terry Peterson", "Brenda Clark", "Deb Kahle", "Carol Dana", "Sheryl Burley & Cathy Reed", "Linda Ulin", "Dianna Erickson", "Dave Sharman", "Lori Tracey & Chuck Murphy", "Carolyn & Robert Dodds", "Jean Ryker", "Bryan Diehl", "Lois Scheck", "Nancy Rathke", "Patrick French", "Laura Halady", "Jennifer Felton", "Clarice Arakawa", "Holly Coburn", "Quint Boe", "Michaelle Barnard", "Paul Beck", "Kelly Johnson", "Doc Reiss", "Helga Filler", "Harriet Reyenga", "Thelma Durham", "Brooke Nelson", "Dick Pilling", "David Ramey", "Rebecca Jackson", "Tim Riley", "Pili Meyer", "Marc Thomsen", "Kathy Brown", "Jean Irvine", "Kathy Love", "Kimi Robertson", "Ed Sumpter", "Jo Cummins", "Kim Bower", "Mike Fuller", "Cathy Reed", "Joyce Underwood", "Daphne Eshom", "Kathy Brown", "Dave Ramey", "Mark DeRousie", "Tom Blore", "Paul Beck", "Kelly Johnson", "Alan Burwell", "Sheryl Burley", "Vivian Landvik", "Mark HcHugh", "Jim Newton", "Chuck Turner"}

文本文件中有什么内容?

    Lois Chase Johnson,Tammy Newton,Team Schmidt,Tanya Kerr,Terry Peterson,Brenda Clark,Deb Kahle,Carol Dana,Sheryl Burley & Cathy Reed,Linda Ulin,Dianna Erickson,Dave Sharman,Lori Tracey & Chuck Murphy,Carolyn & Robert Dodds,Jean Ryker,Bryan Diehl,Lois Scheck,Nancy Rathke,Patrick French,Laura Halady,Jennifer Felton,Clarice Arakawa,Holly Coburn,Quint Boe,Michaelle Barnard,Paul Beck,Kelly Johnson,Doc Reiss,Helga Filler,Harriet Reyenga,Thelma Durham,Brooke Nelson,Dick Pilling,David Ramey,Rebecca Jackson,Tim Riley,Pili Meyer,Marc Thomsen,Kathy Brown,Jean Irvine,Kathy Love,Kimi Robertson,Ed Sumpter,Jo Cummins,Kim Bower,Mike Fuller,Cathy Reed,Joyce Underwood,Daphne Eshom,Kathy Brown,Dave Ramey,Mark DeRousie,Tom Blore,Paul Beck,Kelly Johnson,Alan Burwell,Sheryl Burley,Vivian Landvik,Mark HcHugh,Jim Newton,Chuck Turner �C�o�b�u�r�nutxt����Q�u�i�n�t� �B�o�eutxt���"�M�i�c�h�a�e�l�l�e� �B�a�r�n�a�r�dutxt����P�a�u�l� �B�e�c�kutxt����K�e�l�l�y� �J�o�h�n�s�o�nutxt����D�o�c� �R�e�i�s�sutxt����H�e�l�g�a� �F�i�l�l�e�rutxt����H�a�r�r�i�e�t� �R�e�y�e�n�g�autxt����T�h�e�l�m�a� �D�u�r�h�a�mutxt����B�r�o�o�k�e� �N�e�l�s�o�nutxt����D�i�c�k� �P�i�l�l�i�n�gutxt����D�a�v�i�d� �R�a�m�e�yutxt����R�e�b�e�c�c�a� �J�a�c�k�s�o�nutxt����T�i�m� �R�i�l�e�yutxt����P�i�l�i� �M�e�y�e�rutxt����M�a�r�c� �T�h�o�m�s�e�nutxt����K�a�t�h�y� �B�r�o�w�nutxt����J�e�a�n� �I�r�v�i�n�eutxt����K�a�t�h�y� �L�o�v�eutxt����K�i�m�i� �R�o�b�e�r�t�s�o�nutxt����E�d� �S�u�m�p�t�e�rutxt����J�o� �C�u�m�m�i�n�sutxt����K�i�m� �B�o�w�e�rutxt����M�i�k�e� �F�u�l�l�e�rutxt����C�a�t�h�y� �R�e�e�dutxt����J�o�y�c�e� �U�n�d�e�r�w�o�o�dutxt����D�a�p�h�n�e� �E�s�h�o�mutxt����K�a�t�h�y� �B�r�o�w�nutxt����D�a�v�e� �R�a�m�e�yutxt����M�a�r�k� �D�e�R�o�u�s�i�eutxt����T�o�m� �B�l�o�r�eutxt����P�a�u�l� �B�e�c�kutxt����K�e�l�l�y� �J�o�h�n�s�o�nutxt����A�l�a�n� �B�u�r�w�e�l�lutxt����S�h�e�r�y�l� �B�u�r�l�e�yutxt����V�i�v�i�a�n� �L�a�n�d�v�i�kutxt����M�a�r�k� �H�c�H�u�g�hutxt����J�i�m� �N�e�w�t�o�nutxt����C�h�u�c�k� �T�u�r�n�e�rutxt����

所以它一段时间都很顺利,但最后没有逗号,而是gobblygook无法使用。我哪里出错?

1 个答案:

答案 0 :(得分:0)

不知道我究竟改变了什么,但它现在有效.....

tell application "Adobe InDesign CS5.5"
set myagents to (name of properties of every asset of library "Agents.indl" as list)
end tell

set agentlist to ""
set myagents to my simple_sort(myagents)
repeat with currentagent in myagents
set agentlist to agentlist & (currentagent) & ","
end repeat
set x to count agentlist
set agentlist to characters 1 thru (x - 1) of agentlist as string
my write_agents(agentlist)


on write_agents(this_list)
    set the agent_log to ("/Volumes/RealEstate/agents.txt" as POSIX file)
    try
        set eof of agent_log to 0
        open for access file the agent_log with write permission
    write (this_list) to file the agent_log starting at eof
    close access file the agent_log
on error
    try
        close access file the agent_log
    end try
end try
end write_agents


on simple_sort(my_list)
    set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
    set the low_item to ""
    repeat with i from 1 to (number of items in my_list)
        if i is not in the index_list then
            set this_item to item i of my_list as text
            if the low_item is "" then
                set the low_item to this_item
                set the low_item_index to i
            else if this_item comes before the low_item then
                set the low_item to this_item
                set the low_item_index to i
            end if
        end if
    end repeat
    set the end of sorted_list to the low_item
    set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort