如何制作文件夹操作以查看文件夹,如果它看到 2 个以上具有相同文件名和不同扩展名的文件,则创建新文件夹并将这些文件移动到文件夹中

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

我已经搜索了数周,但未能在我的 Mac 上运行 Ventura 找到一种方法来帮助解决这个问题。

我有很多文件具有相同的文件名,并以不同的扩展名保存,并且都存在于同一个文件夹中。我正在寻找一个脚本来用作将执行以下操作的文件夹操作。

  1. 观看文件夹并将其用作拖放区,并通过脚本执行文件夹操作。
  2. 如果文件夹看到 2 个或更多具有相同文件名的文件,则在工作文件夹内创建一个子文件夹,然后;
  3. 将具有相同名称但不同扩展名的 2 个或更多文件移动到新创建的子文件夹中。

例子;

观看的文件夹位置在我名为“工作”的主文件夹中。

Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif
Green Bird.png
Green Bird.jpg
Lizard Jumping.mkv

理想情况下,如果在所有这些项目所在的文件夹“工作”上作为文件夹操作运行,它将返回以下结果;

/users/blaine/work/Example Print/
Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt

/users/blaine/work/Weird Logo 3454/
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif

/users/blaine/work/Green Bird/
Green Bird.png
Green Bird.jpg

/users/blaine/work/Lizard Jumping.mkv

打开文件夹“工作”时应该看起来像这样。

/users/blaine/work/Example Print/
/users/blaine/work/Weird Logo 3454/
/users/blaine/work/Green Bird/
/users/blaine/work/Lizard Jumping.mkv

有什么想法吗?我设法创建了一个快捷方式,允许我运行“快速操作”,我可以在排序时手动突出显示文件并从选择中创建新文件夹并自动将新文件夹命名为与不带扩展名的文件名相同的名称,但是有成千上万,这让我相信一定有更好的方法。任何帮助表示赞赏。

这些是我能找到的关于这个主题的最接近的线索,但它们有点不同,我无法剖析和创作。

https://stackoverflow.com/a/57465078/21894037

从文件名创建新文件夹并移动文件

macos automation applescript automator
1个回答
0
投票

试试这个。添加到热(监视)文件夹的文件将被移动到热文件夹的相应子文件夹中。

on adding folder items to thisFolder after receiving these_items
    set thisFolderHFSPath to thisFolder as text -- this watch folder's HFS path
    repeat with anItem in these_items
        set baseName to my getBaseName(POSIX path of anItem) -- baseName of file
        tell application "Finder"
            try -- will determine file's appropriate subfolder
                set subFolder to folder (thisFolderHFSPath & baseName)
            on error -- if no appropriate subfolder, then create it
                set subFolder to make new folder at thisFolder with properties {name:baseName}
            end try
            try -- required try block here   
                move file (anItem as text) to subFolder
            end try
        end tell
    end repeat
end adding folder items to

on getBaseName(posixPath)
    set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
    if (posixPath ends with "/") then
        set baseName to text item -2 of posixPath
    else
        set baseName to text item -1 of posixPath
    end if
    if (baseName contains ".") then
        set AppleScript's text item delimiters to "."
        set baseName to text 1 thru text item -2 of baseName
    end if
    set AppleScript's text item delimiters to ATID
    return baseName
end getBaseName
© www.soinside.com 2019 - 2024. All rights reserved.