在 Apple 脚本中为重复文件添加后缀

问题描述 投票:0回答:1

我正在尝试编写一个脚本来保持我的下载文件夹干净,这对于大学/工作来说非常有用,因为文件会移动到正确的文件夹以允许在应用程序等中进行链接。

我有一个脚本,如果文件不存在,它会将文件移动到选定的文件夹(提示用户输入文件夹),并且我添加了一个选项,可以在用户愿意的情况下删除新文件(如果它存在),但是我想要添加一个选项来保留这两个文件,新文件被重命名,并在第二个文件的名称末尾添加“_2”,第三个文件的名称末尾添加“_3”,依此类推。

据我所知,我正在提示手动更改名称,而且这不起作用,我可以看到它增加了效率低下。据我所知,这是:

(* Version 1.2b
Move to Bin prompt added
Fixed activate Finder
add ability to keep a duplicate *)

on run {input, parameters}
    tell application "Finder"
        set fileToMove to item 1 of input
        set fileName to name of fileToMove
        set fileExtension to name extension of fileToMove ¬
            --gets extension incase file needs to be renamed
        activate
        set chosenFolder to POSIX file choose folder with prompt "Select a folder to move " & fileName & " "
        log chosenFolder
        
        try
            move fileToMove to folder chosenFolder
            -- Make sound play only on success
        on error
            -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
            
            display alert "Duplicate File Found" message "Would you like to move " & fileToMove & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep Both" cancel button "Cancel"
            set buttonReturned to button returned of result
            
            if buttonReturned is "Keep Both" then
                display dialog "Set New File Name for: " & fileName & "  " default answer fileName with icon note
                set newName to result
                set the name of fileToMove to newName
                set the extension of fileToMove to fileExtension
                move fileToMove to folder chosenFolder
                do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
            end if
            
            if buttonReturned is "Move Duplicate to Bin" then
                -- Move the file to the trash
                move fileToMove to trash
            end if
        end try
    end tell
end run

如有其他建议,欢迎完善

applescript automator
1个回答
0
投票

您的脚本无论如何都不起作用,因为按钮名称不匹配,并且

do shell script
行中缺少单引号字符。

没关系,您需要一个处理程序来检查文件名并添加索引。
这有点麻烦,因为必须覆盖“无扩展名”的情况,如果有扩展名,则必须从文件名中删除扩展名。

POSIX file choose folder

不需要。

choose folder
就足够了,您可以删除
folder
关键字。
同时移动和重命名文件的唯一方法是使用 shell 的 

mv

(* Version 1.2b
Move to Bin prompt added
Fixed activate Finder
add ability to keep a duplicate *)

on run {input, parameters}
    tell application "Finder"
        set input to {alias "Main:Users:stklieme:Desktop:Huber Frau Musika_title.pages"}
        set fileToMove to item 1 of input
        set fileName to name of fileToMove
        set fileExtension to name extension of fileToMove ¬
            --gets extension incase file needs to be renamed
        activate
        set chosenFolder to choose folder with prompt "Select a folder to move " & fileName & " "
        log POSIX path of chosenFolder
        
        try
            move fileToMove to chosenFolder
            -- Make sound play only on success
        on error
            -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
            
            display alert "Duplicate File Found" message "Would you like to move " & fileToMove & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename"
            set buttonReturned to button returned of result
            
            if buttonReturned is "Keep & Rename" then
                set newPath to my checkFileName(chosenFolder as text, fileName, fileExtension)
                do shell script "mv " & quoted form of POSIX path of fileToMove & space & quoted form of POSIX path of newPath
                do shell script "afplay '/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
            end if
            
            if buttonReturned is "Move Duplicate to Bin" then
                -- Move the file to the trash
                move fileToMove to trash
            end if
        end try
    end tell
end run

-- fDir: HFS path destination folder
-- fName: full file name
-- fExt: name extension

on checkFileName(fDir, fName, fExt)
    if fExt is "" then
        set fullExt to fExt
        set baseName to fName
    else
        set fullExt to "." & fExt
        set endIndex to offset of fullExt in fName
        set baseName to text 1 thru (endIndex - 1) of fName
    end if
    set _index to 0
    repeat
        set _index to _index + 1
        set newPath to (fDir & baseName & "_" & (_index as text) & fullExt)
        try
            newPath as alias
            -- file exists – repeat again
        on error
            -- file does does not exist – return new path
            return newPath
            
        end try
    end repeat
end checkFileName

© www.soinside.com 2019 - 2024. All rights reserved.