Text Field: Set Content

Hey everyone. I’m currently trying to use AppleScript to set the contents of a text field I have in a mini application I made for controlling iTunes. But I constantly get this error, and it makes no sense:

Can’t set contents of “Small System Font Text” to “hey”. (-10006)

I’ve tried everything to no avail. Here’s my code:

set playingTxt to contents of text field "playingTxt" of window "idoControl"
set contents of playingTxt to "hey"

Any help is greatly appreciated, thank you.

Model: iMac Intel 2GHz 1.5GB RAM
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Ok, I figured out what my problem is, but don’t know how to fix it.

I’m trying to edit a text field within my own app from within a tell statement referring to iTunes, which is throwing everything off. How can I identify that it’s my app that I want to change the text field of?

if the name of current track is not equal to songTxt and songTxt is not equal to "" then
	tell application "iTunes" to play track songTxt of playlist "Music"
	set trackName to the name of the current track
	set contents of text field "playingTxt"
	--this is all within a tell statement referring to iTunes
else

If you have to be inside another tell statement, then you can talk to yourself:

tell me to -- do this

tell me
	-- do these
end tell

However, text field "whatever" won’t do your application much good; It won’t know where to find that text field. You have to have a complete reference to the object; That’ll probably look like this:

text field "whatever" of window "whatever"

Hi,

If I may comment on your posts, here’s what I thought when I read them:

On your first post, you had these codes:


set playingTxt to contents of text field "playingTxt" of window "idoControl"
set contents of playingTxt to "hey"

As I look at it, playingTxt is a variable of type string or Unicode text that you assigned the content of text field “playingTxt” of window “idoControl” to. Then you treated it like a text field, setting its contents to “hey”.

You cannot do it that way since playingText in your coding is treated as a variable, not a text field. If you want to refer to the text field, you would do it like this:

set contents of text field "playingTxt" of window "idoControl" to "hey"

It should work this way.

On your second post, there is something missing in your last code. Here, I assume again that songTxt is a variable of type string or Unicode text.


if the name of current track is not equal to songTxt and songTxt is not "" then
   tell application "iTunes" to play track songTxt of playlist "Music"
   set trackName to the name of the current track
   set contents of text field "playingTxt"
   --this is all within a tell statement referring to iTunes
else

Change the last code to:


if the name of current track is not equal to songTxt and songTxt is not "" then
   tell application "iTunes"
       play track songTxt of playlist "Music"
       set trackName to the name of the current track
   end tell
   set contents of text field "playingText" of window "idoControl" to trackname
else
--add  your other codes

That is if you want the text field to show the currently playing track. Please make sure that your tell iTunes block is ended with “end tell”. Also, note that there might be a need to use the block "using terms from application iTunes"block. Please note that this is not the exact code for “using terms from” as i think iTunes must be enclosed in quotes, but you get the idea?

I hope the above help even though it would have been better to see more of your codes.

Good luck.

archseed :slight_smile:

That would and should work, but the problem is that my code extends upwards about 20 lines, all starting with a tell application “iTunes” loop. Thank you for fixing my syntax for setting the text field, that does work now. But not when inside an iTunes loop. Basically, I need a way to, for lack of a better word, excuse the iTunes loop just so I can set that text field inside my app. Would what the previous poster said work?

tell me
--set contents of text field here
end tell

Thanks again.

Hi,

I am still not so sure what to make of it without the underlying codes. But you may try something else that I just thought might help.

Can you try something like this below?

tell application "System Events" to set frontmost of process "enter here the name of your application" to true
--then enter your codes outside the iTunes block

I did something like this in the iTunes Manager application which does a lot of “excursions” between iTunes and the AS Studio application. It’s far from perfect and yes, I encountered a lot of similar problems that you have. That single line code helped a lot and I hope it helps you too.

You can also try the “tell me to activate” block. That might work too.

Good luck.

archseed :slight_smile:

Hi,

Am sorry. What I meant by “enter your codes outside the iTunes block” is really not literally outside.

If you put the application as the frontmost window via the System Events, it’s basically like you are executing them outside the iTunes block though in reality they are physically inside the iTunes tell block.

Excuse me…good luck, just the same and I hope it works for you.

archseed :slight_smile:

I would try changing your code so your not inside one big iTunes tell block the whole time (use multiple tell blocks if you have to).

Still no luck. I found another way (putting the text field changing code at the very end) but I’d rather do it this way. Here’s my code in it’s entirety:

