Create new folder named with a specified file name in Automator
I want to cre开发者_JAVA技巧ate 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)".
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
精彩评论