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!
P.S. Unfortunately there is not any “Library name” property as far as I know in the iTunes AppleScript dictionary.
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)
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
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)
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)