-2

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

我需要创建一个Automator 服务来将大量文件组织到文件夹中。我与illustrator合作,从每个.ai文件中我创建了另外3 种格式:[ name.pdf ]、[ name BAJA.jpg ] 和 [ name.jpg ] ,总共 4 个文件

我的问题是,在一周内,我对 90 多个不同的 .ai 文件重复此过程。所以 90 个文件 * 4 是360 个独立的文件都到某个项目文件夹中。

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

由于所有文件名都是相同的(除了一个),我想告诉 finder 抓取所有同名的文件,复制名称,创建一个文件夹并将这些文件放入其中,但我有一个文件名变体 [name LOW.jpg] 也许我可以告诉脚本将其作为例外剥离。

这样我会将所有 4 个文件统一到一个文件夹中。先感谢您


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

我将此添加为一项服务,并在 MacO 上分配了一个键盘快捷键。这是代码:

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

希望这可以帮助。

4

2 回答 2

1

很遗憾你被否决了,因为我个人喜欢回答这类问题,因为它可以帮助我练习和提高自己的技能。

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

此脚本比使用“系统事件”而不是“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

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

于 2017-12-26T00:59:18.417 回答
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
于 2013-02-14T09:28:43.800 回答