代码之家  ›  专栏  ›  技术社区  ›  Darthwolff

在applescript中重命名一组quicktimes

  •  0
  • Darthwolff  · 技术社区  · 6 年前

    我在一个名为以下内容的文件夹中有一组文件:

    123456\u this\u is\u a\u fun\u test\u v01。压敏电阻

    685954\u this\u more\u is\u a\u fun\u test\u v01\u clean。压敏电阻

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

    这是\u a\u fun\u test\u 123456。压敏电阻

    此\u more\u is\u a\u fun\u test\u clean\u 685954。压敏电阻

    在automator中尝试了一些东西和一些简单的应用程序描述,但没有任何运气。我的脚本编写技能不好,我只是处于“业余”水平。有人有什么建议吗?

    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 回复  |  直到 6 年前
        1
  •  0
  •   CJK    6 年前

    我在这个AppleScript中添加了注释,解释了脚本的每个部分的作用。就目前而言,它的设计是从内部运行 脚本编辑器 ,但它可以很容易地调整为 自动机 工作流。

    Copy-n-将脚本粘贴到 脚本编辑器 . 代替 /路径/到/文件夹 包含文件夹的路径 .压敏电阻 文件已定位(保留引号)。使用完整路径,即。 /用户/CK/电影 而不是 ~/电影 . 按 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
    
        2
  •  0
  •   Darthwolff    6 年前

    通过一点google Fu和大量查看您的代码,我将其用于选定的文件。谢谢你的帮助

    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