You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator: Create folder from filename

I am trying to use Automator to query a file in a foldr, then create a folder based off of the filename, then move the file into the newly created folder. I am not as savvy as I thought that I was when it comes to Automator. Even thinking that Automator might not be the correct tool for the job. Each file has a different name or this would be a lot easier. Any help would be greatly appreciated.

Posted on Dec 23, 2012 8:02 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 23, 2012 8:55 PM

So I found this Script:


tell application "Finder"

set current_folder to folder of front window

set x to (get the selection) as list

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to makenewfolderwith properties {name:new_name} atcurrent_folder


movethis_filetonew_folder

else


movethis_filetonew_folder

end if

end repeat

end tell


But it only works if the file is selected in the finder window. How can it be modified to select the file/files in a folder to process them ?

24 replies
Question marked as Top-ranking reply

Dec 23, 2012 8:55 PM in response to bullware

So I found this Script:


tell application "Finder"

set current_folder to folder of front window

set x to (get the selection) as list

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to makenewfolderwith properties {name:new_name} atcurrent_folder


movethis_filetonew_folder

else


movethis_filetonew_folder

end if

end repeat

end tell


But it only works if the file is selected in the finder window. How can it be modified to select the file/files in a folder to process them ?

Dec 23, 2012 10:04 PM in response to bullware

You can use a Run AppleScript action to do the work, and feed items to it by using a service workflow, an Ask for Finder Items action, etc - for example:


1) Service receives selected files or folders in Finder

2) Get Folder Contents

3) Run AppleScript

onrun {input, parameters} -- create folders from file names and move



setoutputto {} -- this will be a list of the moved files



repeatwithanItemintheinput-- step through each item in the input

set {theContainer, theName, theExtension} to (getTheNamesfromanItem)

try

setdestinationto (makeNewFolderfortheNameattheContainer)

tellapplication"Finder"

moveanItemtodestination

settheendoftheoutputtotheresultasalias-- success

endtell

onerrorerrorMessage-- duplicate name, permissions, etc

logerrorMessage

# handle errors if desired - just skip for now

endtry


endrepeat



returntheoutput-- pass on the results to following actions

endrun



togetTheNamesfromsomeItem-- get a container, name, and extension from a file item


tellapplication"System Events"totelldisk item (someItemastext)

settheContainertothepathofthecontainer

set {theName, theExtension} to {name, name extension}


endtell


iftheExtensionisnot""then

settheNametotext1thru -((counttheExtension) + 2) oftheName-- just the name part

settheExtensionto"." & theExtension


endif


return {theContainer, theName, theExtension}

endgetTheNames



tomakeNewFolderfortheChildattheParent-- make a new child folder at the parent location if it doesn't already exist


settheParenttotheParentastext


iftheParentbegins with"/"thensettheParenttotheParentasPOSIX fileastext


try

return (theParent & theChild) asalias


onerrorerrorMessage-- no folder

logerrorMessage

tellapplication"Finder"tomakenewfolderattheParentwith properties {name:theChild}

returntheresultasalias


endtry

endmakeNewFolder

4) other actions as desired

Dec 24, 2012 6:11 PM in response to bullware

Your original request was to use Automator, so the script is designed to be used in an Automator Run AppleScript action (paste it over the action's default script). When creating a new Automator workflow, there are several different kinds of workflow templates available, including a Service (which gets its input from selected items) and a Folder Action (which gets its input from items dropped in an attached folder)


The script can be modified to run as a regular script from the AppleScript Editor, although it is easier to create services in Automator.

Feb 13, 2013 5:36 PM in response to red_menace

@red_menace: This apple script worked like a charm on my test, but I need something very much like this.


I have 4 different format files with same filename except from one (aaa LOW.jpg):

1) newspaper ad 15x35.ai

2) newspaper ad 15x35.pdf

3) newspaper ad 15x35 LOW.jpg

4) newspaper ad 15x35.jpg


What I want is to create a new folder in the same place where this files are, but using the Ai file (newspaper ad 15x35), then moving all 4 files into this new folder. This Ai file is my master file and the othe three files are the files sent to the printing house, so they are the same, but in different format.


Now, I may have sometimes around 50 master files (Ai) in the same project folder with their PDF and JPG files, being up to 200 files. In my head I want to select all this content, and wish the applescript-automator only grabs the Ai filenames and put 4 files into each folder. Resulting 50 folders (each named after the Ai master file) and every folder with 4 files moved into them.


My master files are usually named different because of the number on the filename, por example newspaper ad 15x35.ai, newspaper ad 13,5x25.ai, newspaper ad 32x29,08.ai


Is this possible?

Kind regards

Feb 13, 2013 7:51 PM in response to alexgsrubio

Since your file groups just have different extensions (the folder name doesn't use the extension), all you need to do is check for the one that has the "LOW" suffix - changing the run handler to the following should do it:


onrun {input, parameters} -- create folders from file names and move



setoutputto {} -- this will be a list of the moved files



repeatwithanItemintheinput-- step through each item in the input

set {theContainer, theName, theExtension} to (getTheNamesfromanItem)

try

# check for a suffix and strip it off for the folder name

iftheNameends with" LOW"then

setdestinationto (makeNewFolderfor (text1thru-5oftheName) attheContainer)

else

setdestinationto (makeNewFolderfortheNameattheContainer)

endif

tellapplication"Finder"

moveanItemtodestination

settheendoftheoutputtotheresultasalias-- success

endtell

onerrorerrorMessage-- duplicate name, permissions, etc

logerrorMessage

# handle errors if desired - just skip for now

endtry


endrepeat



returntheoutput-- pass on the results to following actions

endrun

Feb 14, 2013 8:12 AM in response to red_menace

The first script perfectly makes a new folder and move all the files with the same name, but the files that ends with <space>LOW, they get a new folder for themselves.


Now I am trying the second script (for the LOW ending). Making a new services on its own sends me an error. If this is a complementary script, should I add this at the end of the first script? 😕

Feb 14, 2013 9:41 AM in response to red_menace

I have replaced the second script, with no error on syntax, but now I have 2 folders. One containing three files with the same name. And the file named baja is now in a different folder but the name has an extra space.


I get this folders:

experiment

and

experiment<space>

In this last one is the file with LOW at the end


I think is about this line:

set destination to (makeNewFolder for (text 1 thru -5 of theName) at theContainer)

which sets a new folder for this LOW file, instead of just moving this LOW file into the same folder as the rest.


This is the updated script:


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 " LOW" then

set destination to (makeNewFolder for (text 1 thru -5 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 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

Automator: Create folder from filename

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.