displaying artist and track names

ok, I am really frustrated with this code and I need some help bad.
This is the just my code to get the track name and artist name from iTunes and display in my application.
My app is called Play MiTunes

using terms from application "iTunes"
	set playerstate to player state
	if playerstate is playing then
		set currenttrackname to name of current track
		set currenttrackartist to artist of current track
		try
			tell application "Play MiTunes"
				set contents of text field "track_text" of window "mainWindow" to currenttrackname
				set contents of text field "artist_text" of window "mainWindow" to currenttrackartist
			end tell
		end try
	end if
end using terms from

i keep getting some stupid warning that says an error of type -10660 has occured.

Hi hendo,

iTunes has got to be the easiest app to script. Fool around with it a little and you don’t need to post anything. Experiment man!

Edited: BTW, that’s how you learn to debug.

gl,

dude that’s the problem! I am not experienced enough to know what to keep trying…bit I have tried everything that I could think of…like this is what I tried next but I’m getting an NSEvaluation error…so like when I do this code it compiles fine but when the program is run I get the error…please I really need help
anywayz this is what I have now and it doesn’t work either:

tell application "iTunes"
	set playerstate to the player state
	if playerstate is playing then
		set thetitle to (name of current track)
		set theartist to (artist of current track)
		set thealbum to (album of current track)
	else if (name of current track) is equal to "" then
		set thetitle to "No song playing"
		set theartist to ""
		set thealbum to ""
	else if playerstate is stopped then
		set thetitle to (name of current track)
		set theartist to (artist of current track)
		set thealbum to (album of current track)
	end if
end tell

set the contents of text field "track_text" of window "mainWindow" to thetitle
set the contents of text field "artist_text" of window "mainWindow" to theartist

hendo13,

this one should work in my opinion - are you sure that you correctly named the text fields and the window? NSReceiverEvaluationError sounds like there might be the problem …

D.

well since my app is a iTunes remote should the code be in some sort of on clicked tag or anything like that? and yes i checked my textfileds and windows…it’s just not working which really bites…so what i put in for now is some dumb thing where you click a button and a dialog pops up with the track info but that is inconvienient and it will have to be changed…i just don’t know why it doesnt work…

EDIT: I don’t know if this makes a difference but my program has the segmented windows…like one tab has Song Search and the other has Controller…I want the track info to appear on the controller part…
if you need a general idea of what it looks like go here: http://www.macupdate.com/info.php/id/21965 and look at the screenshot. I hope someone can figure out whats wrong:/

hendo,

I’m not sure about what you want to do … you say it’s an itunes remote … so if you just want to watch what’s iTunes doing or playing then you could regularly ask iTunes for these values and set the fields everytime something has changed.
You would use an ‘on idle’ construction in this case …

Or if ‘remote’ means, that you also have buttons to control itunes (play/pause/stop/selectr track …) etc. then you can of course simply connect this script to those buttons.

btw. I’ve tried the above script and it is working (tried it with a button in an on clicked handler).
In case you want to do the same for the player states ‘playing’, ‘paused’ and ‘stopped’ you also might shortcut it to sth like:

	tell application "iTunes"
		set playerstate to the player state
		if playerstate is in {playing, paused, stopped} then
			set thetitle to (name of current track)
			set theartist to (artist of current track)
			set thealbum to (album of current track)
		else
			set thetitle to ("no song playing")
			set theartist to ("no song playing")
			set thealbum to ("no song playing")
			
		end if
	end tell
	set the contents of text field "tf1" of window "main" to thetitle
	set the contents of text field "tf2" of window "main" to theartist

here this is the whole script! take a look and see if anything is wrong

-- Play MiTunes.applescript
-- Play MiTunes
property thetitle : ""
property theartist : ""
property thealbum : ""
property the_song : "Search for your song"
on clicked theObject
	if the name of theObject is equal to "back_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				back track
			end tell
		end using terms from
	else if the name of theObject is equal to "next_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				next track
			end tell
		end using terms from
	else if the name of theObject is equal to "pp_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				playpause
			end tell
		end using terms from
	else if the name of theObject is equal to "open_search" then
		set repeat_num to 0 as integer
		repeat
			set repeat_num to repeat_num + 1
			if repeat_num is 1 then
				set dialog_text to "Which song do you want to play?"
			else
				set dialog_text to "Your search turned up 0 results. Try again?"
			end if
			repeat
				set the_dialog to (display dialog dialog_text default answer ¬
					the_song buttons {"Play", "Cancel"} default button 1)
				set the_song to the text returned of the the_dialog
				if the_song is not "" then exit repeat
			end repeat
			tell application "iTunes"
				set fi_value to fixed indexing
				set fixed indexing to true
				set the matching_tracks to (every track of library playlist 1 whose name contains the_song) as list
				
				--if there's only one match, it is played
				if the (count of matching_tracks) is 1 then
					set my_track to item 1 of matching_tracks
					exit repeat
					
					--if there are more than one matches...
				else if the (count of matching_tracks) > 1 then
					
					--creates a variable containing info about each matching track
					repeat with i from 1 to the (count of matching_tracks)
						set track_name to the name of item i of matching_tracks
						if the artist of item i of matching_tracks is not "" then
							set track_artist to the artist of item i of matching_tracks
						else
							set track_artist to "Unknown"
						end if
						if the album of item i of matching_tracks is not "" then
							set track_album to the album of item i of matching_tracks
						else
							set track_album to "Unknown"
						end if
						set track_info to i & ". " & track_artist & " - " & track_name & " (" & track_album & ")" as string
						if i is 1 then
							set matching_track_display to (track_info) as list
						else
							set matching_track_display to matching_track_display & track_info
						end if
					end repeat
					
					--allows user to choose which matching track to play
					set the_track to (choose from list matching_track_display with prompt ¬
						"Your search turned up more than 1 result. Please specify the track you want to play") as string
					if the result is "false" then
						set fixed indexing to fi_value
						return
					end if
					
					--finds which track to play
					set ascii_bounds to {48, 49, 50, 51, 52, 53, 54, 55, 56, 57} as list
					set the_number to ""
					repeat until the (ASCII number of the first character of the_track) is not in ascii_bounds
						set the_number to the_number & character 1 of the_track
						set the_track to (characters 2 thru -1 of the_track as string)
					end repeat
					set my_track to item the_number of matching_tracks
					exit repeat
				end if
			end tell
		end repeat
		tell application "iTunes"
			play my_track
			set fixed indexing to fi_value
		end tell
		tell application "Finder" to set the visible of every process whose name is "iTunes" to false
	end if
	if the name of theObject is equal to "open_prefs" then
		using terms from application "iTunes"
			tell application "iTunes"
				activate
				«event aevtpref»
			end tell
		end using terms from
	end if
