-- rename school files use framework "Foundation" use AppleScript version "2.4" # Yosemite or later use scripting additions property NSString : a reference to current application's NSString property NSCharacterSet : a reference to current application's NSCharacterSet property NSArray : a reference to current application's NSArray property istext : {"public.text", "public.plain-text"} property adesktop : (path to desktop) as alias property delim : tab global outFolder1, outFolder2 set moveList to {} set rowItems to {} set matchList to {} set printErrors to {} set printProcessed to {} set errorList to NSArray's array()'s mutableCopy() set processedList to NSArray's array()'s mutableCopy() -- by default, the invisibles is true, and other clauses are false by default set myList to (choose file with prompt "Select file containing list:" of type istext default location adesktop without invisibles) -- select folder with files to be moved and renamed set inFolder to (choose folder with prompt "Select copy-from folder: " default location adesktop) -- collect text to use as folder names of output folders when created tell application "Finder" to set jobName to name of inFolder -- read the text file into a list of the images to duplicate -- all 23 fields in a row are string list items set moveList to (read myList as text using delimiter linefeed) -- text is utf-8 by default -- extract just the header fields set headerList to ((NSString's stringWithString:(item 1 of moveList))'s componentsSeparatedByString:delim) -- and then the rows of data set dataList to rest of moveList -- get column number representing header fields of interest -- assumption: Header fields are not already double-quoted -- we add 1 because Objective-C arrays are zero-based set firstName to (headerList's indexOfObject:"FirstName") + 1 set lastName to (headerList's indexOfObject:"LastName") + 1 set sID to (headerList's indexOfObject:"StudentID") + 1 set grade to (headerList's indexOfObject:"Grade") + 1 set fname to (headerList's indexOfObject:"FileName") + 1 -- > firstName returns 2 -- > lastName returns 3 -- > sID returns 4 -- > grade returns 6 -- > fname returns 22 -- create lists of files to move and lists to be created as text documents repeat with arow in dataList -- convert the row as text to individual list items so we can find the needed fields StudentID firstName lastName etc. set rowItems to my txt_to_list(arow, delim) -- >>>> SEE THE HANDLER BELOW >>>>>>>> -- retrieve studentID code in the row items set tempID to (item sID of rowItems) as text -- and if blank is found, then add selected fields to the printErrors list OR if not blank add selected fields to the printProcessed if tempID = "" then (errorList's addObject:((item fname of rowItems) as text)) -- create errorList with the filenames move -- and then create a new list for creating text document that contains several fields set text item delimiters to tab -- prepare to parse line using tab set theTextItems to text items of arow -- parse line into fields set AppleScript's text item delimiters to "" -- append to the lists breaking lines into items with tab delimiters set end of printErrors to item firstName of theTextItems & {tab} & item lastName of theTextItems & {tab} & item sID of theTextItems & {tab} & item grade of theTextItems & {tab} & item fname of theTextItems & {return} else (processedList's addObject:((item fname of rowItems) as text)) -- create processList with filenames to move -- and then create a new list for creating text document that contains several fields set text item delimiters to tab -- prepare to parse line using tab set theTextItems to text items of arow -- parse line into fields set AppleScript's text item delimiters to "" -- append to the lists breaking lines into items with tab delimiters set end of printProcessed to item firstName of theTextItems & {tab} & item lastName of theTextItems & {tab} & item sID of theTextItems & {tab} & item grade of theTextItems & {tab} & item fname of theTextItems & {return} end if end repeat -- convert the NSMutableArray back to an AppleScript list set errorList to errorList as list set processedList to processedList as list if (count of (errorList as list)) = 0 then display alert "No Error Items were foundâ ¦ error folder should be empty." return end if tell application "Finder" activate -- Move and rename error files set outFolder1 to (make new folder at adesktop with properties {name:jobName & "_errors"}) set matchList to (every item in folder inFolder whose name is in errorList) as alias list repeat with anItem in matchList move ((inFolder & anItem's name) as text) to folder outFolder1 end repeat set matchList to {} -- clear matchList -- Move and rename Processed files set outFolder2 to (make new folder at adesktop with properties {name:jobName & "_processed"}) set matchList to (every item in folder inFolder whose name is in processedList) as alias list repeat with anItem in matchList move ((inFolder & anItem's name) as text) to folder outFolder2 end repeat end tell -- create text document with information about files with errors WOULD LIKE TO AUTOMATICALLY NAME THE TEXT FILE with jobName_errors.txt beep set printErrors to printErrors as string set myoutputFile to open for access (choose file name with prompt "Name for errors: ") with write permission set eof of myoutputFile to 0 write printErrors to myoutputFile close access myoutputFile -- create text document with information about processed files WOULD LIKE TO AUTOMATICALLY NAME THE TEXT FILE with jobName_processes.txt beep set printProcessed to printProcessed as string set myoutputFile2 to open for access (choose file name with prompt "Name for Processed images: ") with write permission set eof of myoutputFile2 to 0 write printProcessed to myoutputFile2 close access myoutputFile2 -- >>>>>>>>>>>> HANDLER USED TO CONVERT THE DATA ROWS >>>>>>>>>>>>>>>> on txt_to_list(astr, delim) -- split the text row to its individual list items based on TSV delimiter (tab) return ((NSString's stringWithString:astr)'s componentsSeparatedByString:delim) as list end txt_to_list