Renaming Files and Read TextFile

Hello,

I want to automatically rename files into a specific folder. I am capturing video files and I want to rename them using a script. I am building the string for each file like name of the series, season and I want to use a text file for the titles.

the text file should look like this with the titles of each episode using enter als the delimiter:

Pilot
Landing
Stop

I already created the script and it renames the files but just with values from an input form or static. So how can I read in a loop one line, put it into the string and then going to the next file.

This is my code at the moment.



set episodefolder to choose folder

display dialog "Name of the Series" default answer ""
set seriesname to text returned of result


display dialog "Which Season" default answer ""
set Season to text returned of result


tell application "Finder"
	
	set numbering to the count of document files of the folder episodefolder
	
	set filename to every document file of the folder episodefolder
	
	repeat with numberid from 1 to numbering
		
		set currentfile to item numberid of filename
		
		if numberid ≤ 9 then
			
			set the name of currentfile to (seriesname & "-" & "S0" & Season & "E0" & (numberid as string) & "." & name extension of currentfile)
			
		else
			
			set the name of currentfile to (seriesname & "-" & "S0" & Season & "E" & (numberid as string) & "." & name extension of currentfile)
			
		end if
		
	end repeat
	
	
end tell


Hi,

welcome to MacScripter.

How do you expect the exact syntax of a final string including the title?

This is a (untested version) to put the title at the end


set episodefolder to choose folder
set episodeTitlesFile to choose file with prompt "Select text file containing the Episode Titles"
set seriesname to text returned of (display dialog "Name of the Series" default answer "")
set seasonNumber to (text returned of (display dialog "Which Season" default answer "") as integer)
set episodeTitles to paragraphs of (read episodeTitlesFile)

tell application "Finder" to set videoFiles to every document file of episodefolder
if (count videoFiles) = (count episodeTitles) then
	repeat with numberid from 1 to (count videoFiles)
		set currentTitle to item numberid of episodeTitles
		set currentfile to item numberid of videoFiles
		set newFileName to (seriesname & "-" & pad("S", seasonNumber) & pad("E", numberid) & "-" & currentTitle & "." & name extension of currentfile)
		tell application "Finder" to set the name of currentfile to newFileName
	end repeat
else
	display dialog "the number of episode does not match the number of episode titles" buttons {"Cancel"} default button "Cancel"
end if

on pad(prefix, v)
	return prefix & (text -2 thru -1 of ("00" & v))
end pad