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

Applescript 从文件名创建新文件夹并移动文件,applescript,automator,Applescript,Automator,(这是我上一个问题的新编辑,获得了-3票。希望这个新问题有更好的资格) 我需要创建一个自动程序服务,将大量文件组织到文件夹中。我与illustrator一起工作,从每个.ai文件中我创建了三种以上的格式:[name.pdf]、[name BAJA.jpg]和[name.jpg],总共四个文件 我的问题是,在这一周里,我对90多个不同的.ai文件重复了这个过程。因此,90个文件*4是360个独立文件全部放入某个项目文件夹中 我想将所有4个相关文件抓取到一个文件夹中,并将文件夹名称设置为与.ai文件

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

我需要创建一个自动程序服务,将大量文件组织到文件夹中。我与illustrator一起工作,从每个.ai文件中我创建了三种以上的格式:[name.pdf]、[name BAJA.jpg]和[name.jpg],总共四个文件

我的问题是,在这一周里,我对90多个不同的.ai文件重复了这个过程。因此,90个文件*4是360个独立文件全部放入某个项目文件夹中

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

因为所有的文件名都是相同的(除了一个),我想告诉finder抓取所有同名的文件,复制名称,创建一个文件夹,然后把这些文件放在里面,但是我有一个文件名变体[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

希望这有帮助。

您也可以将其另存为
script.sh
(在纯文本模式下的文本编辑中),并在终端中使用
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

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

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

此脚本略短于,并使用“系统事件”而不是“查找器”,因此对于大量文件来说会更快:

    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
只是做事情的另一种方式。并不是说它是好是坏,而是一个不同的解决方案。

您应该发布或雇佣该领域众多有才华的开发人员之一