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

Using AppleScript to sort files Into Specified Folders based on Extension

Hi All!


Here is what I am trying to figure out. I would love some help with this script if anyone can lend a helping hand. I am currently using Hazel to do this but I think AppleScript will be alot more effective.


Situation:


- I have a folder with 4 different file types inside - the extensions are (.cop, .cof, .cos, .cr2)

- These files need to be organized in this folder structure Captureone > Cache > Proxies

> Settings50


- The .cop & .cof files both need to be put in the "Proxies" folder

- The .cos needs to be put in the "Settings50" folder

- The .cr2 can stay where it is.



I'm already using an AppleScript application where it automatically creates the folder structure in the current finder location, which works great. I use a program named Hazel to sort the files automatically into the folders where they need to go. Unfortunately all my computers at work don't have Hazel and I would like help to make a script. Any thoughts to add to my existing script?



# define a list of folders - items will need to be quoted if they contain spaces, etc.

property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"


try

tell application "Finder" to set targetFolder to (target of the front window) as alias

on error -- no window

set targetFolder to (choose folder)

end try


# build a parameter string from the folder list

set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}


do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders


MacBook Pro, Mac OS X (10.6.8)

Posted on Mar 14, 2012 3:33 PM

Reply
15 replies

Mar 14, 2012 3:39 PM in response to acabaero

Here:


tell application "Finder"

move (every file of window 1 whose name extension is "cop") to folder "Captureone:Cache:Proxies" of window 1

move (every file of window 1 whose name extension is "cof") to folder "Captureone:Cache:Proxies" of window 1

move (every file of window 1 whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of window 1

end tell


(64829)

Mar 14, 2012 3:45 PM in response to Niel

Hi Niel,


Thanks for the help. I added what you said and got this error. Any thoughts?



# define a list of folders - items will need to be quoted if they contain spaces, etc.

property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"


try

tell application "Finder" to set targetFolder to (target of the front window) as alias

on error -- no window

set targetFolder to (choose folder)

end try


# build a parameter string from the folder list

set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}


do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders


tell application "Finder"

move (every file of window 1 whose name extension is "cop") to folder "Captureone:Cache:Proxies" of window 1

move (every file of window 1 whose name extension is "cof") to folder "Captureone:Cache:Proxies" of window 1

move (every file of window 1 whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of window 1

end tell




User uploaded file

Mar 14, 2012 4:02 PM in response to Niel

Hi Niel, sorry for all the questions. This time I changed 'of window 1' to 'targetFolder' like you said. And I got this. I am saving it as an application in AppleScript then dragging it to my tool bar of my finder window. I am selecting the folder I want and hitting the application button. Is this the correct way I should launch the script to work on the folder?



# define a list of folders - items will need to be quoted if they contain spaces, etc.

property theFolders : {"Captureone/Cache/Proxies", "Captureone/Settings50"} -- can also nest, e.g. "Inside/One"


try

tell application "Finder" to set targetFolder to (target of the front window) as alias

on error -- no window

set targetFolder to (choose folder)

end try


# build a parameter string from the folder list

set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}

set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}


do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders


tell application "Finder"

move (every file of targetFolder whose name extension is "cop") to folder "Captureone:Cache:Proxies" of targetFolder

move (every file of targetFolder whose name extension is "cof") to folder "Captureone:Cache:Proxies" of targetFolder

move (every file of targetFolder whose name extension is "cos") to folder "Captureone:Cache:Proxies:Settings50" of targetFolder

end tell




User uploaded file

Mar 14, 2012 4:54 PM in response to acabaero

Hi,


acabaero wrote:



User uploaded file

Because the folder "Captureone:Cache:Proxies:Settings50" is not created.


The script create the folder "Captureone:Settings50"


-------------------

try

tellapplication "Finder" tosettargetFolderto (targetofthefrontFinder window) asalias

onerror -- no window

settargetFolderto (choose folder)

endtry


do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"


tell application "Finder" to tell targetFolder

