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

时间:2013-02-14 00:51:16

标签: applescript automator

(这是我之前的一个问题的新编辑,获得了-3票。希望这个新版本有更好的资格)

我需要创建一个 Automator服务来将大量文件整理到文件夹中。我使用插图,并从每个 .ai 文件中创建 3种格式:[ name.pdf ], [名称BAJA.jpg ]和[ name.jpg ],共计4个文件

我的问题是,在本周我将这个过程重复到90多个不同的.ai文件。因此,90个文件* 4 360个独立文件全部进入某个项目文件夹。

我想将所有4个相关文件合并到一个文件夹中,将文件夹名称设置为与.ai文件相同

由于所有文件名都相同(除了一个),我想告诉查找器抓取所有具有相同名称的文件,复制名称,创建文件夹并将这些文件放入其中,但我有一个文件名variant [name LOW.jpg]也许我可以告诉脚本将该工作剥离为异常。

这样我将所有4个文件统一到一个文件夹中。 提前谢谢


更新:此问题最初发布于2013年,现在我有了解决方案。人们帮我组装这个脚本以满足我的需求。

我将其添加为服务并在MacOs上分配了键盘shurtcut。 这是代码:

on run {input, parameters} -- create folders from file names and move

    set output to {} -- this will be a list of the moved files

    repeat with anItem in the input -- step through each item in the input
        set {theContainer, theName, theExtension} to (getTheNames from anItem)
        try

            # check for a suffix and strip it off for the folder name
            if theName ends with " BAJA" then
                set destination to (makeNewFolder for (text 1 thru -6 of theName) at theContainer)
            else
                set destination to (makeNewFolder for theName at theContainer)
            end if

            tell application "Finder"
                move anItem to destination
                set the end of the output to the result as alias -- success
            end tell
        on error errorMessage -- duplicate name, permissions, etc
            log errorMessage
            # handle errors if desired - just skip for now
        end try
    end repeat

    return the output -- pass on the results to following actions
end run


to getTheNames from someItem -- get a container, name, and extension from a file item
    tell application "System Events" to tell disk item (someItem as text)
        set theContainer to the path of the container
        set {theName, theExtension} to {name, name extension}
    end tell
    if theExtension is not "" then
        set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
        set theExtension to "." & theExtension
    end if
    return {theContainer, theName, theExtension}
end getTheNames


to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist
    set theParent to theParent as text
    if theParent begins with "/" then set theParent to theParent as POSIX file as text
    try
        return (theParent & theChild) as alias
    on error errorMessage -- no folder
        log errorMessage
        tell application "Finder" to make new folder at theParent with properties {name:theChild}
        return the result as alias
    end try
end makeNewFolder

希望这有帮助。

2 个答案:

答案 0 :(得分:0)

您也可以将其保存为script.sh(在纯文本模式下的TextEdit中)并在终端中使用bash script.sh运行它:

cd ~/Target\ Folder/
for f in *.ai *.pdf *.jpg; do
    dir=${f%.*}
    dir=${dir% LOW}
    mkdir -p "$dir"
    mv "$f" "$dir"
done

答案 1 :(得分:0)

很遗憾,你个人喜欢回答这些问题,因为这有助于我练习和提高自己的技能。

感谢您发布解决方案。我认为这是一个很好的姿态,其他人会发现它很有用。

此脚本比使用"系统事件"有点短。而不是" Finder",因此对于大量文件会更快:

    set IllustratorOutputFolder to "/Users/CK/Desktop/example"

    tell application "System Events" to ¬
        set ai_files to every file in folder IllustratorOutputFolder ¬
            whose name extension is "ai"

    set Output to {}

    repeat with ai_file in ai_files
        set AppleScript's text item delimiters to "."

        get name of ai_file
        get text items of result

        set basename to reverse of rest of reverse of result as text

        tell application "System Events"
            get (every file in folder IllustratorOutputFolder ¬
                whose name begins with basename)

            move result to (make new folder ¬
                in folder IllustratorOutputFolder ¬
                with properties {name:basename})
        end tell

        set end of Output to result
    end repeat

    return Output -- list of lists of moved files

只是另一种做事方式。并不是说它更好或更糟,而只是一个不同的解决方案。