please help me with making a wave to flac audio watchfolder

Hi everybody,

I am completly new to applescript. Because I need a watchfolder that can automatically convert wave to flac files (audio format) and standard tools didn’t do the trick I decided to dive into the world of Applescript.

Up till now I have puzzled together a script that can convert a wave file to flac and when done it adds in the spotlight comment “opened” the second script moves the flac file to a different location. to automate it I use the lingon tool to tell launchd to watch the wave directory. when there is change is in the wave dir it will start the script. (found the tutorial on http://www.youtube.com/watch?v=1diirrvJR6I)

Uptill now i can only convert files in the top of the wave dierctory not into sub directories.

This is the situation:
In the wave directory automatically comes a new sub directory named beatles and into that subdirectory come the directory “sergeant pepper” and white album.
wave/beatles/sergeant pepper/track_a.wac etc.
wave/beatles/white album/track1.wav etc.

This is what I would like to have:
Launchd is able to see a directory change ( i assume also in a subdirectory), so when the script starts to run it encounters a file without the spotlight comment “opened” , notates what the directory structure is and checks if the output directory already exists, if not then the script should create it. that means that only for the first track from the beatles release the script creates the directory beatles/sergeant pepper into the flac output directory.
The names of the artist and albums are not known in advance. when that’s done the flac files should be renderd into that directory (flac/beatles/sergeant pepper/track_a.flac)

this is what I have done uptill now.

property folder_path : "/Users/eddee/Desktop/wav/"

on run
	tell application "Finder"
		set action_folder to ((POSIX file folder_path) as alias)
		set folder_items to every item in action_folder
	end tell
	repeat with an_item in folder_items
		tell application "Finder"
			set item_name to name of (an_item as alias)
			set the_comment to (get comment of (an_item as alias))
		end tell
		if the_comment does not contain "opened" then
			set an_item to (an_item as alias)
			set input2 to item_name
			set input to folder_path
			set input3 to "/Users/eddee/Desktop/flac/"
			set input4 to item_name
			if item_name ends with ".wav" then
				do shell script "/opt/local/bin/flac -f  -s " & input & "" & input2 & ""
				do shell script "osascript /Library/flac/copyanddelete.scpt"
				
			end if
			tell application "Finder" to set comment of an_item to (the_comment & " opened")
			
		end if
		
	end repeat

and the copy and delete script I didn’t manage to get the flac encoder point to a new directory, so a move and clean afterwards.

property folder_path : "/Users/eddee/Desktop/wav/"

on run
	tell application "Finder"
		set action_folder to ((POSIX file folder_path) as alias)
		set folder_items to every item in action_folder
	end tell
	repeat with an_item in folder_items
		tell application "Finder"
			set item_name to name of (an_item as alias)
			set the_comment to (get comment of (an_item as alias))
		end tell
		
		set an_item to (an_item as alias)
		if item_name ends with ".flac" then
			set input to folder_path
			set input3 to "/Users/eddee/Desktop/flac/"
			set input4 to item_name
			do shell script "cp " & input & "" & input4 & " " & input3 & "" & input4 & ""
			do shell script "rm -f " & input & "" & input4 & ""
		end if
	end repeat

and this is launchd xml setup

<?xml version="1.0" encoding="UTF-8"?> Disabled Label com.janpaul.epmflacconvert.folder ProgramArguments /Library/flac/wavnaarflac.app RunAtLoad ServiceDescription watch folder convert wav files to flac WatchPaths /Users/eddee/Desktop/wav

O, one more thing, I decided to read a book on applescript (AppleScript The Comprehensive Guide to Scripting and Automation on Mac OS X). After a few tutorials I got stuck the example script does not want to run on my computer (powerbook g4 10.5.8 dutch language) nor on a dualcore 10.5.8 system (english language). The message given is in dutch and tells something like finder cannot get directory applications. Is it me, the system or the book?

tell application "Finder"
	tell folder "AppleScript" of folder "Applications"
		set file_list to name of every item
		set time_folder_was_created to creation date
		set time_folder_was_modified to modification date
	end tell
end tell

Thanks for your time and I hope that you can give me some hints.
Jan-Paul

Model: powerbook g4
AppleScript: 2.1.2
Browser: Firefox 3.6
Operating System: Mac OS X (10.5)

Hi,

first of all, launchd does NOT watch subfolders in the specified folder.
For a good reason: Immagine you are going to watch your home folder, then you get notified about each change of any preference file, and this is consumes a lot of resources and CPU time.

Here’s a different approach: Put a folder containing subfolders into your watched folder.
The script searches for all files with extension .wav, does the flac encoding, creates all intermediate directories
in the flac folder and writes the output file into the proper place.

I don’t use flac, so I couldn’t test the encoding part
The script copyanddelete.scpt is probably not necessary anymore.

You can avoid checking the comment in the Finder by using an extra hot folder which moves the wav file into the wav folder and the encoded flac file into the flac folder


property parentFolder : "/Users/stklieme/Desktop/"
property wavFolder : parentFolder & "wav"
property flacFolder : parentFolder & "flac"


set flacFiles to paragraphs of (do shell script "/usr/bin/find " & quoted form of wavFolder & " -iname '*.wav'")
set {TID, text item delimiters} to {text item delimiters, "/"}
set o to (count wavFolder)

repeat with oneFile in flacFiles
	set fileAlias to POSIX file oneFile as alias
	tell application "Finder" to set the_comment to (get comment of (an_item as alias))
	if the_comment does not contain "opened" then
		set inputPath to text (o + 2) thru -1 of oneFile
		set filename to text item -1 of inputPath
		try
			set subfolders to "/" & (text items 1 thru -2 of inputPath)
		on error
			set subfolders to ""
		end try
		set outputDirectory to (flacFolder & subfolders)
		set outputPath to (outputDirectory & "/" & text 1 thru -5 of filename & ".flac")
		do shell script "/bin/mkdir -p " & quoted form of outputDirectory
		do shell script "/opt/local/bin/flac -f  -s " & quoted form of oneFile & " -o " & quoted form of outputPath
		tell application "Finder" to set comment of an_item to (the_comment & " opened")
	end if
end repeat
set text item delimiters to TID

to answer your second question: the Finder needs a reference starting at a disk.
If no disk is specified, the Finder takes the desktop folder of the current user as root directory


 tell folder "AppleScript" of folder "Applications" of startup disk