move (files whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies"

move (files whose name extension is "cos") to folder "Captureone:Settings50"

endtell

Mar 14, 2012 5:25 PM in response to Jacques Rioux

Hi Jacques,


Below is my folder structure. The script in my first post works perfectly find to create the folder structure I need. The second part to sort the images is where I can't get it to work. Maybe this image will help illustrate where I need files to go. Can you please send me the full script text I should be using if you can? Thanks for the help!


User uploaded file

User uploaded file

Mar 14, 2012 5:26 PM in response to acabaero

acabaero wrote:


Hi Jacques,


Can you please send me the full script text I should be using if you can? Thanks for the help!




Yes, here is the script


-------------------

try

tell application "Finder" to set targetFolder to (target of the front Finder window) as alias

on error -- no window

set targetFolder to (choose folder)

end try


do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"


tell application "Finder" to tell targetFolder

move (files whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies"

move (files whose name extension is "cos") to folder "Captureone:Settings50"

end tell

--------------------

Mar 14, 2012 6:42 PM in response to acabaero

acabaero wrote:


User uploaded file

No it was not related to the created folders.

My syntax tell targetFolder when targetFolder is an alias doesn't work on some system.


try this


-------------------------

try

tell application "Finder" to set targetFolder to (target of the front Finder window) as string

on error -- no window

set targetFolder to (choose folder) as string

end try


do shell script "cd " & (quoted form of POSIX path of targetFolder) & "; mkdir -p Captureone/Cache/Proxies Captureone/Settings50"


tell application "Finder"

move (every file of folder targetFolder whose name extension is in {"cop", "cof"}) to folder "Captureone:Cache:Proxies" of folder targetFolder

move (every file of folder targetFolder whose name extension is "cos") to folder "Captureone:Settings50" of folder targetFolder

end tell

Oct 25, 2012 7:48 PM in response to Jacques Rioux

Hi jacques rioux


I was hoping some one can help me, i read post

"Using AppleScript to sort files Into Specified Folders based on Extension"

I have zero knoledge of apple script and i just started using automator. i found out the hard way that you can't sort in automator. and i would like some help to build a dynamic script so i can target the content and at the same time move files to folder according to the name that will end with underscore.


for example:

master folder name: 1

sub folder path: 1>2>3>4>5

insdie folder 5 theirs 3 folders: 5a, 5b, 5c


i want to name my files like so: "name_5a" "name_5b" "name_5c


i want the scrip to pick up all the files that ends with the name "_5a" and place them in folder 5a and repeat the process untill it finishes sorting all my files. what is the easy way to build this? the reason i want to build it dynamic is because i would like to re-use this script later for other folder structurs. i configured my phosotop to set layers to comps and i usually send them to the master folder so i would like the apple script to pick them up from their and sort all fo them according to the end of the naming convention, is this possible?

Oct 26, 2012 10:17 AM in response to applescript_problems

Hi,



applescript_problems wrote:


for example:

master folder name: 1

sub folder path: 1>2>3>4>5

insdie folder 5 theirs 3 folders: 5a, 5b, 5c


i want to name my files like so: "name_5a" "name_5b" "name_5c


i want the scrip to pick up all the files that ends with the name "_5a" and place them in folder 5a


I presume that the files are in the master folder not in the subFolders, that the files has an name extension.


This script take the first two characters after the last underscore.

It will move the file in the subfolder, according to the two characters found.

---------------------------

settargetFolderto (POSIX file "") asalias --<-- drop the master folder between the double quotes


tellapplication "Finder"

repeatwithtFilein (getfilesoftargetFolder)

set tName to name of tFile

setsubnametomygetChar_1_2AfterLastUnderscore(tName)

-- example : subname = "5c" --> folder "5c" of folder "5" of targetFolder

if subname is not "" then

tell folder subname of folder (text 1 of subname) of folder targetFolder

ifexiststhenmovetFiletoit -- if exists this folder then move this file

end tell

end if

endrepeat

endtell


ongetChar_1_2AfterLastUnderscore(t)

if "_" is in t then try

setoTIDtotext item delimiters

settext item delimitersto "_"

set t to text 1 thru 2 of last text item of t

settext item delimiterstooTID

return t

on error

settext item delimiterstooTID

end try

return ""

endgetChar_1_2AfterLastUnderscore

---------------------------

To get the exact path of the master folder :

Drag/drop the folder between the double quotes in the first line of the script.

Oct 29, 2012 5:34 PM in response to Jacques Rioux

This worked perfectly. after i built the file structure, but unfortunately my setup was hypothetical, my tough was to take your script and modify it to work for my set up. but after looking at it, their was some limitation. I will create a video explaining my situation in deeper detail and send you the link hopefully you can help me next time.

I apologize if you feel i wasted your time, it's my fault, i didn't fully describe in detail how i wanted to use the script. hopefully when i send you the video link it will help you with what i want to accomplish.

Oct 29, 2012 7:43 PM in response to applescript_problems

Hi,

applescript_problems wrote:


I will create a video explaining my situation in deeper detail and send you the link hopefully you can help me next time.

When this is done, start a new discussion, because it has been solved.


You'll have more luck than others helps you for the script in a new thread, as some may have other way to do this.

Using AppleScript to sort files Into Specified Folders based on Extension

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