在 Automator 中创建以指定文件名命名的新文件夹

发布于 2024-10-03 03:01:31 字数 114 浏览 1 评论 0原文

我想创建一个新文件夹,以用于输入的文件的文件名命名。

示例:

如果我将新服务与文件“test (test).jpg”一起使用,我想自动创建一个名为“test (test)”的文件夹。

I want to create a new folder named with the filename of the file used for the input.

Example:

If I use my new service with the file "test (test).jpg" I want to automatically create a folder named "test (test)".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2024-10-10 03:01:31

要使用常规 Automator 操作执行此操作,会有点复杂,因为您需要保存原始输入、获取名称、创建文件夹、取回原始输入等。您可以使用运行 AppleScript 一次性完成大部分操作的操作,尽管这取决于您想要对原始输入和创建的文件夹路径执行的操作。

以下运行AppleScript操作将使用输入项的名称创建新文件夹(请注意,服务可以传递多个项目),并且仅传递原始输入。新文件夹在同一父文件夹中创建 - 不执行错误处理(重复名称等):

on run {input, parameters} -- make new folders from base file names

    set output to {}

    repeat with anItem in the input -- step through each item in the input

        set anItem to anItem as text
        tell application "System Events" to tell disk item anItem
            set theContainer to path of container
            set {theName, theExtension} to {name, name extension}
        end tell
        if theExtension is in {missing value, ""} then
            set theExtension to ""
        else
            set theExtension to "." & theExtension
        end if
        set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

        tell application "Finder"
            make new folder at folder theContainer with properties {name:theName}
            set end of output to result as alias
        end tell
    end repeat

    return input -- or output
end run

To do this with regular Automator actions it gets a bit convoluted, since you need to save the original input, get the name, make the folder, get the original input back, etc. You can use a Run AppleScript action to do most of that in one go, although it depends on what you are wanting to do with the original input and created folder paths.

The following Run AppleScript action will create new folders with the name of the input items (note that a service can pass multiple items), and just passes the original input on. The new folders are created in the same parent folder - no error handling (duplicate names, etc) is performed:

on run {input, parameters} -- make new folders from base file names

    set output to {}

    repeat with anItem in the input -- step through each item in the input

        set anItem to anItem as text
        tell application "System Events" to tell disk item anItem
            set theContainer to path of container
            set {theName, theExtension} to {name, name extension}
        end tell
        if theExtension is in {missing value, ""} then
            set theExtension to ""
        else
            set theExtension to "." & theExtension
        end if
        set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part

        tell application "Finder"
            make new folder at folder theContainer with properties {name:theName}
            set end of output to result as alias
        end tell
    end repeat

    return input -- or output
end run
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文