(Applescript) 重命名和移动/组织文件到子文件夹

(Applescript) Rename and Move / Organize Files to Subfolders

Edit: I have already created the subfolders beforehand using automator.

我是一名设计师,目前正在为 Android 设计。我使用 Sketch App 导出我的所有资产,以便它们具有等效的后缀(例如 -xxxhdpi、-xxhdpi、-xhdpi、-hdpi、-mdpi)。

文件示例: see the screenshot

文件夹示例: see the screenshot

我想做的是将所有这些资产移动到子文件夹(/xxxhdpi、/xxhdpi 等)和 trim 它们的后缀。

对 programming/applescripting 的了解为零,我尝试使用 Automator。但是我偶然发现了它对相对路径的限制。在搜索了很多资源(包括Looking for an Applescript to find files and move them to different folder and Applescript: Create folders/subfolders and move multiple files)之后。这有点令人困惑,因为我不太明白这些线在做什么。但是我想到了这个

tell application "Finder"

    set assetsFolder to (target of front Finder window) as text
    do shell script "cd " & (quoted form of POSIX path of assetsFolder) & "; "
    set selectedItems to selection

    repeat with this_item in selectedItems
        if this_item's name contains "-xxxhdpi" then
            set the theName to this_item's name
            set the theSource to "-xxxhdpi"
            set the theReplacement to ""
            move this_item to folder "xxxhdpi" of folder assetsFolder
            set name of this_item to my replace(theName, theSource, theReplacement)
        end if
    end repeat

end tell

on replace(theString, theSource, theReplacement)
    set AppleScript's text item delimiters to theSource
    set theItems to every text item of theString
    set AppleScript's text item delimiters to theReplacement
    return theItems as Unicode text
end replace

我意识到这只适用于 "xxxhdpi" 情况,因为我想先测试一下。以后希望这个脚本能有其他case来申请剩下的后缀(英文不好请见谅)

脚本本身可以很好地重命名和移动。但是我不能让他们一起工作。

现在,问题出在这两行内:

set name of this_item to my replace(theName, theSource, theReplacement)
move this_item to folder "xxxhdpi" of folder assetsFolder

要么文件重命名,但移动失败, 或文件已移动但重命名失败。

此外,我曾尝试搜索插件来完成这件事(组织 android 资产),但我没有成功。要么我在编写这个 applescript 时得到了帮助,要么有人会告诉我一个可以完成这项工作的等效插件。

提前致谢

试试这个,脚本假定子文件夹的名称始终是(最后一个)连字符和名称扩展名之前的点之间的部分,并且文件名中始终至少有一个连字符。

该脚本使用命令行界面ditto,可以即时创建中间目录。它将每个选定文件的名称和扩展名分开,并从文件名中去除子文件夹名称。没有必要创建子文件夹 "manually".

tell application "Finder"
    set selectedItems to selection
    if selectedItems is {} then return
    set parentFolder to POSIX path of (container of item 1 of selectedItems as text)
end tell

set {TID, text item delimiters} to {text item delimiters, "-"}
repeat with anItem in selectedItems
    set {fileName, fileExtension} to splitNameExtension(anItem)
    tell text items of fileName to set {prefix, suffix} to {items 1 thru -2 as text, item -1}
    set newFilePath to quoted form of (parentFolder & suffix & "/" & prefix & fileExtension)
    set sourceFile to quoted form of POSIX path of (anItem as text)
    do shell script "/usr/bin/ditto " & sourceFile & space & newFilePath & "; /bin/rm " & sourceFile
end repeat
set text item delimiters to TID

on splitNameExtension(aFile)
    set {name:fileName, name extension:fileExtension} to aFile
    if fileExtension is missing value then return {fileName, ""}
    return {text 1 thru ((count fileName) - (count fileExtension) - 1) of fileName, "." & fileExtension}
end splitNameExtension