首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从文本返回变量重命名文件夹及其部分内容

如何从文本返回变量重命名文件夹及其部分内容
EN

Stack Overflow用户
提问于 2015-07-17 23:00:02
回答 1查看 359关注 0票数 0

我想要的是用applescript执行一个查找和替换命令。我知道我可以使用namechanger和其他程序,但这需要太多的步骤。我关心的都是效率。

代码语言:javascript
复制
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)

代码语言:javascript
复制
main folder: 12345_Sample Job Folder
 - 12345_D.ai
 - 12345_P.ai
 - Proofs
 - Working Files
      - 12345.ai
EN

回答 1

Stack Overflow用户

发布于 2015-07-18 07:27:01

尝试以下脚本:

代码语言:javascript
复制
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底部的代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31478800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档