Need a script: "Last Listened Date" to "Text box"

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
At its simplest, this one-line modification will get you what you want...changing the dialog message to reflect Last Played instead of Date Created is left as an exercise for the reader ;-)

Open the the "File Creation Date to Comment" script in Script editor and locate this line:
set newC to my get_info_of(location)
and make it look like this:
--set newC to my get_info_of(location)

Then, right below it, add this line:
set newC to played date as string
and save the script; that's it.
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Wow!! It works perfectly...
But, but, it saves data in the space of "Comments", and i want it to save into "Lyrics" space, so that results can be seen on Ipod too..
What do I have to edit?
Thanks..
 
Joined
Nov 15, 2004
Messages
13,228
Points
36
Age
51
Location
Toronto, Canada
Website
www.ilounge.com
Simply change all the references to "comment" to "lyrics" for example:

Code:
if opt is item 1 of myOpts then
	--"Replace current track Comments"
	set lyrics to newC
	else if opt is item 2 of myOpts then
		--"Append before track Comments"
		if lyrics is not "" then
			set lyrics to newC & separator & lyrics
		else
			set lyrics to newC
		end if
	else
	--"Append after track Comments"
		if lyrics is not "" then
			set lyrics to lyrics & separator & newC
		else
			set lyrics to newC
		end if
	end if
Simply doing a search-and-replace will probably suffice.
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Ta-dah:
http://depositfiles.com/files/5932048
Fantastic. Thanks.
But I wonder if this script can run on Ipod too.
I explain: with this script Itunes adds data to every song. But is it possible to make Ipod record the date on a song? (That is: when I finish to listen to a song, Ipod automatically copies the date of the last listening in "Lyrics", so without Itunes..)
I know tahat it's almost impossible, but..
Thanks.
 
Last edited:

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
It's completely impossible -- the iPod doesn't understand Applescript, at all.

Sorry about not understanding you wanted the last played date to go into Lyrics; glad to see you made the changes easily.

FWIW, here's a version that trims some of the excess (unneeded) code, and adds a little error-checking to cover songs that have never been played --
Code:
-- you can change this
property separator : "

"

-- don't modify below unless you know what you are doing : )
property myOpts : {"Replace the current Text", "Append before the Text", "Append after the Text"}

tell application "iTunes"
	set p to view of front window
	if selection is not {} then
		set sel to selection
		set ptext to "of the selected tracks"
	else
		set sel to every track of p
		set ptext to "track of the selected playlist"
	end if
	
	display dialog "Last Played" & return & return & ¬
		"Copies the Last Played from the file of each " & ptext & " to the track's Lyrics tag." buttons {"Cancel", "Continue..."} default button 2
	
	set m to "Click on an option:"
	set opt to (choose from list myOpts with prompt m without multiple selections allowed) as string
	if opt is "false" then error number -128
	
	set ofx to fixed indexing
	set fixed indexing to true
	repeat with t in sel
		if class of t is file track then
			tell t
				try
					with timeout of 3000 seconds
						set newCdate to played date
						if newCdate is missing value then
							set newC to "*Never Played*"
						else
							set newC to newCdate as string
						end if
						
						if ((lyrics is "") or (opt is item 1 of myOpts)) then
							--"Replace the current Text"
							set lyrics to newC
						else if opt is item 2 of myOpts then
							--"Append before the Text"
							set lyrics to newC & separator & lyrics
						else
							--"Append after the Text"
							set lyrics to lyrics & separator & newC
						end if
						-- to lyrics
					end timeout
				end try
			end tell
		end if
	end repeat
	set fixed indexing to ofx
	if frontmost is true then display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1
end tell
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Ahhhhh.. I realized that there is a small trobule: you suppose I have a song with a text in "Lyrics".
Now, when I open the script, it adds the date before or after the text.
That's all very well, but then, when I apply the script for the second time, it adds another date to "Lyrics", and, in the long run, an endless list will be created.
Naturally I could select "replace", but in this way, I delete the text of the song too.

So I need something that recognizes the date and deletes only it, saving the text of the song. For example we could put the condition of rewriting just the line beginning with "Friday, or Saturday, or Sunday, or Monday, or..."

What do I have to do? (Maybe I didn't make myself clear..)
Thanks.:)
 
Last edited:

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
LOL -- it's not that you didn't make yourself clear. It's more like I'm just an Applescript amateur at best, so all I try to do is point in the right direction, to give folks a better shot at doing it themselves. Besides, I'm busy enough as it is ;-) Nonetheless....

Given Applescript's poor text-handling skills, it's easier coding to just put the played date in the same spot in the lyrics every time; I picked the top ;-) There's any number of ways to figure out what's lyrics and what's an existing last played date; the trick is get as few "false positives" as possible. This is pretty simple:
get first line of lyrics -> convert to date object -> convert to string -> compare string to first line of lyrics
If they match, the script assumes that it "owns" that line, and replaces it with the current last played date; if they don't match, the script prepends the current last played date and the separator to the lyrics.

Seems to work fine on Tiger using US English; not sure how it will do with another language. Looks like this:
Code:
-- you can change these
property separator : "

"
property unplayedStr : "*Unplayed*"

tell application "iTunes"
	set p to view of front window
	if selection is not {} then
		set sel to selection
		set ptext to "of the selected tracks"
	else
		set sel to every track of p
		set ptext to "track of the selected playlist"
	end if
	
	display dialog "Last Played" & return & return & ¬
		"Copies the Last Played Date from each " & ptext & " to the track's Lyrics tag." buttons {"Cancel", "Continue..."} default button 2
	
	set ofx to fixed indexing
	set fixed indexing to true
	repeat with t in sel
		if class of t is file track then
			tell t
				try
					with timeout of 3000 seconds
						set newCdate to played date
						if newCdate is missing value then
							set newC to unplayedStr
						else
							set newC to newCdate as string
						end if
						
						set the_lyrics to lyrics
						if ((the_lyrics is "") or (the_lyrics is unplayedStr)) then
							set lyrics to newC
						else
							set first_line_of_lyrics to first paragraph of the_lyrics
							set date_test_str to ""
							try
								set date_test_str to my dateTest(first_line_of_lyrics)
							end try
							if date_test_str = first_line_of_lyrics then
								set the_lyrics to characters ((count of characters of first_line_of_lyrics) + 1) thru end of the_lyrics as string
							else
								set the_lyrics to separator & the_lyrics
							end if
							set lyrics to newC & the_lyrics
						end if
					end timeout
				end try
			end tell
		end if
	end repeat
	set fixed indexing to ofx
	if frontmost is true then display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1
end tell

on dateTest(str)
	set test_str to ""
	tell me
		set test_str to date str
	end tell
	return test_str as string
end dateTest
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Unluckily it doesn't work in the Italian version (it adds the first date, but then doesn't replace it...).
However, if you are too busy, I will try to solve this probleme alone.
Thank you for your help.:)
 
Last edited:

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
Maybe it's some "personalization" settings in International or Date & Time settings... when I use "default" settings it seems to work OK. For example, the date for a just-played tune is: Domenica, 15 giugno 2008 15:50:23 which Applescript had no problem re/converting. What does a date from your machine look like?
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
In this version the script simply looks for "Last Played: " as the first characters of the Lyrics field.
Code:
-- you can change these
property separator : "

"
property unplayedStr : "*Unplayed*"

tell application "iTunes"
	set p to view of front window
	if selection is not {} then
		set sel to selection
		set ptext to "of the selected tracks"
	else
		set sel to every track of p
		set ptext to "track of the selected playlist"
	end if
	
	display dialog "Last Played" & return & return & ¬
		"Copies the Last Played Date from each " & ptext & " to the track's Lyrics tag." buttons {"Cancel", "Continue..."} default button 2
	
	set ofx to fixed indexing
	set fixed indexing to true
	repeat with t in sel
		if class of t is file track then
			tell t
				try
					with timeout of 3000 seconds
						set newCdate to played date
						if newCdate is missing value then
							set newC to unplayedStr
						else
							set newC to newCdate as string
						end if
						
						set the_lyrics to lyrics
						if the_lyrics is not "" then
							set first_line_of_lyrics to first paragraph of the_lyrics
							if first_line_of_lyrics begins with "Last Played: " then
								set the_lyrics to characters ((count of characters of first_line_of_lyrics) + 1) thru end of the_lyrics as string
							else
								set the_lyrics to separator & the_lyrics
							end if
						end if
						
						set lyrics to "Last Played: " & newC & the_lyrics
					end timeout
				end try
			end tell
		end if
	end repeat
	set fixed indexing to ofx
	if frontmost is true then display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1
end tell
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Unluckily the problem is not the setting of the date...
I've created a video to explain better..
http://it.youtube.com/watch?v=4EeLAxpBz30
I present three cases:

1 the space "Lyrics" is blank....I open the script.....It adds the date..OK

2 the space "Lyrics" has only a date, with no text.....I open the script....It doesn't refresh the old date!!!

3 the space "Lyrics" has a date and a text...I open the script...All works perfectly: the date is changed and the text is saved.
 
Last edited:

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
Probably the trouble is in this line:

"set the_lyrics to characters ((count of characters of first_line_of_lyrics) + 1) thru end of the_lyrics as string"

(even if it seems to be ok)
In fact, there was the same fragment in the previous script, which presented the same problem...
 

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,876
Points
0
Location
About 3 feet in front of the monitor
OK, let's give it one more try ;-)
Code:
-- you can change these:
property separator : "

"
property unplayedStr : "*Unplayed*"
property lastPlayedStr : "Last Played:"

tell application "iTunes"
	set p to view of front window
	if selection is not {} then
		set sel to selection
		set ptext to "of the selected tracks"
	else
		set sel to every track of p
		set ptext to "track of the selected playlist"
	end if
	
	display dialog "Last Played" & return & return & ¬
		"Copies the Last Played Date from each " & ptext & " to the track's Lyrics tag." buttons {"Cancel", "Continue..."} default button 2
	
	set ofx to fixed indexing
	set fixed indexing to true
	repeat with t in sel
		set lyrics_list to {}
		set str to ""
		set the_lyrics to ""
		if class of t is file track then
			tell t
				try
					with timeout of 3000 seconds
						set newCdate to played date
						if newCdate is missing value then
							set newC to unplayedStr
						else
							set newC to newCdate as string
						end if
						set str to lastPlayedStr & space & newC
						
						set the_lyrics to lyrics
						if the_lyrics is not "" then
							set lyrics_list to paragraphs of the_lyrics
							if item 1 of lyrics_list starts with lastPlayedStr then
								set item 1 of lyrics_list to ""
							else
								set str to str & separator
							end if
							tell me
								set {delims, text item delimiters} to {text item delimiters, return}
								set the_lyrics to lyrics_list as text
								set text item delimiters to delims
							end tell
						end if
						set lyrics to str & the_lyrics
					end timeout
				end try
			end tell
		end if
	end repeat
	set fixed indexing to ofx
	if frontmost is true then display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1
end tell
 

Ortensio

New member
Joined
Jun 10, 2008
Messages
21
Points
0
It's..It's..It's..perfect. Fantastic, fantabulous, a dream becomes true. I haven't got enough words to thank you....Thanks:)
 
Top