How Do I Create an Applescript that will auto-generate sequential text

Long time browser - first time poster. Apologies for the sincere noob inquiry. I have a specific application where I am batch encoding lots (and lots) of video files to be pushed to a CDN. My problem is, I have to provide an SMIL file for each and every video file that includes three (3) different encode bitrates so that the CDN can generate the m3u8 HLS. So my question is:

“How Do I Create an Applescript that will auto-generate sequential text files (UTF-8 with .smil file extension) based on two question (variable) input”

Basically I would like to have the script ask for 1) “What is the [filename]?” and 2) How many [chapX]?

Applescript would then build [chap] number of files with the naming convention [filename]-[chap01 through chapX].smil (Example if [filename]=“AAUFO” and [chapX]=18 then the Applescript would create eighteen (18) files) named as:

AAUFO-01.smil
AAUFO-02.smil

AAUFO-18.smil

[chap] would begin with 01 and always be two (2) digits. It would auto-increment the [chap] by 1 until it creates the total number of files as indicated by the input (18 in our example)

Contents of each file would be:

Hi,

try this, you will be prompted for base folder, file name prefix and number of chapters.
There is a rudimental error handler. Possible existing files will be overwritten.

I added the declaration in the smil tag. Delete it if it’s not needed


set baseFolder to choose folder
set fileNamePrefix to text returned of (display dialog "Enter file name prefix" default answer "" buttons {"Cancel", "OK"} default button "OK")
repeat
	set numberOfChapters to text returned of (display dialog "Enter number of chapters" default answer "" buttons {"Cancel", "OK"} default button "OK")
	try
		set numberOfChapters to numberOfChapters as integer
		if numberOfChapters < 100 then exit repeat
		display dialog "Please enter a value between 1 and 99"
	on error
		display dialog "string is no integer"
	end try
end repeat
repeat with i from 1 to numberOfChapters
	set numberString to text -2 thru -1 of ("0" & i)
	set fileName to fileNamePrefix & "-" & numberString & ".smil"
	set smilFilePath to baseFolder & fileName
	set smilText to "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\" xmlns:rn=\"http://features.real.com/2001/SMIL20/Extensions\">
	<head>
	</head>
	<body>
		<switch>
			<video src=" & quote & "mp4:" & fileNamePrefix & "-550-" & numberString & ".mp4" & quote & " system-bitrate=\"550000\"/>
			<video src=" & quote & "mp4:" & fileNamePrefix & "-950-" & numberString & ".mp4" & quote & " system-bitrate=\"950000\"/>
			<video src=" & quote & "mp4:" & fileNamePrefix & "-1250-" & numberString & ".mp4" & quote & " system-bitrate=\"1250000\"/>
		</switch>
	</body>
</smil>"
	try
		set fRef to open for access file smilFilePath with write permission
		set eof fRef to 0
		write smilText to fRef
		close access fRef
	on error e
		try
			close file smilFilePath
		end try
		display dialog "An error occurred: " & e
	end try
end repeat


Thank you for this StefanK!

Looking this over this gives me a brilliant start. Thank you.

When I try this in AppleScript Editor I receive the following error:

“An error occurred: Can’t make file {alias “Macintosh HD:Users:mesmer:Desktop:test:”,“DoV-01.smil”} into type <>.”

I created a folder on my Desktop called “test” for the output of the files. Then I input DoV as the first basename variable and used the integer 9 for the 9 chapters.

I receive the above error once for each time it tries to create the file for the relevant chapter (9 times in this instance.)

Could it be the version of AppleScript or a permissions problem?

Any insight you could provide would be greatly appreciated.

Thank you so much for your help and input!

sorry my fault, the result of choose folder must be coerced to text


set baseFolder to (choose folder) as text

or hard-code the folder


set baseFolder to (path to desktop as text) & "test:"

Perfect! Thank you. Makes perfect sense. Thank you so much for your contribution.

Looking this over, it becomes clear to me just how useful this is. Could be used to create batch XML files as well for example I would imagine.

What a great community.

Thank you StefanK!