如何根据 CSV 数据自动创建文件夹和其中的文本文件
How to automate the creation of a folder, and a text file within it from CSV data
对于带分隔符的文本文件的每一行,我想让 Automator 创建一个以源文件的字段命名的文件夹。
然后在该文件夹中,创建一个名为 "file.json" 的文本文件,其内容是来自源文件另一个字段的一串数据。
CSV 文件大约有 100 个条目,可能只有两个字段,例如
folderName | { JSON was here }
我该怎么做?
我不太了解 automator,但以下 AppleScript 应该可以满足您的需求。没有太多的错误检查方式。
try
set src to (choose file with prompt "choose your input")
set o to (open for access src)
set inputStr to (read o)
close access o
end try
set fldrOut to (choose folder with prompt "choose your output folder")
set atids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
repeat with p in (paragraphs of inputStr)
set folderName to text item 1 of (p as string)
set jsonString to text item 2 of (p as string)
tell application "Finder"
set fldr to (make new folder at fldrOut with properties {name:folderName})
end tell
set pth to (fldrOut as string) & folderName & ":file.json"
set outFile to (open for access pth with write permission)
write jsonString to outFile starting at 0
close access outFile
end repeat
set AppleScript's text item delimiters to atids
对于带分隔符的文本文件的每一行,我想让 Automator 创建一个以源文件的字段命名的文件夹。
然后在该文件夹中,创建一个名为 "file.json" 的文本文件,其内容是来自源文件另一个字段的一串数据。
CSV 文件大约有 100 个条目,可能只有两个字段,例如
folderName | { JSON was here }
我该怎么做?
我不太了解 automator,但以下 AppleScript 应该可以满足您的需求。没有太多的错误检查方式。
try
set src to (choose file with prompt "choose your input")
set o to (open for access src)
set inputStr to (read o)
close access o
end try
set fldrOut to (choose folder with prompt "choose your output folder")
set atids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
repeat with p in (paragraphs of inputStr)
set folderName to text item 1 of (p as string)
set jsonString to text item 2 of (p as string)
tell application "Finder"
set fldr to (make new folder at fldrOut with properties {name:folderName})
end tell
set pth to (fldrOut as string) & folderName & ":file.json"
set outFile to (open for access pth with write permission)
write jsonString to outFile starting at 0
close access outFile
end repeat
set AppleScript's text item delimiters to atids