Create new folder named with a specified file name in Automator

瘦欲@ 提交于 2019-12-11 05:09:33

问题


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)".


回答1:


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


来源:https://stackoverflow.com/questions/4204372/create-new-folder-named-with-a-specified-file-name-in-automator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!