Как автоматизировать создание папки и текстового файла в ней из данных CSV - PullRequest
0 голосов
/ 10 февраля 2020

Для каждой строки текстового файла с разделителями я хочу, чтобы Automator создал папку с именем из поля исходного файла.

Затем в этой папке создайте текстовый файл с именем «file. json», содержимое которого представляет собой строку данных из другого поля исходного файла.

Файл CSV будет иметь около 100 записей, и, вероятно, только два поля, такие как

folderName | { JSON was here }

Как мне go сделать это?

1 Ответ

1 голос
/ 10 февраля 2020

Я не очень хорошо знаю 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
...