Get name of the opened iTunes library

I would like to get the name of the open iTunes library. I managed to do this with the following AppleScript:


tell application "iTunes"
	set selectedSongs to selection
	if selectedSongs is {} then set selectedSongs to file track 1 of file tracks of playlist 1 as list
	set firstSelectedSong to item 1 of selectedSongs
	set songlocation to location of firstSelectedSong as text
	
	
	set old_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set items_list to text items of songlocation
	set AppleScript's text item delimiters to old_delimiters
	
	set iTunesMediaPosition to item 1 of list_positions(items_list, "iTunes Media", true) of me
	set namePositon to iTunesMediaPosition - 1
	set libraryName to item namePositon of items_list
	
	
end tell

on list_positions(this_list, this_item, list_all)
	set the offsetlist to {}
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then
			set the end of the offsetlist to i
			if list_all is false then
				return item 1 of offset_list
			end if
		end if
	end repeat
	
	if list_all is false and offsetlist is {} then return 0
	return the offsetlist
end list_positions

The problem of the above script is that is depended on the name of the folder that the iTunes tracks are inside. If this folder is not “iTunes Media” the script will not work.

Any alternative solutions are welcomed! :slight_smile:

P.S. Unfortunately there is not any “Library name” property as far as I know in the iTunes AppleScript dictionary.

Hi,

try this


set libraryURL to do shell script "defaults read com.apple.iApps iTunesRecentDatabases | grep file | cut -d \\\" -f 2"
set libraryPOSIXpath to (do shell script "perl -e 'use URI::file; print URI->new(\"" & libraryURL & "\")->file;'")
set libraryAlias to POSIX file libraryPOSIXpath as alias
tell application "System Events" to set nameOfLibrary to text 1 thru -5 of (get name of libraryAlias)

Thank you very much for your immediate answer!

After a slight modification it gives a better solution than mine. Specifically, lets say that the path of the iTunes xml file is:

The result of your script is

but I want it to be

So the modified script is the following:


set libraryURL to do shell script "defaults read com.apple.iApps iTunesRecentDatabases | grep file | cut -d \\\" -f 2"
set libraryPOSIXpath to (do shell script "perl -e 'use URI::file; print URI->new(\"" & libraryURL & "\")->file;'")
set libraryPathText to libraryPOSIXpath as text

set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set items_list to text items of libraryPathText
set AppleScript's text item delimiters to old_delimiters
set libraryName to item -2 of items_list

Now the script is folder name independent.

Thanks again,
John

or this


set libraryURL to do shell script "defaults read com.apple.iApps iTunesRecentDatabases | grep file | cut -d \\\" -f 2"
set libraryPOSIXpath to (do shell script "perl -e 'use URI::file; print URI->new(\"" & libraryURL & "\")->file;'")
set libraryAlias to POSIX file libraryPOSIXpath as alias
tell application "System Events" to set nameOfLibraryFolder to (get name of container of libraryAlias)


If someone is interested in running the above AppleScript inside an Objective-C project the following code can be used:

Probably there is a better way doing this in Objective-C but my knowledge of Objective-C is quite limited.

there is one


- (NSString *)getLibraryName {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults addSuiteNamed:@"com.apple.iApps"];
    NSURL *iTunesXML = [[defaults arrayForKey:@"iTunesRecentDatabasePaths"] objectAtIndex:0];
    [defaults removeSuiteNamed:@"com.apple.iApps"];
    NSArray *pathComponents = [iTunesXML pathComponents];
    return [pathComponents objectAtIndex:[pathComponents count] -2];
}

Thank you so much! :slight_smile:

Hey guys

I’m brand new to apple script and ran into an error i can solve.

the script runs perfect but when i save it and run it as an application it comes up with the error can’t find end
can anyone see whats wrong with my script??

thanks in advance

tell application “iTunes” to play some track in playlist “temp”
tell application “iTunes”
repeat until player state is stopped
end repeat
end tell
end

tell application “iTunes”
if player state is stopped then delete tracks in playlist “temp”
end tell
end

Model: mid 2010 macbook pro 2.66 i7
AppleScript: 2.4.2
Browser: Safari 534.53.10
Operating System: Mac OS X (10.7)

Just in case there’s someone trying to solve the same issue (getting the name of the current iTunes library), here’s an alternative solution:


try
	
	set libraryFolderName to do shell script ¬
		"basename $(dirname $(defaults read com.apple.iApps iTunesRecentDatabases | grep file))"
		
	display dialog "Current iTunes library is:" & return & return & libraryFolderName buttons {"OK"} default button 1
on error theError
	display dialog "Error:" & return & return & theError buttons {"Tabarnak!"} default button 1
end try

However, note that com.apple.iApps.plist may not necessarily contain the path to the current library – see Doug Adams’ comments here

http://dougscripts.com/itunes/itinfo/multlibraryconfusion.php