Script to list files on CD or DVD

Okay first this is very crude and is really my first Applescript. I patched a lot from what I learned from this and other sites. I had just made a bunch of backup DVD’s and I wanted a way to “index” them. I thought, hey this new spotlight would work great if I had a text file of these 14 DVD’s I just made.

Anyway after several version’s here’s the one I used.

set d_loop to false
set text1 to "Hello, This Applescript will create"
set text2 to "a 'searchable' text file with the"
set text3 to " complete list of files from any CD"
set text4 to "or DVD. It will not work on folders."
set text5 to "Would you like to record a Disc?"
repeat while not d_loop
	if button returned of (display dialog ((text1 & return & text2 & return & text3 & return & text4 & return & text5) as string) buttons {"No, not now", "OK"} default button 2 with icon note giving up after 600) = "OK" then
		set text1 to "Okay, that worked..."
		set text2 to "Let's try another..."
		set text3 to "Would you like to"
		set text4 to "record another Disc?"
		
		tell application "Finder"
			activate
			set t_date to "Created On: "
			set t_date to t_date & (current date)
			set the chosen_folder to name of (choose folder with prompt "Pick the DISC containing the files to record:")
			
			tell application "Terminal"
				activate
				do script "Echo 'Disc name: '" & chosen_folder & " >~/Documents/OffLine/" & chosen_folder & ".txt"
				do script "Echo " & t_date & " >>~/Documents/OffLine/" & chosen_folder & ".txt"
				do script "ls -R -C /Volumes/" & chosen_folder & " >>~/Documents/OffLine/" & chosen_folder & ".txt"
				
				delay 6
				close (windows whose name begins with "Bash")
				close (windows whose name begins with "Terminal")
				
			end tell
		end tell
		
	else
		tell application "Terminal"
			quit
		end tell
		set d_loop to true
	end if
end repeat

I want to modify it to use a variable for the location of the text file, instead of hard coding it to the Offline folder. About the only problem was that after it closed the terminal windows it did not return focus to the Applscript dialog box.

I picked a couple of odd files from a few of the DVD’s and sure enough when I search with Spotlight almost always the top hit is my DVD.txt file.

Hopefully it will help someone and those that are much smarter than me will be able to tweak it.
:cool:

Model: 12" PowerBook
Browser: Safari 412
Operating System: Mac OS X (10.4)

You can use this for the user-selected folder:

set userSelectedFolder to choose folder with prompt "choose output location..."
do shell script "cd " & quoted form of posix path of userSelectedFolder

(this is only an example, adjust as needed using the statement “quoted form of posix path of”)
To return the focus to your application, you can:

tell me to activate
display dialog "foo"

Also, you can use the command “do shell script” to execute your shell-script commands, instead of using the Terminal (this way, you don’t launch and rely on a third-party application, don’t loss focus, etc.), as shown in the sample above.

:smiley:
Thanks jj - I tried to use the do shell script at first but couldn’t get it to work. This time it worked fine. Here’s the much improved script, with comments. I’ve also revised it to work with either a DISC or FOLDER.

--Set loop variable
set d_loop to false
--Create text for dialog box
set text1 to "Hello, this Applescript will create"
set text2 to "a 'searchable' text file with the"
set text3 to " complete list of files from any CD"
set text4 to "or DVD. It will not work on folders."
set text5 to "Would you like to record a Disc?"
--Have user select output folder
set userSelectedFolder to choose folder with prompt "Choose output location..."
--Begin loop
repeat while not d_loop
	--Ask user to create file or not
	if button returned of (display dialog ((text1 & return & text2 & return & text3 & return & text4 & return & text5) as string) buttons {"No, not now", "OK"} default button 2 with icon caution giving up after 600) = "OK" then
		--Set dialog box to different text after first run
		set text1 to "Okay, that worked..."
		set text2 to "Let's try another..."
		set text3 to "Would you like to"
		set text4 to "record another Disc?"
		
		tell application "Finder"
			activate
			--Creates the "created on" line of the text file
			set t_date to "Created On: "
			set t_date to t_date & (current date)
			--Have user select the disc or folder
			set full_folder to choose folder with prompt "Please select the Disc or Folder"
			set chosen_folder to name of (full_folder)
			--Adds quotes to name of disc in case name has spaces
			set quoted_folder to "'" & chosen_folder & "'"
			--Writes first line "Disc name: <name of disc>" of 'new' file
			do shell script "Echo 'Folder/Disc name: '" & quoted_folder & " > " & quoted form of POSIX path of userSelectedFolder & quoted_folder & ".txt"
			--Writes second line "Created on: <today>" to same file
			do shell script "Echo " & t_date & " >> " & quoted form of POSIX path of userSelectedFolder & quoted_folder & ".txt"
			--Write out multi-column folder / file list to same file
			do shell script "ls -R -C " & quoted form of POSIX path of full_folder & " >> " & quoted form of POSIX path of userSelectedFolder & quoted_folder & ".txt"
		end tell
		tell me to activate
		
	else
		set d_loop to true
	end if
