我想要的是用applescript执行一个查找和替换命令。我知道我可以使用namechanger和其他程序,但这需要太多的步骤。我关心的都是效率。
tell application "Finder"
set selected_items to selection
set jobNum to text returned of (display dialog "Job Number:" default answer "")
set name of document folder selection to jobNum (*if it did work it would rename the entire folder which isn't what I want. My goal is to replace all "12345" with the value of jobNum*)
end tell示例文件夹结构(我还不能提交图片,因为我需要10pt)
main folder: 12345_Sample Job Folder
- 12345_D.ai
- 12345_P.ai
- Proofs
- Working Files
- 12345.ai发布于 2015-07-18 07:27:01
尝试以下脚本:
set jobFolder to choose folder with prompt "Choose the Job Folder:"
tell application "Finder"
set rootName to jobFolder's name
set searchString to my FindSearchString(rootName)
set replaceString to text returned of (display dialog ("This script will search for files that contain " & searchString & ". Please enter the replacement text and click OK.") default answer searchString)
set everything to every file in (jobFolder's entire contents)
repeat with onething in everything
if ((onething's name) as text) contains searchString then
set onething's name to my replace_chars(((onething's name) as text), searchString, replaceString)
end if
end repeat
set jobFolder's name to my replace_chars(((jobFolder's name) as text), searchString, replaceString)
end tell
---------------------------------
on replace_chars(this_text, search_string, replacement_string)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to astid
return this_text
end replace_chars
---------------------------------
to FindSearchString(txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set sst to txt's text item 1
set AppleScript's text item delimiters to astid
return sst
end FindSearchString这只会影响初始选择的文件夹中的文件名,而不会影响子文件夹名称。最后,它会更改所选文件夹的名称。
重命名处理程序基于this page底部的代码
https://stackoverflow.com/questions/31478800
复制相似问题