2015-08-26 4 views
0

У меня есть переменная, которая содержит {«ЭТО», «ЧТО»}, которую я пытаюсь записать в csv, так что форматы csv как ЭТО, ЭТО. Текущее это просто выплевывается как ЭТОТ.Applescript преобразовать переменную в строку с запятыми для CSV

Я думаю, что нужно повторить хотя переменной, но я не уверен ...

код выглядит следующим образом (проверьте ---> для важных битов):

tell application "Adobe InDesign CC 2014" 
delete unused swatches of document 1 
set _NotUSED to {"None", "Paper", "Black", "Registration", "Keyline", "ImageLabel", "C=0 M=0 Y=0 K=37", "C=0 M=100 Y=100 K=0", "Map this to white ->", "Dieline", "C=0 M=100 Y=0 K=0"} as string 

try 
---> Get the variables 
    set _UpDatedList to get (name of swatches of document 1 whose name is not in _NotUSED) 

on error 

    display alert {"Your document has no spot colours"} 

end try 

end tell 

set filePath to (path to desktop as text) & "Pantones.csv" 

---> Set the theString to the variables 
set theString to _UpDatedList as string 

set theResult to writeTo(filePath, theString, text, false) 

if not theResult then display dialog "There was an error writing the data!" 

on writeTo(targetFile, theData) 
try 

    set openFile to open for access file targetFile with write permission 

---> write the variables to csv 
    write theData to openFile 
    close access openFile 
    return true 
    on error 
    try 
     close access file targetFile 
    end try 
    return false 
end try 
end writeTo 

ответ

1

Попробуйте это, самый простой способ конвертировать список в CSV - использовать text item delimiters.
Основная проблема заключается в принуждении к строке в третьей строке. Удалить as string.

tell application "Adobe InDesign CC 2014" 
    delete unused swatches of document 1 
    set _NotUSED to {"None", "Paper", "Black", "Registration", "Keyline", "ImageLabel", "C=0 M=0 Y=0 K=37", "C=0 M=100 Y=100 K=0", "Map this to white ->", "Dieline", "C=0 M=100 Y=0 K=0"} 
    try 
     set _UpDatedList to (get name of swatches of document 1 whose name is not in _NotUSED) 
    on error 
     display alert "Your document has no spot colours" buttons {"Cancel"} 
     return -- abort the script 
    end try 
end tell 

set filePath to (path to desktop as text) & "Pantones.csv" 

set {TID, text item delimiters} to {text item delimiters, ","} 
set csvString to _UpDatedList as text 
set text item delimiters to TID 

set theResult to writeTo(filePath, csvString) 

if not theResult then display dialog "There was an error writing the data!" 

on writeTo(targetFile, theData) 
    try 
     set openFile to open for access file targetFile with write permission 
     write theData to openFile 
     close access openFile 
     return true 
    on error 
     try 
      close access file targetFile 
     end try 
     return false 
    end try 
end writeTo 
0

Вот еще один вариант. Просто измените обработчик WriteTo, как показано ниже, чтобы сообщить ему добавить запятую после каждого элемента в списке, кроме последнего элемента.

-- Define theString and the target file 
set theString to {"This", "That"} 
set theFile to (path to desktop as text) & "Pantones.csv" 

-- Execute the write handler 
set theResult to writeTo (theFile, theString) 


on writeTo(targetFile, theData) 
set openFile to open for access file targetFile with write permission 

set theCount to the count of items in theData 
set n to 0 

--write to the target file & add a comma after every item except the last 
repeat theCount times 
    set n to n + 1 
    if n is not theCount then write item n of theData & "," to openFile as string 
    if n is theCount then write item n of theData to openFile as string 
end repeat 

close access openFile 
end writeTo 

В этом примере, конечный результат будет файл "Pantones.csv" со следующим текстом: This, что