end repeat

Thanks again!

Model: 12" PowerBook
Browser: Safari 412
Operating System: Mac OS X (10.4)

CyberGreg, I tried to clean up the code a little:

set loop to false
set dialogText to "Hello, this Applescript will create a 'searchable' text file with the complete list of files from any CD/DVD/folder. Would you like to record a list?"

choose folder with prompt "Choose output location:"
set outputFolder to POSIX path of result

repeat while loop is false
	display dialog dialogText buttons {"No, not now", "OK"} default button 2 giving up after 600
	if button returned of result = "OK" then
		set dialogText to "Okay, that worked." & return & ¬
			"Would you like to record another list?"
		
		choose folder with prompt "Please select the disc or folder:"
		set sourceFolder to POSIX path of result
		set sourceName to do shell script "basename " & ¬
			quoted form of sourceFolder
		set outputFile to quoted form of (outputFolder & sourceName & ".txt")
		set header to "Folder/Disc name: " & sourceName & return & ¬
			"Created On: " & (current date) & return & return & ¬
			sourceFolder & ":"
		
		do shell script "echo " & quoted form of header & " > " & outputFile
		do shell script "ls -RC " & quoted form of sourceFolder & ¬
			" >> " & outputFile
	else
		set loop to true
	end if
end repeat

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

WOW!

guardian34 - that’s a thing of beauty… I just tested it and it works like a dream. Thanks… It will take me awhile to digest your changes, again thanks.

I’m really learning a LOT!

Edit: Let me rephrase that… I have a LOT to learn! I just man’d ‘baseline’, sweet. I need to buy a unix book I bet there are tons of powerful unix commands.

:cool:

Model: 12" PowerBook
Browser: Safari 412
Operating System: Mac OS X (10.4)

Okay, last post on this topic (I hope…). I revised the script to be more like a mini-app instead of something that I’d have to remember what it does. I think the “Eject CD” is a cool touch. Suited my usage perfectly. Many thanks to the very helpful and knowledgeable people on this forum.

set loop to false
set dialogText to "Hello, this Applescript will create a 'searchable' text file with the complete list of files from any CD/DVD/folder. Would you like to continue?"
display dialog dialogText buttons {"OK", "Cancel"} default button 2 giving up after 600
if button returned of result = "OK" then
	display dialog "Okay, first we need to select the folder to put the text files in." buttons {"OK"} default button 1 giving up after 120
	choose folder with prompt "Choose output location:"
	set outputFolder to POSIX path of result
	set dialogText to "Now please select the Folder or Disc you'd like to index"
else
	set loop to true
end if
set sourceFolder to ""
repeat while loop is false
	display dialog dialogText buttons {"Eject CD", "OK", "Cancel"} default button 3 giving up after 180
	if button returned of result = "Eject CD" then
		if sourceFolder = "" then
			choose folder with prompt "Please select the disc to EJECT"
			set sourceFolder to POSIX path of result
			set sourceName to do shell script "basename " & ¬
				quoted form of sourceFolder
		end if
		try
			tell application "Finder"
				eject disk sourceName
			end tell
		on error
			display dialog "I don't know the name of the disc. Could not eject CD!" buttons {"Sorry"} default button 1 giving up after 60
		end try
	else
		if button returned of result = "OK" then
			choose folder with prompt "Please select the disc or folder:"
			set sourceFolder to POSIX path of result
			set sourceName to do shell script "basename " & ¬
				quoted form of sourceFolder
			set outputFile to quoted form of (outputFolder & sourceName & ".txt")
			set header to "Folder/Disc name: " & sourceName & return & ¬
				"Created On: " & (current date) & return & return & ¬
				sourceFolder & ":"
			
			do shell script "echo " & quoted form of header & " > " & outputFile
			do shell script "ls -RC " & quoted form of sourceFolder & ¬
				" >> " & outputFile
		else
			set loop to true
		end if
	end if
end repeat

Model: 12" PowerBook
Browser: Safari 412
Operating System: Mac OS X (10.4)