I am going through this script to get a better understanding of the nature of the script so that I could learn to make scripts myself. I saved the script you posted prior to my last response, in that script you layed out the method for selecting the file containing the new names for the "untitiled" media files and for selecting the folder containing the "untitled" meia files.
Here are the two methods you created to choose the file and folder:
-- choose the csv file
set csvFile to choose file with prompt "Select the CSV file." of type "public.text"
-- choose the folder
set workingFolder to (choose folder "Select the folder to be processed") as text
tell application "System Events"
-- list of files in alphabetical order
set fileList to (POSIX path of (files of folder workingFolder whose visible is true andname begins with "untitled"))
Here are the methods I would like to implement:
-- choose the folder
set workingFolder to (choose folder "Select the folder to be processed") as text
-- choose the csv file automatically: need to specify name extension
tell application "System Events"
set csvFile to POSIX path of first file of folder workingFolder whose name extension is"csv"
end tell
Based on my request you provided options which are outlined in your last reply; I am unsure what needs to be removed to implement the method I like, which is to choose a folder and have the script locate the csv file amongst the media files in the folder.
-- choose the csv file
--set csvFile to choose file with prompt "Select the CSV file." of type "public.text"
-- choose the folder
set workingFolder to (choose folder "Select the folder to be processed") as text
-- csv contents as list
set csvList to paragraphs of (read csvFile)
-- change this to specify the correct csv delimiter
set splitDelimiter to ","
-- change this to specify the file name join character
set recombineDelimiter to "_"
tell application "System Events"
set csvFile to (POSIX path of (first file of folder workingFolder whose name extension is "csv"))
-- list of files in alphabetical order
set fileList to (POSIX path of (files of folder workingFolder whose visible is true and name begins with "untitled"))
repeat with i from 1 to count of fileList
-- get a line from the csv list, split it at the delimiter, and recombine it
set {oldTID, my text item delimiters} to {my text item delimiters, splitDelimiter}
set newFileNamePieces to text items of (itemi of csvList)
set my text item delimiters to recombineDelimiter
set newFileName to newFileNamePieces as text
set my text item delimiters to oldTID
-- get a file from the file list and note its extension
set currentFile to file (itemi of fileList)
set fileExtension to name extension of currentFile
-- write the new name back out to the file
set name of currentFile to (newFileName & "." & fileExtension)
end repeat
end tell
I had to comment out the set csvFile because I was unsure whether it stays or goes. When I run the script as it is here i get a applescript error saying "The variable csvFile is not defined."