Re: addendum to iTunes scripting
Re: addendum to iTunes scripting
- Subject: Re: addendum to iTunes scripting
- From: Michael Terry <email@hidden>
- Date: Mon, 16 Feb 2004 03:32:24 -0800
On Feb 16, 2004, at 12:45 AM, Stephen Schaefer wrote:
Sorry to not have added this in the last post, I just decided to take a
stab at it based on what I've read. Hopefully what I said in the last
post will clarify what I'm trying to do with this snippet and someone
can point out to me what I'm doing wrong (wrong terms, wrong order,
etc.). When I try to compile it I get the error "Expected expression
but found end of line." I know that sounds really simple, but I don't
know how to correct it because I'm a newbie to Applescript and
programming. here's the snippet:
tell application "iTunes"
if playlist "Last 1 day" contains track and
if playlist "Last 2 days" contains track then
set rating of track to 100
else if playlist "Last 3 days" contains track then
set rating of track to 100
else if playlist "Last 4 days" contains track then
set rating of track to 100
else if playlist "Last 5 days" contains track then
set rating of track to 100
end if
end tell
If you haven't yet, you might consider changing Mail's preferences to
'view messages with a fixed-width font' and then reopening this
message. I included a lot of inline comments which makes the script
look a little bit gibberishy. Or you could just paste it into Script
Editor where it will also be legible.
The following script gets a list of iTunes database IDs where the
members are repeated in the playlists. Working with database IDs, which
are just numbers, meant that I could check for duplicate tracks in
native AS rather than mucking around with iTunes. Usually, an
application is good for scripting very specific operations and not good
at anything else. Since it wasn't designed with any way of checking for
duplicate tracks in mind, it's not good at that. Native AS is OK,
though.
------------------------------------------------------------------------
-------
------------------------------------------------------------------------
-------
-- script to check for duplicates in specially-named playlists
------------------------------------------------------------------------
-------
------------------------------------------------------------------------
-------
------------------------------------------------------------------------
-------
-- variables to hold all the track's database ids and duplicate ids
------------------------------------------------------------------------
-------
set everyID to {}
set everyDup to {}
tell application "iTunes"
------------------------------------------------------------------------
--
-- it looks obvious what this line does--get a list of each playlist we
-- want--but you should take special note of the construct. it's
sometimes
-- called a filter clause, because it lets you get a whole list of
-- elements at one stroke based on a criterion (or criteria). after
-- getting the list of playlists, we loop through it, storing a
reference
-- to the next playlist in the variable thisPlaylist
------------------------------------------------------------------------
--
repeat with thisPlaylist in (every playlist whose name starts with
"Last")
---------------------------------------------------------------------
-- another filter clause, but with a twist; now we're not getting
-- the elements themselves, but a list of a property that each of the
-- elements has. note that properties are specified as singular in
-- whose clauses, but elements as plurl: 'tracks' or 'every track'
---------------------------------------------------------------------
set everyID to everyID & (database ID of every track of thisPlaylist)
end repeat
------------------------------------------------------------------------
--
-- count all the database IDs we got, and indicate to loop that many
times
------------------------------------------------------------------------
--
repeat (count everyID) times
----------------------------------------------------------------------
-- store the ID in a variable so its easier to refer to
----------------------------------------------------------------------
set thisID to everyID's first item
----------------------------------------------------------------------
-- 'rest' is a property of a list which means all items except the 1st
-- check two things: if the ID is a duplicate, that is, it's somewhere
-- in the rest of the list, and if we've already seen that it's a dup
----------------------------------------------------------------------
if thisID is in everyID's rest and thisID is not in everyDup then
-----------------------------------------------------------------
-- save duplicates in a list
-----------------------------------------------------------------
set everyDup's end to thisID
end if
----------------------------------------------------------------------
-- we've checked the first item of the list--now discard it
----------------------------------------------------------------------
set everyID to everyID's rest
end repeat
end tell
------------------------------------------------------------------------
-------
Now, after you have this list of IDs, you can refer to the target track
in iTunes with something like this:
tell application "iTunes"
set trackID to everyDup's first item
get first track of front library playlist whose database ID is trackID
end tell
You could loop through the database IDs and make a list of application
object references if you wanted, which are a little bit different than
referring to the object via a whose clause.
You didn't say what you wanted to do with the result, but I included a
semi-practical application below:
------------------------------------------------------------------------
-------
set popularTracks to everyDup
tell application "iTunes"
set lib to front library playlist
tell (make new user playlist with properties {name:"Popular"})
repeat with trackID in popularTracks
try
duplicate (first track of lib whose database ID is trackID) to end
end try
end repeat
end tell
(first playlist whose name is "Popular")
set view of front browser window to result
end tell
------------------------------------------------------------------------
-------
I hope there was something useful in there.
Mike
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.