Applescript 将变量转换为带 CSV 逗号的字符串

Applescript convert variable into a string with commas for CSV

我有一个包含 {"THIS", "THAT"} 的变量,我正在尝试将其写入 csv,以便 csv 格式为 THIS,THAT。目前它只是吐出 THISTHAT。

我想我需要重复变量但我不确定...

代码如下(检查重要位的--->):

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

试试这个,将列表转换为 CSV 的最简单方法是使用 text item delimiters.
主要问题是第 3 行中对字符串的强制转换。删除 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

这是另一种选择。只需修改您的 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",其中包含以下文本: 这个,那个