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

Automater/applescript help... Putting files in sub-folders with matching name

Hi,


I am new to automater and applescripts. Basically, I have a folder full of files of varying filetype (filecount is in the hundreds). I am trying to put every file in that folder into into its own subfolder that matches it's name (without the extension).


So ultimately I want the layout to go from:


Main Folder / Mountain.txt

Main Folder / Lion.avi

Main Folder / OSX.html


To...


Main Folder / Mountain / Mountain.txt

Main Folder / Lion / Lion.avi

Main Folder / OSX / OSX.html


So far I have been able to create a folder action that makes the subfolder when I add a file to the folder of my choice using something I found on stackoverflow (http://stackoverflow.com/questions/11220379/applescript-create-new-folder-from-f ile-name-and-move-file-into-that-folder)


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


The problem is that this doesn't move the files into the newly created subfolders! So, I did a little more hunting last night and found the following:

(Source: http://stackoverflow.com/questions/11220379/applescript-create-new-folder-from-f ile-name-and-move-file-into-that-folder)


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


However, this doesn't seem to work! I've tried in automater making both a folder action and application and neither seems to work. I really am new to automater and didn't know if somebody could help me accomplish what I am trying to do.


Thank you so much!


-Joe

Mac mini, OS X Mountain Lion (10.8.2)

Posted on Dec 29, 2012 10:46 AM

Reply
Question marked as Best reply

Posted on Dec 29, 2012 3:06 PM

Hi,


When using a folder action:

The script should check if the item is not a folder, otherwise you will get an error when creating the folder (because the same name).

The script must check if the folder doesn't exist before creating it, because (Lion.avi and Lion.jpg go to the same folder).

-----------

on run {input, parameters} -- make new folders from base file names
    repeat with anItem in input -- step through each item in the input
        tell application "Finder"
            if (class of item anItem) is not folder then --  not a folder
                set {name:theName, name extension:theExtension, container:theContainer} to anItem
                if theExtension is not "" then
                    set theName to text 1 thru -((count theExtension) + 2) of theName
                    set theExtension to "." & theExtension
                end if
                try
                    if not (exists folder theName of theContainer) then
                        move anItem to (make new folder at theContainer with properties {name:theName})
                    else -- exists folder 
                        move anItem to folder theName of theContainer
                    end if
                end try
            end if
        end tell
    end repeat
    return input
end run
2 replies
Question marked as Best reply

Dec 29, 2012 3:06 PM in response to ohNOitsJOE

Hi,


When using a folder action:

The script should check if the item is not a folder, otherwise you will get an error when creating the folder (because the same name).

The script must check if the folder doesn't exist before creating it, because (Lion.avi and Lion.jpg go to the same folder).

-----------

on run {input, parameters} -- make new folders from base file names
    repeat with anItem in input -- step through each item in the input
        tell application "Finder"
            if (class of item anItem) is not folder then --  not a folder
                set {name:theName, name extension:theExtension, container:theContainer} to anItem
                if theExtension is not "" then
                    set theName to text 1 thru -((count theExtension) + 2) of theName
                    set theExtension to "." & theExtension
                end if
                try
                    if not (exists folder theName of theContainer) then
                        move anItem to (make new folder at theContainer with properties {name:theName})
                    else -- exists folder 
                        move anItem to folder theName of theContainer
                    end if
                end try
            end if
        end tell
    end repeat
    return input
end run

Automater/applescript help... Putting files in sub-folders with matching name

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