setting arrow keys as the key equivilant in IB

How do you set the arrow keys as a key equivilant in Interface Builder?

EDIT: might as well not make a new topic so I’m gonna ask another question here, so I in my iTunes quick song player all it is, is a text field to enter the song, artist or album. but I also have a pop-up menu with the choices of Title Search, Artist Search and Album Search. i want it so if the current item in the pop-up menu is title seach, when you hit enter it searches/plays that song, and the same for artist and album. I already have the script to play the songs, I just don’t know how to work with pop-up menus.

This is my attempted (and failed) script: I’m getting NSReceiverEvaluationScriptError3(1)

on action theObject
	if the current menu item of popup button "search_list" of window "mainWindow" is equal to "title_search" then
		if the name of theObject is equal to "search_box" then
			set songTitle to content of text field "search_box" of window "mainWindow"
			repeat
				tell application "iTunes"
					set fixedIndexing to fixed indexing
					set fixed indexing to true
					
					set tracksFound to every track of first library playlist whose name contains songTitle
					set foundCount to count of tracksFound
					
					if foundCount is 1 then
						set playTrack to beginning of tracksFound
						exit repeat
					else if foundCount is not 0 then -- greater than 1    
						set tracksList to {}
						repeat with thisTrack in tracksFound
							set trackName to name of thisTrack
							tell artist of thisTrack to if it is "" then
								set trackArtist to "Unknown"
							else
								set trackArtist to it
							end if
							tell album of thisTrack to if it is "" then
								set trackAlbum to "Unknown"
							else
								set trackAlbum to it
							end if
							set trackInfo to trackName & " - " & trackArtist & " (" & trackAlbum & ")" as string
							set tracksList to tracksList & trackInfo
						end repeat
						
						set trackChosen to choose from list tracksList with prompt ¬
							"Your search turned up " & foundCount & " results. Please specify which track you want to play."
						if trackChosen is false then
							set fixed indexing to fixedIndexing
							return
						end if
						set my text item delimiters to return
						set tracksList to tracksList as Unicode text
						set my text item delimiters to beginning of trackChosen
						set trackNumber to count of paragraphs of first text item of tracksList
						set my text item delimiters to ""
						
						set playTrack to item trackNumber of tracksFound
						exit repeat
					end if
				end tell
				set songTitle to text returned of (display dialog "Your search turned up 0 results. Try again?." default answer "" buttons {"Cancel", "Go"} default button 2)
			end repeat
			tell application "iTunes"
				play playTrack
				set fixed indexing to fixedIndexing
			end tell
		end if
		
	else if the current menu item of popup button "search_list" of window "mainWindow" is equal to "artist_search" then
		if the name of theObject is equal to "search_box" then
			set songTitle to content of text field "search_box" of window "mainWindow"
			repeat
				tell application "iTunes"
					set fixedIndexing to fixed indexing
					set fixed indexing to true
					
					set tracksFound to every track of first library playlist whose artist contains songTitle
					set foundCount to count of tracksFound
					
					if foundCount is 1 then
						set playTrack to beginning of tracksFound
						exit repeat
					else if foundCount is not 0 then -- greater than 1    
						set tracksList to {}
						repeat with thisTrack in tracksFound
							set trackName to name of thisTrack
							tell artist of thisTrack to if it is "" then
								set trackArtist to "Unknown"
							else
								set trackArtist to it
							end if
							tell album of thisTrack to if it is "" then
								set trackAlbum to "Unknown"
							else
								set trackAlbum to it
							end if
							set trackInfo to trackName & " - " & trackArtist & " (" & trackAlbum & ")" as string
							set tracksList to tracksList & trackInfo
						end repeat
						
						set trackChosen to choose from list tracksList with prompt ¬
							"Your search turned up " & foundCount & " results. Please specify which track you want to play."
						if trackChosen is false then
							set fixed indexing to fixedIndexing
							return
						end if
						set my text item delimiters to return
						set tracksList to tracksList as Unicode text
						set my text item delimiters to beginning of trackChosen
						set trackNumber to count of paragraphs of first text item of tracksList
						set my text item delimiters to ""
						
						set playTrack to item trackNumber of tracksFound
						exit repeat
					end if
				end tell
				set songTitle to text returned of (display dialog "Your search turned up 0 results. Try again?." default answer "" buttons {"Cancel", "Go"} default button 2)
			end repeat
			tell application "iTunes"
				play playTrack
				set fixed indexing to fixedIndexing
			end tell
			
			
		else if the current menu item of popup button "search_list" of window "mainWindow" is equal to "album_search" then
			if the name of theObject is equal to "search_box" then
				set songTitle to content of text field "search_box" of window "mainWindow"
				repeat
					tell application "iTunes"
						set fixedIndexing to fixed indexing
						set fixed indexing to true
						
						set tracksFound to every track of first library playlist whose album contains songTitle
						set foundCount to count of tracksFound
						
						if foundCount is 1 then
							set playTrack to beginning of tracksFound
							exit repeat
						else if foundCount is not 0 then -- greater than 1    
							set tracksList to {}
							repeat with thisTrack in tracksFound
								set trackName to name of thisTrack
								tell artist of thisTrack to if it is "" then
									set trackArtist to "Unknown"
								else
									set trackArtist to it
								end if
								tell album of thisTrack to if it is "" then
									set trackAlbum to "Unknown"
								else
									set trackAlbum to it
								end if
								set trackInfo to trackName & " - " & trackArtist & " (" & trackAlbum & ")" as string
								set tracksList to tracksList & trackInfo
							end repeat
							
							set trackChosen to choose from list tracksList with prompt ¬
								"Your search turned up " & foundCount & " results. Please specify which track you want to play."
							if trackChosen is false then
								set fixed indexing to fixedIndexing
								return
							end if
							set my text item delimiters to return
							set tracksList to tracksList as Unicode text
							set my text item delimiters to beginning of trackChosen
							set trackNumber to count of paragraphs of first text item of tracksList
							set my text item delimiters to ""
							
							set playTrack to item trackNumber of tracksFound
							exit repeat
						end if
					end tell
					set songTitle to text returned of (display dialog "Your search turned up 0 results. Try again?." default answer "" buttons {"Cancel", "Go"} default button 2)
				end repeat
				tell application "iTunes"
					play playTrack
					set fixed indexing to fixedIndexing
				end tell
			end if
		end if
	end if
end action


Well for one you can have a button named “Search” and set it’s key equiv to Return. Or if you don’t want to see a button make it invisible with the key equiv.

well the whole point of this program is that is is really small…buttons take up space…like if I didn’t put anything about popup buttons in my code is works perfectly…but only for song titles, not artists, or albums.

gah! genious!

actually…that does something weird…it reall slows it down for some reason…what I would like to know if it can be done the way I said it…like it seems so simple…you pick either title search, artist search, or album search from the popup button, type whatever you want to listen to in the text box and hit enter and bam!..but no it has to be all complicated and stuff:P

To get a text field to “submit” it’s value on enter, you need to make a change to one of it’s settings in IB. With the text field selected, change the “Send Action On:” setting to “Enter Only”. That way, whenever the box is the first responder and the enter key is pressed, it triggers the “on action” event of the object. Then you can use something like…

on action theObject
	if name of theObject is "textField" then
		set theInput to (string value of theObject)
	end if
end action

j

i already did that…i think what my real problem is i don’t know what to check off for my popup button in IB, and I don’t know the coding to check to see which item it’s on.

set theTitle to the title of popup button "selectTitle"

This set theTitle to whatever the popup button is on.

Going back to the arrow keys question

There is no way to do this in IB or ASStudio alone. There are a handful of other ways to do this, though, all of which are going to involve some form of Obj-c code. An easy way to do it using as much applescript as possible is this…

  1. Create an NSObject subclass according to the directions found in my obj-c subclassing faq. For this example, I’ve named my subclass “JBObject”

  2. Add up to 4 buttons to your project (one for each: up, down, left and right). If you do not need all of them, just add what you do need.

  3. Connect each of your buttons to the “awake from nib” handler. Below is the code that initializes each of the buttons in your awake from nib handler. Make sure to change the “Button_Up”, “Button_Dn”, etc. names to match your own names…