end clicked
on mouse down theObject event theEvent
	if the name of theObject is equal to "rew_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				rewind
			end tell
		end using terms from
	else if the name of theObject is equal to "ff_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				fast forward
			end tell
		end using terms from
	end if
end mouse down

on mouse up theObject event theEvent
	if the name of theObject is equal to "rew_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				resume
			end tell
		end using terms from
	else if the name of theObject is equal to "ff_button" then
		using terms from application "iTunes"
			tell application "iTunes"
				resume
			end tell
		end using terms from
	end if
end mouse up

on idle theObject
	using terms from application "iTunes"
		tell application "iTunes"
			set sound_volume to sound volume
		end tell
	end using terms from
	tell application "iTunes"
		set playerstate to the player state
		if playerstate is in {playing, paused, stopped} then
			set thetitle to (name of current track)
			set theartist to (artist of current track)
			set thealbum to (album of current track)
		else
			set thetitle to ("no song playing")
			set theartist to ("no song playing")
			set thealbum to ("no song playing")
			
		end if
	end tell
	set the contents of text field "title_text" of window "mainWindow" to thetitle
	set the contents of text field "artist_text" of window "mainWindow" to theartist
	
	return 2
end idle

on should quit after last window closed theObject
	return true
end should quit after last window closed

on awake from nib theObject
	using terms from application "iTunes"
		tell application "iTunes"
			set sound_volume to sound volume
		end tell
	end using terms from
	tell application "iTunes"
		set playerstate to the player state
		if playerstate is in {playing, paused, stopped} then
			set thetitle to (name of current track)
			set theartist to (artist of current track)
			set thealbum to (album of current track)
		else
			set thetitle to ("no song playing")
			set theartist to ("no song playing")
			set thealbum to ("no song playing")
			
		end if
	end tell
	set the contents of text field "title_text" of window "mainWindow" to thetitle
	set the contents of text field "artist_text" of window "mainWindow" to theartist
	
end awake from nib

on action theObject
	if the name of theObject is equal to "vol_control" then
		set enabled of slider of window "mainWindow" to true
		set volumevalue to integer value of theObject
		using terms from application "iTunes"
			tell application "iTunes"
				set the sound volume to volumevalue
			end tell
		end using terms from
	end if
end action
--this gets the track and artist
tell application "iTunes"
	set playerstate to the player state
	if playerstate is in {playing, paused, stopped} then
		set thetitle to (name of current track)
		set theartist to (artist of current track)
		set thealbum to (album of current track)
	else
		set thetitle to ("no song playing")
		set theartist to ("no song playing")
		set thealbum to ("no song playing")
		
	end if
end tell
set the contents of text field "title_text" of window "mainWindow" to thetitle
set the contents of text field "artist_text" of window "mainWindow" to theartist

--  Created by Collin on 14/06/06.
--  Copyright 2006 Hendosoft. All rights reserved.

and another question…so what I’m trying to do is…I have two NSTexFields (2 system Font Text)…I know when I go to the applescript menu in interafce builder i am supposed to name them. I did that…should I or should I not check off the box to connect it to Play MiTunes.applescript?

I couldn’t try out everything of course - but I tried the parts responsible for setting the the text field contents (on idle and awake from nib) and it worked ok. The rest of you script looks ok for me but i haven’t tried it …

connecting a text field to your script is only necessary if you want to connect it to a handler (on end editing etc.) - it is not needed if it is a ‘passive’ GUI element - then naming it - so it can be accessed - is sufficient.

I haven’t read your whole script, but from looking at the screenshot you linked to, it seems like you are not properly referring to your text field.

If it is in a tab view, you need to refer to


text field "track_text" of tab view item "foo" of tab view "bar" of window "mainWindow"

where foo and bar are the names you have given to the Controller tab view item (i.e. the tab) and the entire tab view structure, respectively.

Your text field is inside a tab view item, which is inside the tab view, which is inside the window, so that’s how you have to refer to it