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

(Applescript)重命名并将文件移动/组织到子文件夹,applescript,assets,automator,file-organization,Applescript,Assets,Automator,File Organization,编辑:我已经预先使用automator创建了子文件夹 我是一名设计师,目前正在为Android设计。我使用Sketch应用程序导出我的所有资产,以便它们具有等效的后缀(例如,-xxxhdpi,-xxhdpi,-xhdpi,-hdpi,-mdpi) 文件示例: 文件夹示例: 我试图做的是将所有这些资产移动到子文件夹(/xxxhdpi、/xxhdpi等)并修剪它们的后缀 由于我对编程/应用程序描述一无所知,所以我尝试使用Automator。但我无意中发现了相对路径的局限性。在搜索了大量资源后(包

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

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

文件示例:

文件夹示例:

我试图做的是将所有这些资产移动到子文件夹(/xxxhdpi、/xxhdpi等)并修剪它们的后缀

由于我对编程/应用程序描述一无所知,所以我尝试使用Automator。但我无意中发现了相对路径的局限性。在搜索了大量资源后(包括和)。这有点让人困惑,因为我不太明白这些台词在做什么。但是我想到了这个

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资产),但我没有运气。要么我得到了编写这个applescript的帮助,要么有人会告诉我一个同等的插件可以完成这项工作


提前感谢

尝试此操作,脚本假定子文件夹的名称始终位于名称扩展名之前的(最后一个)连字符和点之间,并且文件名中始终至少有一个连字符

该脚本使用命令行界面
同上
,可以动态创建中间目录。它分离每个选定文件的名称和扩展名,并将子文件夹名称与文件名分离。无需“手动”创建子文件夹


尝试此操作,脚本假定子文件夹的名称始终位于名称扩展名之前的(最后一个)连字符和点之间,并且文件名中始终至少有一个连字符

该脚本使用命令行界面
同上
,可以动态创建中间目录。它分离每个选定文件的名称和扩展名,并将子文件夹名称与文件名分离。无需“手动”创建子文件夹


您的脚本可能会失败,因为资产文件夹中不存在文件夹“xxxhdpi”。因此有两种解决方案:1)简单,在启动脚本之前,确保在asset文件夹中创建所有子文件夹(子文件夹xxxhdpi、xxhdpi、xhdpi、hdpi、mdpi)。2) 您可以更改脚本以添加检查子文件夹是否存在,如果不存在,则创建它。事实上,选项1非常简单!你好,谢谢你的回复!我忘了告诉你我已经创建了子文件夹。因此,该脚本只在一个条件下工作(重命名或移动)。您的脚本可能会失败,因为资产文件夹中不存在文件夹“xxxhdpi”。因此有两种解决方案:1)简单,在启动脚本之前,确保在asset文件夹中创建所有子文件夹(子文件夹xxxhdpi、xxhdpi、xhdpi、hdpi、mdpi)。2) 您可以更改脚本以添加检查子文件夹是否存在,如果不存在,则创建它。事实上,选项1非常简单!你好,谢谢你的回复!我忘了告诉你我已经创建了子文件夹。所以这个脚本只适用于一个条件(重命名或移动)。OMG!!这正是我想要实现的!我正在浏览你的代码,虽然我什么都不懂:))但无论如何,非常感谢你!!!你帮我节省了无数时间准备资产。非常感谢。天啊!!这正是我想要实现的!我正在浏览你的代码,虽然我什么都不懂:))但无论如何,非常感谢你!!!你帮我节省了无数时间准备资产。非常感谢。
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