如何将文件复制到文件夹并根据文件夹名称编辑文件中的变量

时间:2019-05-28 18:09:28

标签: applescript

我有一个.xml文件,需要将其复制到文件夹的每个子文件夹中。该文件中有一个变量需要更改以反映文件夹的名称(无路径)。

我提供的代码在选定的主文件夹中生成新文件夹。它通过获取该文件夹中文件(例如.jpgs)的名称,然后创建具有相同名称的新文件夹并将每个.jpg放入其各自的文件夹中来进行此操作。然后,现在我需要它来编辑xml中已设置为“ vID”的变量,然后将其复制到每个文件中。

tell application "Finder"


    set selected to selection
    set current_folder to item 1 of selected
    set mlist to every file of current_folder
    set theFile to choose file (* XML file with variable *)
    set MyFolder to current_folder

    repeat with this_file in mlist
        set cur_ext to name extension of this_file
        set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

        set stringToFind to "vID"
        set stringToReplace to new_name
        set theContent to read theFile as «class utf8»
        set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
        set ti to every text item of theContent
        set AppleScript's text item delimiters to stringToReplace
        set newContent to ti as string
        set AppleScript's text item delimiters to oldTID
        try
            set fd to open for access theFile with write permission
            set eof of fd to 0
            write newContent to fd as «class utf8»
            close access fd
        on error
            close access theFile
        end try

        set new_folder to make new folder with properties {name:new_name} at current_folder




        move this_file to new_folder

        duplicate MyFile to new_folder

    end repeat



end tell

当前,代码正确生成了文件夹,将.jpgs移入了它们的文件夹,编辑了xml并将其复制到了每个文件夹中。问题在于该变量不能反映每个文件夹。它只是更改为最后一个文件夹的名称。如果需要进一步说明,请告诉我。

1 个答案:

答案 0 :(得分:0)

您的XML文件没有更新,因为您覆盖了模板,该模板替换了占位符字符串。您可以制作模板的副本,然后编辑副本,也可以直接编写新的XML内容:

Alarm