on awake from nib theObject
	if name of theObject is "Button_Up" then
		call method "addKeyEquivalent:toButton:" of class "JBObject" with parameters {"UP", theObject}
	else if name of theObject is "Button_Dn" then
		call method "addKeyEquivalent:toButton:" of class "JBObject" with parameters {"DN", theObject}
	else if name of theObject is "Button_Lt" then
		call method "addKeyEquivalent:toButton:" of class "JBObject" with parameters {"LT", theObject}
	else if name of theObject is "Button_Rt" then
		call method "addKeyEquivalent:toButton:" of class "JBObject" with parameters {"RT", theObject}
	end if
end awake from nib
  1. Add the following code to your obj-c subclass files…

Your buttons should now respond to their respective arrow keys. I also posted some code in this thread a while back showing a different approach, which assigns arrow keys to menu items.

j

actually it was able to be done through IB, i opened up the special characters palette in textedit and copied the arrow, and pasted it in IB. Works perfectly, no nned for crazy cocoa stuff

That’s interesting, because that doesn’t seem to work for me. Which arrow are you using, and how did you go about finding it? Please elaborate a bit, as this is something that would be nice for everyone to know.

j

i am not using my computer (im on a windows) but in special charactersthere is something like “show all characters” or something like that. now go into iTunes and in the controls look at the arrow they have for their shortcut. find that one, it works.

OK, discovered the problem. You must be adding key equivalents to menu items… not buttons… as I was. It seems that buttons do not respond to non-alphanumeric keys without having a modifier key, such as command. I was setting the key equivalent as just the dotted arrow, without the cmd key as a modifier, which didn’t work. It still doesn’t work with a button, even when the cmd key is selected as a modifier, but it works with or without modifiers in a menu item.

So, if you’re trying to set an arrow key as a key equivalent for a button, you will need the obj-c code I posted above. If you’re using a menu item (including those in popup buttons), then it will work as hendo13 describes using just the cut/paste into IB.

j

Disappointed to hear that there’s no simple way to specify the arrows as the key equivalents for buttons: that was one of my objectives in porting my GUI from Pashua to AS.

Well, I guess I better look into the C code stuff in this thread…

Thanks.

-Dave

Model: Intel-CPU iMac running 10.4.9; Xcode 2.4
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I have an App which is set to always float on top, and to display the image currently selected in the finder.

I want to be able to use the up and down arrows on the keyboard and up and down buttons on the app to navigate in the finder.

Using Jobu scripts in post #11,
I have added a on clicked theObject handler, for the buttons upOne and downOne

For each button, there is an equivalent keystroke with the key code for the up arrow or down.

on clicked theObject

	if name of theObject is "upOne" then
		tell application "Finder" to activate
		tell application "System Events" to keystroke (key code 126)
end if
	if name of theObject is "downOne" then
	tell application "Finder" to activate
		tell application "System Events" to keystroke (key code 125)
	end if
end clicked

This works sort of…

If I click the buttons the finder activates, and advances once up or down in the file list.

But if I use the arrow keys on the keyboard, the buttons flash, and the finder activates and then finder advances Twice up or down in the file list. ??
It should only advance once

Ideally I would like to control the finder while staying in my App, and without having to Activate the finder,
but alas I am only just learning Applescript Studio.
I suspect it can be done, but in Object-c for which i have even less knowledge at the moment.

Is this actually possible ?? or is there an alternative, I would appriciate any help

many thanks

Mark