用applescript重命名一堆快速时间

时间:2018-02-12 14:05:49

标签: applescript automator

我在一个名为的文件夹中有一堆文件:

123456_this_is_a_fun_test_v01.mov

685954_this_more_is_a_fun_test_v01_clean.mov

它们在开头都有一个6位数字,在某处有一个版本号。我需要做的是删除版本号并移动前6位数字直到结尾,在扩展名称之前,如下所示:

this_is_a_fun_test_123456.mov

this_more_is_a_fun_test_clean_685954.mov

在automator和一些简单的Applescripting尝试了一些东西,但没有任何运气。我的脚本技能不好,我只是处于“业余爱好”水平。有人有意见吗?

tell application "Finder"
--grab the selected files and put them into a variable
set F to selection


 end tell
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"

 repeat with g in F -- Loop through the file list

-- Get the filename
tell application "System Events" to get name of g

-- Eliminate the version number component of the filename
set r to do shell script ¬
    "echo " & quoted form of result & ¬
    " | egrep -o '[^_\\.]+'" & ¬
    " | egrep -iv 'v\\d+'"

-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"

-- Rename the file to the new name
tell application "System Events" to set name of g to result

end repeat

2 个答案:

答案 0 :(得分:0)

我已经为这个AppleScript添加了注释,它解释了脚本的每个部分的作用。目前,它设计为从脚本编辑器内部运行,但它可以很容易地调整为 Automator 工作流程的一部分。

将脚本复制粘贴到脚本编辑器中。将 / Path / To / Folder 替换为 .mov 文件所在文件夹的路径(保留引号)。使用完整路径,即 / Users / CK / Movies ,而不是〜/ Movies 。按 Cmd + K 编译脚本,并检查预运行语法错误等。当字体改变并且脚本看起来都是漂亮的,然后点击 Cmd + R 来执行它。

    tell application "System Events" to get the files in folder ¬
        "/Path/To/Folder" whose name extension is "mov"

    set F to the result -- The list of .mov files that need renaming

    -- This will be the character used to rejoin pieces
    -- of the filename after breaking them apart
    set the text item delimiters to "_"

    repeat with g in F -- Loop through the file list

        -- Get the filename
        tell application "System Events" to get name of g

        -- Eliminate the version number component of the filename
        set r to do shell script ¬
            "echo " & quoted form of result & ¬
            " | egrep -o '[^_\\.]+'" & ¬
            " | egrep -iv 'v\\d+'"

        -- Assemble the other components in the new order
        get {paragraphs 2 thru -2, paragraph 1} of r
        get (result as text) & ".mov"

        -- Rename the file to the new name
        tell application "System Events" to set name of g to result

    end repeat

答案 1 :(得分:0)

有了一些谷歌富和很多看你的代码我得到它来处理选定的文件。谢谢你的帮助

tell application "Finder"
set F to selection
end tell


set the text item delimiters to "_"

repeat with g in F -- Loop through the file list

-- Get the filename
tell application "Finder" to get name of g

-- Eliminate the version number component of the filename
set r to do shell script ¬
    "echo " & quoted form of result & ¬
    " | egrep -o '[^_\\.]+'" & ¬
    " | egrep -iv 'v\\d+'"

-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"

-- Rename the file to the new name
tell application "Finder" to set name of g to result

end repeat

tell application "System Events"
activate
display dialog "Selected files have been renamed!"
end tell