Sorry for the delay, but I chose to rewrite parts of the AppleScript (and Ruby) for efficiency, and to better validate the dropped files. And last evening, storms took out my Internet. Student folders are now created in Desktop : Students, and respective, valid files are moved from the drop folder to the student folders.
I have tested it on the following filename formats with color coded result. [1,2] Green success. [3,4] Red error dialog.
- Student_report_SteveJobs.docx
- This weeks report_HavisMedwic.xls
- My daily report_for_you.xls
- Here is a report_for_you.docx
Open Automator, and from the File menu : Open Recents, and choose your folder action from yesterday. Once opened, select all of the content in the Run AppleScript action and backspace to remove. You should have a blank action. Now, copy/paste all of the following AppleScript code into that Run AppleScript action window, and click the hammer icon to compile it. Save the folder action. No need to change the name, this will just update it.
Code:
-- Folder Action to inspect dropped files against a specified filename format and then extract
-- student name from the filename to create named folders under Desktop : Students folder. Then
-- move the dropped filename into the respective students folder. Base filename must end in "_FirstLast".
-- Version: 1.5
-- VikingOSX, 2018-08-06, Apple Support Communities, No warranties or service expressed or implied.
use scripting additions
property fStudents : "Students:"
property outpath : ((path to desktop as text) & fStudents) as alias
property delim : "_"
on run {input, parameters}
repeat with anItem in input
tell application "Finder" to set workStr to name of anItem
set theArgs to workStr'squoted form & space & delim'squoted form
set studentFolder to my validate_filename(theArgs) as text
if studentFolder is "false" or studentFolder is "" then
display dialog "Invalid file format" & return & return & workStr & return & return & "Processing ends." with icon stop giving up after 15
exit repeat
end if
try
set outfolder to (outpath & studentFolder & ":") as alias
on error
set outfolder to (outpath & studentFolder & ":") as text
tell application "Finder" to makenewfolderatoutpathwith properties {name:studentFolder}
end try
tell application "Finder" to move anItem to folder outfolder
end repeat
return input
end run
on validate_filename(sargv)
return do shell script "ruby <<'EOF' - " & sargv & "
#!/usr/bin/ruby
sname, delim = ARGV
# regular expression to match against filename format
xr = /(.*?)(_[A-Z].*[A-Z].*)\\.(.*?)\\z/
valid = xr.match(sname) # no match implies nil result
puts !valid.nil? ? sname.split(/(?=[ A-Z#{delim}.])/)[-3..-2].insert(1, ' ').join : false
EOF"
end validate_filename