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 并将其复制到每个文件夹中。问题是该变量不能反映每个文件夹。它只是更改为最后一个文件夹的名称。如果您需要进一步说明,请告诉我。
解决方案
您的 XML 文件没有得到更新,因为您覆盖了替换占位符字符串的模板。您可以复制模板然后编辑该副本,或者直接编写新的 XML 内容:
global base_template -- this will be the master template to copy
set base_template to (choose file with prompt "Choose the template file:" of type "com.apple.property-list") -- XML file with placeholder text to replace
tell application "Finder"
set current_folder to item 1 of (get selection)
repeat with this_file in (get files of current_folder)
set {file_name, file_extension} to my getNamePieces(this_file)
set new_folder to make new folder with properties {name:file_name} at current_folder
move this_file to new_folder
set new_template to (new_folder as text) & (name of base_template)
my copyTemplate(file_name, new_template)
end repeat
end tell
to copyTemplate(name_text, new_template) -- write edited copy of template
set placeholder_text to "vID"
set base_content to read base_template as «class utf8»
set {temp_TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, placeholder_text}
set {item_list, AppleScript's text item delimiters} to {text items of base_content, name_text}
set {new_content, AppleScript's text item delimiters} to {item_list as text, temp_TID}
try
set fd to (open for access file new_template with write permission)
write new_content to fd as «class utf8»
close access fd
on error errmess -- oops, make sure file gets closed
log errmess
close access fd
end try
end copyTemplate
to getNamePieces(file_item) -- get base file name and extension
tell application "System Events" to tell disk item (file_item as text) to set {_name, _extension} to {name, name extension}
if _extension is not "" then
set _name to text 1 thru -((count _extension) + 2) of _name
set _extension to "." & _extension
end if
return {_name, _extension}
end getNamePieces
推荐阅读
- r - 将一个主题观察合并到所有其他观察
- java - 如何将选定的 Maven 配置文件传递给 Spring 配置文件?
- angular6 - 如何测试 Web 应用 UI 的布局是否正确。我应该在 Angular 中使用 Karma/Jasmine 吗?
- css - 导航栏固定顶部内的 Bootstrap 4 下拉问题
- asp.net-mvc - 目前我正在尝试制作一个 EF 数据库,我想知道为什么以及如何让这个“NULL”远离我的表
- c# - 拦截键盘输入并返回自定义值
- html - Outlook 的电子邮件问题
- python - 可以通过命令行连接到数据库,但不能在 Python 脚本中连接?
- java - JFrame 最初出现的最大宽度限制为我的显示器宽度
- java - 将 InputStream 作为输入的对象方法转换为要打印的数组