osascript -e 'tell application "iTunes" to add POSIX file "/Users/xil/Psycho_Chicken.mp3"'
osascript command from Perl to add tracks. I couldn't figure out how to delete tracks by file location using Applescript, so rather than doing targeted removals I call a cut-down version of Doug's Super Remove Dead Tracks at the end.osascript within backticks causes all sorts of quoting problems when your filename contains shell metacharacters (e.g. tunes by Jane's Addiction, or song titles that contain parentheses). So, after much googling, this is the solution I came up with:
foreach my $add (@additions) {
my $applescript = <>ADD;
tell application "iTunes" to add POSIX file "$add"
ADD
system('osascript', '-e', $applescript);
}
Important bits here are a) to use a here document contained in a variable, and b) use the list form of system. The here document handles the quoting properly with no crazy regexes needed to escape special characters. (Don't forget like I did that leading whitespace will prevent the parser from finding the string terminator.) And if given a list longer than one item system doesn't hand off the arguments to the shell for interpolation -- instead, it calls the program directly, using the arguments provided. Now it works just fine for filenames containing single quotes and parentheses. Yay!>You are not logged in, either login or create an account to post comments
posted by xil at 2:16 PM on November 14, 2008