itunes force added track to be copied into itunes library

I need to script adding a file to itunes. After I add the file I want to delete it because I create the file in a temporary location. Now if the user has the preference checked to “copy the files into itunes when adding” then all is OK. The file is added to the itunes music library and I can delete the original… but if the user doesn’t have that preference checked then I can’t delete the original.

So how can I force itunes to copy the file into the music library no matter the preference setting?

EDIT:
Adding is simple but it works with that preference. You’ll note that I’m working with a pdf file.

set thePath to "/Users/hmcshane/Desktop/kMDItemContentTypes.pdf"
set macPath to POSIX file thePath

tell application "iTunes"
	add macPath
end tell

However, I’m trying the “duplicate” command and it doesn’t work.

set thePath to "/Users/hmcshane/Desktop/kMDItemContentTypes.pdf"
set macPath to POSIX file thePath

tell application "iTunes"
	duplicate macPath
end tell

You could make yourself a folder in the iTunes folder and move all files you add to that folder. Then you can add them to iTunes with knowing they will be there.

set fileToAdd to (choose file)

-- get the apps destination folder
set appDestinationFolder to do shell script "echo \"$HOME/Music/iTunes/Regulus Added Tracks\""
-- make sure it exists
do shell script "/bin/mkdir -p " & quoted form of appDestinationFolder

-- make sure filename isn't already in use
set fileNamesInUse to list folder (POSIX file appDestinationFolder)
set chosenFileName to fileNameWithoutNameExstentsion(name of (info for fileToAdd))
set fileExt to name extension of (info for fileToAdd)
if fileExt is missing value then set fileExt to ""
set newName to chosenFileName -- be sure we have a new name
set fileIndex to 2
repeat while (fileNamesInUse contains newName)
	set newName to chosenFileName & " (" & fileIndex & ")"
	if (fileExt is not "") then set newName to newName & "." & fileExt
	set fileIndex to fileIndex + 1
end repeat

-- rename the file
set newFile to stringByDeletingLastPathComponent(POSIX path of fileToAdd) & newName
do shell script "/bin/mv " & quoted form of POSIX path of fileToAdd & space & quoted form of newFile

-- move the file to the directory
do shell script "/bin/mv " & quoted form of newFile & space & quoted form of appDestinationFolder
set fileToAdd to (POSIX file newFile)

-- add to itunes
tell application "iTunes" to add fileToAdd

(* HANDLERS *)
on fileNameWithoutNameExstentsion(aFilename)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set allItems to every text item of aFilename
	if (count allItems) is 1 then
		set AppleScript's text item delimiters to tid
		return aFilename
	end if
	set toReturn to ((items 1 thru -2 of allItems) as text)
	set AppleScript's text item delimiters to tid
	return toReturn
end fileNameWithoutNameExstentsion

on stringByDeletingLastPathComponent(fileName)
	-- if slash
	if fileName is "/" then return fileName
	
	-- if ends with slash
	if fileName ends with "/" then set fileName to (characters 1 thru -2 of fileName) as string
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set allComponents to every text item of fileName
	set AppleScript's text item delimiters to ""
	
	-- if no dir
	if (count allComponents) is 1 then return ""
	
	set allComponents to (items 1 thru -2) of allComponents
	
	set AppleScript's text item delimiters to "/"
	set newPath to allComponents as text
	set AppleScript's text item delimiters to ""
	if newPath does not end with "/" then set newPath to (newPath & "/") as string
	
	set AppleScript's text item delimiters to tid
	
	return newPath
end stringByDeletingLastPathComponent

Hope it helps,
ief2

Thanks for the response. I thought of this too. I was thinking to create an application support folder and placing them in there. The problem becomes am I maintaining duplicates? If the preference is checked then the file is copied into itunes’s music folder too. So it gets messy. I really need a way to force it to copy or at least read that preference so I know ahead of time what it’s going to do then adjust accordingly.

As a last resort I just won’t worry about duplicates, but I’m hoping to avoid it if I can.

With a little bit of FileMerge magic, I got to know what data to look for in the com.apple.iTunes.plist-preference file. And surprisedly enough, it worked!!! :smiley:

property ITUNES_COPYING_WORD : 624
property ITUNES_COPYING_DISABLED : "00000100"
property ITUNES_COPYING_ENABLED : "00010100"

do shell script "/usr/bin/defaults read com.apple.iTunes pref:130:Preferences"

get word ITUNES_COPYING_WORD of result

if result is ITUNES_COPYING_DISABLED then
	display dialog "Copying disabled"
else if result is ITUNES_COPYING_ENABLED then
	display dialog "Copying enabled"
else
	display dialog "This scrip does not work properly"
end if

Tested with version 9.2.1 (5) of iTunes

Hope it helps,
ief2

EDIT: Remember that iTunes should have started up at least once for this file to be there.

Hi Hank,

the best way is to do an action depending on the preference value,
but the preference information is encapsulated in an NSData object in com.apple.iTunes.plist.

What’s about this pseudo code

[add file to iTunes] [search file in iTunes Music Folder] if [file exists] then [delete original file] else [move file to appropriate location] [set location of added file to appropriate location] end if

That’s pretty good ief2. Thanks. It worked for me too. That was a great thought about figuring it out. I looked at that preference too and figured the detail was in that data somewhere but didn’t know how to find which one.

I’m worried that word 624 might change or b different in other versions though so I’m not sure what to do about that yet. This will be used in a shipping cocoa application of mine so I probably won’t feel good about using that. I have to think about it more. See that data is probably a dictionary of values that gets archived, and dictionaries don’t have an order to them. As such that order might change as other stuff is added or removed from the dictionary.

But it’s a great feat just to figure that out so thanks. Any other ideas??? Anyone??? Hopefully there’s something more solid to rely upon.

Yep StefanK. That was my next thought. I’ll have to add the file, then search the database to find the file, then get the location of that found itunes file, and finally compare it to my location. I was hoping there was a more direct way but as you and ief2 saw, the value is encapsulated in a data variable that we don’t know how to deconstruct reliably.

OK, I figured it out and it’s pretty simple. I’ll create the file in an application support folder instead of a temporary folder. When I add the file to itunes, I get a reference back to the added file. So I just then need to get the location of that reference and compare it to my original location. This works…

set thePath to "/Users/hmcshane/Desktop/kMDItemContentTypes.pdf"
set macPath to POSIX file thePath

tell application "iTunes"
	set thePDF to add macPath
	set theLoc to location of thePDF
end tell

set posixThePDF to POSIX path of theLoc
if thePath is posixThePDF then
	return "leave original alone"
else
	return "delete original"
end if