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

时间:2016-03-17 04:03:20

标签: applescript assets automator file-organization

  

编辑:我已经使用automator预先创建了子文件夹。

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

文件示例: see the screenshot

文件夹示例: see the screenshot

我尝试做的是将所有这些资源移至子文件夹(/ xxxhdpi,/ xxhdpi等)并修剪其后缀。

在编程/编写应用程序方面没有任何知识,我尝试使用Automator。但我偶然发现它对相对路径的限制。搜索了大量资源(包括Looking for an Applescript to find files and move them to different folderApplescript: 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"因为我想先测试它。在以后的时间里,我希望这个剧本有其他案例适用于其余的后缀(如果我的英语不好,我很抱歉)。

脚本本身可以正常重命名和移动。但我不能让他们一起工作。

目前,问题在于这两行:

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

文件已重命名,但无法移动, 或文件已移动但无法重命名。

另外,我试图搜索插件来做这件事(组织android资产),但我没有运气。要么我得到了写这个苹果的帮助,要么有人会告诉我一个能完成这项工作的等效插件。

提前致谢

1 个答案:

答案 0 :(得分:3)

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

该脚本使用命令行界面ditto,该界面能够即时创建中间目录。它分隔每个选定文件的名称和扩展名,并从文件名中删除子文件夹名称。没有必要手动创建子文件夹"。

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