Make a list in TextEdit with results

I have a script that goes and gets the name of every video in my iTunes

Everything is working fine, except I am having trouble making the results into a list in TextEdit.


property extension_list : {"mp4"}

set targFolder to "Macintosh HD:Users:username:Music:iTunes:Test:" as alias -- test folder has a mp3, a mp4 and a folder, which also has 1 of each for test purposes

tell application "Finder" to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list
set listVids to text items of vidList

set Filename to "TestFile" --Name the file 

set this_data to listVids as string

set target_file to "Macintosh HD:Users:username:Desktop:" & Filename

set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file

try --This part writes the file
	set the target_file to the target_file as text
	set the open_target_file to open for access file target_file with write permission
	if append_data is false then set eof of the open_target_file to 0
	write this_data to the open_target_file starting at eof
	close access the open_target_file
on error
	try
		close access file target_file
	end try
end try


This makes my TextEdit document read

Video 1.mp4Video2.mp4

What I am trying to get is

Video 1.mp4
Video 2.mp4

I can get each item, so I tried


property extension_list : {"mp4"}

set targFolder to "Macintosh HD:Users:username:Music:iTunes:Test:" as alias -- test folder has a mp3, a mp4 and a folder, which also has 1 of each for test purposes

tell application "Finder" to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list
set listVids to text items of vidList

set Filename to "Video List" --Name the file 

--set this_data to listVids as string

set target_file to "Macintosh HD:Users:username:Desktop:" & Filename

set append_data to false --Set this to false if you want to overwrite, true if you want to add text to an existing file

try --This part writes the file
	set the target_file to the target_file as text
	set the open_target_file to open for access file target_file with write permission
	if append_data is false then set eof of the open_target_file to 0
	repeat with i from 1 to number of items in vidList --New
		set theItem to (item i of vidList) --New
		--write this_data to the open_target_file starting at eof 
		write theItem to the open_target_file starting at eof --New
	end repeat --New
	close access the open_target_file
on error
	try
		close access file target_file
	end try
end try

But, I don’t get any results.

Where I could get each item I do not think my problem is TID’s (which I don’t fully understand), But I could be, and often am, wrong.

Thanks for the help,

higashijoe

Your basic problem is with applescript’s text item delimiters (TIDs). It happens when you coerce the list of the song names into a string. By default TIDs are “” (i.e. nothing), so when you coerce to a string it puts “nothing” between each song name. But you don’t want nothing, you want a return character so that each name is on its own line rather than next to each other… therefore you need to change TIDs to return, then coerce to a string. Of course you always nee to change the TIDs back when your finished so here’s your script modified with this change.

Note also that I removed the line “set listVids to text items of vidList” because I don’t see where it does anything. Also, I cleaned up the order of your code… in general it’s good to put all of your defined variables at the top of your script so that you can quickly see them all together and you know what variables you can change if you need to.

Anyway, I hope it works and good luck.

set extension_list to {"mp4"}
set Filename to "TestFile" --Name the file
set target_file to "Macintosh HD:Users:username:Desktop:" & Filename
set targFolder to "Macintosh HD:Users:username:Music:iTunes:Test:" as alias -- test folder has a mp3, a mp4 and a folder, which also has 1 of each for test purposes
set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file


tell application "Finder" to set vidList to get (name of files of (entire contents of (targFolder))) whose name extension is in the extension_list

set {TIDs, text item delimiters} to {text item delimiters, return}
set this_data to vidList as string
set text item delimiters to TIDs

try --This part writes the file
	set the target_file to the target_file as text
	set the open_target_file to open for access file target_file with write permission
	if append_data is false then set eof of the open_target_file to 0
	write this_data to the open_target_file starting at eof
	close access the open_target_file
on error
	try
		close access file target_file
	end try
end try

Hi,

remember an old fashioned type writing machine, if you don’t press carriage return,
the text will continously written on the same line :wink:
You can do this with text item delimiters

.
-- set listVids to text items of vidList not needed
set Filename to "TestFile" --Name the file 
set {TID, text item delimiters} to {text item delimiters, return}
set this_data to vidList as string
set text item delimiters to TID
set target_file to "Macintosh HD:Users:username:Desktop:" & Filename
.

By the way, here’s a good tutorial on TIDs…
http://macscripter.net/articles/402_0_10_0_C/

Thanks Guys!

The line you both removed, I missed removing for the post.
I had used it in one of the many ways I tried to get this to work on my own.

As for TID’s, I am starting to understand them, However in this script, where I could get the first or second item, Why could I not get the first put it in the doc, then get the second ect.

I have no doubt that your way is the correct and faster way, I’m just trying to understand where my thinking was off.

Thanks again!

higashijoe

because append_data is false, therefore eof is 0 which means that every time you write to the file it overwrites everything you have written before.

then you have to add the carriage return in the set theItem line

try --This part writes the file
	-- 	set the target_file to the target_file as text -- not needed
	set the open_target_file to open for access file target_file with write permission
	if append_data is false then set eof of the open_target_file to 0
	repeat with i from 1 to number of items in vidList --New
		set theItem to (item i of vidList & return) --New
		write theItem to the open_target_file starting at eof --New
	end repeat --New
	close access the open_target_file
on error
	try
		close access file target_file
	end try
end try

PS: all thes are only “grammar cosmetics”, they don’t have any function