I need some help with some applescript so that I can add it to my automator.
What I would like to do is create a script for making a new folder inside the current folder I’m working on.
I have quite a big folder called ‘Clients’, and within that folder I have sub folders containing the clients name. In each of the ‘Clients name’ folder, I have many different files. What I would like to do, is create another folder inside the ‘Clients name’ folder and organize all the files by year.
Example:
Clients–>Clients name–>2007–>somefile.jpg
or
Clients–>Clients name–>2006–>somefile.jpg
Now I think I can do most of this through Automator, although I can’t create a subfolder within that current folder.
Any help to get me started in the right direction would be appreciated. I’m new to applescript…so please go easy.
set mainFolder to (path to desktop as Unicode text) & "Clients name:"
tell application "Finder"
set listDocs to every file of folder mainFolder
repeat with F in listDocs
set F to F as alias
set y to year of (get creation date of F)
set yFolder to mainFolder & y & ":"
if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
move F to folder yFolder
end repeat
end tell
Thanks for your help. Now I’m running into another issue.
Here is how the code looks now:
set mainFolder to ("MyHardrive:Users:myname:Work:") & "Clients:"
tell application "Finder"
set listDocs to every file of folder mainFolder
repeat with F in listDocs
set F to F as alias
set y to year of (get modification date of F)
set yFolder to mainFolder & y & "2007:"
if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
move F to folder yFolder
end repeat
end tell
This error is probably happening because in some of the folders I’ve already created 2007.
My other question is this:
How does this script know if the modification date is within 2007?
The error occurs if a file (not a folder) exits in folder yFolder.
You can avoid this either with a try block (the file will not be copied) or with move F to folder yFolder replacing yes
that replaces the file (replacing ask asks whether should be replaced or not)
the result of year of (modification date of F) means exactly what’s written:
the 4-digit year number of the modification date of the file, stored in the variable y,
so you don’t need to add the year number manually.
Here is the code again with the try block mentioned above
set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
tell application "Finder"
set listDocs to every file of folder mainFolder
repeat with F in listDocs
set F to F as alias
set y to year of (get modification date of F)
set yFolder to ((mainFolder & y as Unicode text) & ":")
if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
try
move F to folder yFolder
end try
end repeat
end tell
Thanks.
I tried your script as suggested, but it doesn’t seem to do anything. I’m probably doing this wrong somehow.
In automator:
I have your script in the ‘Run Applescript’ box, and this is what it looks like.
on run {input, parameters}
set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
tell application "Finder"
set listDocs to every file of folder mainFolder
repeat with F in listDocs
set F to F as alias
set y to year of (get modification date of F)
set yFolder to ((mainFolder & y as Unicode text) & ":")
if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
try
move F to folder yFolder
end try
end repeat
end tell
return input
end run
I tried running this from the Automator program, but it’s not doing anything. Then I made this into a workflow ‘Finder-plug in’, still without it working.
This is probably a really simple problem…I’m just new.
Thanks for your patience.
Happyworker
set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
tell application "Finder"
repeat with subfolder in (get folders of folder mainFolder)
set listDocs to every file of subfolder
repeat with F in listDocs
set F to F as alias
set y to year of (get modification date of F)
set yFolder to ((mainFolder & y as Unicode text) & ":")
if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
try
move F to folder yFolder
end try
end repeat
end repeat
end tell
Would I try to change the subfolder name as described below?
set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
tell application "Finder" to repeat with -->subfolders<-- in (get folders of folder mainFolder)
tell subfolder -->and subfolders<-- to repeat with F in (get files)
set the_year to (year of (get modification date of F)) as Unicode text
if not (exists folder the_year) then make new folder at it with properties {name:the_year}
try
move F to folder the_year
end try
end repeat
end repeat
The following script does it.
It uses a recursive function to navigate thru all folders
set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
make_folder(mainFolder)
on make_folder(theFolder)
tell application "Finder" to set subfolders to (get folders of folder theFolder)
if (count subfolders) > 0 then
repeat with thesubfolder in subfolders
make_folder(thesubfolder as string)
end repeat
end if
tell application "Finder" to tell folder theFolder to repeat with F in (get files)
set the_year to (year of (get modification date of F)) as Unicode text
if not (exists folder the_year) then make new folder at it with properties {name:the_year}
try
move F to folder the_year
end try
end repeat
end make_folder