on clicked Btn
	if the name of Btn is "playBtn" then
		set songTxt to contents of text field "songField" of window "idoControl"
		tell application "iTunes"
			set trackName to the name of the current track as string
			if player state is paused or player state is stopped then
				if songTxt is not equal to "" then
					tell application "iTunes" to play track songTxt of playlist "Music"
					set trackName to the name of the current track as string
				else
					tell application "iTunes" to play
					set trackName to the name of the current track as string
				end if
			else if player state is playing then
				if the name of current track is not equal to songTxt and songTxt is not equal to "" then
					tell application "iTunes" to play track songTxt of playlist "Music"
					set trackName to the name of the current track
					--This is the section I'm having trouble with
					tell application "System Events" to set frontmost of process "idoControl" to true
					set contents of text field "playingTxt" of window "idoControl" to trackName
				else
					tell application "iTunes" to pause
				end if
			end if
		end tell
		--set contents of text field "playingTxt" of window "idoControl" to trackName
	else if the name of Btn is "stopBtn" then
		tell application "iTunes" to stop
	end if
end clicked

Model: iMac Intel 2GHz 1.5GB RAM
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Does this work any better? (Tried to cleanup the iTunes tell blocks.)

on clicked theObject
	if theObject's name is "playBtn" then
		set songTxt to contents of text field "songField" of window "idoControl"
		
		tell application "iTunes"
			set trackName to current track's name
			set isPlaying to player state is playing
		end tell
		
		if not isPlaying then
			if songTxt is not "" then
				tell application "iTunes" to play track songTxt of playlist "Music"
			else
				tell application "iTunes" to play
			end if
			
			tell application "iTunes" to set trackName to current track's name
		else if isPlaying then
			if trackName is not songTxt and songTxt is not "" then
				tell application "iTunes"
					play track songTxt of playlist "Music"
					set trackName to the name of the current track
				end tell
				
				tell application "System Events" to set frontmost of process "idoControl" to true
				set contents of text field "playingTxt" of window "idoControl" to trackName
			else
				tell application "iTunes" to pause
			end if
		end if
		
		set contents of text field "playingTxt" of window "idoControl" to trackName
	else if theObject's name is "stopBtn" then
		tell application "iTunes" to stop
	end if
end clicked

Yeah, I could do that, but I was curious is there’s another way (without escaping the iTunes block). If there isn’t, that’s fine, I’ll work around it.

Another question: I want to update the playingTxt field as soon as the iTunes song is changed (whether or not it’s changed using my app). I’ve been searching for an appropriate event handler. I can’t find anything. I assumed there was a remote iTunes event handler (like on changed song), but apparently not. I’m currently using this (which isn’t too efficient):

on became main theWindow
	tell application "iTunes"
		set trackName to the name of the current track as string
	end tell
	set contents of text field "playingTxt" of window "idoControl" to trackName
end became main

…where I’ve enabled the “became main” event handler for my app’s main window. This works, to a degree, but not fully. Any suggestions? Thanks a lot.

Using ObjC, you would probably subcribe to a notifcation, but AppleScript doesn’t do that. Try checking the current track with an on [url=http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/sr3_app_suite/sr_app.html#//apple_ref/doc/uid/20011217-ASKApplicationSuite.Events.Idle]idle[/url] handler.

Thanks for the suggestion. That seems practical enough, except I can’t get it to work:

on idle
	tell application "iTunes"
		try
			set trackName to the name of the current track as string
		on error
			set trackName to "No song currently playing"
		end try
	end tell
	set contents of text field "playingTxt" of window "idoControl" to trackName
	return 5
end idle

I don’t get any errors, but it’s not changing the song. Is the on idle event handler only called when my entire system goes idle for a certain period of time? Thanks again.

Did you connect the handler to the application object in Interface Builder?

Aha! Under File’s Owner, right? Thanks.

Yes. :slight_smile:

One more question (I swear): how can I trigger an action on an Enter key stroke while in a text field called “songField”?

Hi,

To make your text field respond to Enter or Return presses, connect the text field to the Event Handlers in the AppleScript Pane of IB (Command-8). You can choose between the Action and Key event handlers.

If you choose the Action event handler, you will get something like this:


on action theObject
	display dialog "This is testing if text field will react"
end action

For Key event handler, you will get this instead:


on keyboard up theObject event theEvent
	display dialog "This is testing if text field will react"
end keyboard up

Pressing either Return or Enter will make the text field do something. In the above, it displays the string after “display dialog” command.

Good luck.

archseed :slight_smile: