/* This rough and ready groovy script simply imports m4a files found in a source directory into iTunes in date/time order. The files are imported one at a time to ensure they appear in the correct order within iTunes. Written in 2007 by Maurice Walton, www.mauricewalton.com You are free to use this code for any legal purpose, but please credit me and provide a reference to www.mauricewalton.com Requires java, groovy, iTunes & Windows, plus: Groovy COM Scripting support: http://groovy.codehaus.org/COM+Scripting */ import java.io.* import java.util.* import java.text.* import org.codehaus.groovy.scriptom.ActiveXProxy /* * Compare one file date to another, used for sorting */ class FileDateComparator implements Comparator { public int compare(Object o1, Object o2) { int returnVal if (((File) o1).lastModified() > ((File) o2).lastModified()) { returnVal = +1 } else if (((File) o1).lastModified() < ((File) o2).lastModified()) { returnVal = -1 } else { returnVal = 0 } return returnVal } } /* * grab all the m4a files from the source directory */ Object getFiles() { List files = new ArrayList() File sourceDirectory = new File(sourceDirectoryName) for(File file : sourceDirectory.listFiles()) { if (file.isFile() && file.canRead() && file.getName().endsWith("m4a")) files.add(file) } // sort file list into ascending date order so they will also be imported into iTunes in ascending date order Collections.sort(files, new FileDateComparator()) return files } void getUserInput() { println("Press Enter to continue") BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)) try { bufferedReader.readLine() } catch (IOException e) { } } /* Add each file to iTunes */ void doImport() { sourceFiles = getFiles() // process one file at a time as it doesn't really cost much extra time and means that any problem in the chain of events will not effect all files for(sourceFile in sourceFiles) { List arguments = new ArrayList() arguments.add("cmd") arguments.add("/c") arguments.add("addFileToiTunes.bat") arguments.add("\"" + sourceFile.getAbsolutePath() + "\"") synchronousExecute(arguments) println("Add file to iTunes done") // finished with the output file, so remove it println ("removing " + sourceFile.getAbsolutePath()) sourceFile.delete() } } /* Call an external program, waiting for it to complete */ void synchronousExecute(arguments) { ProcessBuilder processBuilder = new ProcessBuilder(arguments) processBuilder.redirectErrorStream(true) try { Process process = processBuilder.start() InputStream inputStream = process.getInputStream() InputStreamReader inputStreamReader = new InputStreamReader(inputStream) BufferedReader bufferedReader = new BufferedReader(inputStreamReader) // this code also prevents the script from continuing on without waiting for the program called to finish String line while ((line = bufferedReader.readLine()) != null) { println(line) } } catch (IOException ioe) { ioe.printStackTrace() } } /* Check the arguments */ if (args.length < 1) { if (args.length > 0) { for (arg in args) print arg + " " println } else println "No parameters supplied" println "Usage : Mything " println "Example: Mything L:\\MyVideos" getUserInput() System.exit(1) } File directory; // check source directory is readable sourceDirectoryName = args[0] directory = new File(sourceDirectoryName) if (!(directory.exists() && directory.canRead() && directory.isDirectory())) { println "Cannot read from source directory: " + sourceDirectoryName getUserInput() System.exit(2) } // check can create iTunes proxy iTunesProxy = new ActiveXProxy("iTunes.Application") if (iTunesProxy == null) { println "Can't create iTunes connection" getUserInput() System.exit(5) } iTunesProxy.release() iTunesProxy = null doImport() getUserInput() /* @rem @rem addFileToiTunes.bat @rem start /wait addFileToiTunes.js %1 @echo Done */ /* * addFileToiTunes.js * Copies the file specified to iTunes and waits for iTunes to complete */ /* arguments = WScript.Arguments; if (arguments.length != 1) { WScript.Echo("Usage: addFileToiTunes.js "); } else { var fileSystemObject = new ActiveXObject("Scripting.FileSystemObject"); var fileName = fileSystemObject.GetAbsolutePathName(arguments(0)); var iTunesApplication = WScript.CreateObject("iTunes.Application"); var mainLibrary = iTunesApplication.LibraryPlaylist; var status = mainLibrary.AddFile(fileName); while (status != null && status.InProgress) { WScript.Sleep(1000); } } */