Hi, I’m a long-time Applescripter, but this is my first Studio project. As such, I’ve run into several stumbling blocks. I hope you can help.
Link a Horizontal Top Slider and a Combo Box. I had been doing it by connecting them both ways with takeDoubleValueFrom, but that gave me error -1708 when I changed the slider’s value.
How to set a popup menu’s contents dynamically. In this case, I want to let the user select a running application.
How to set an Image View to the icon of the app the user chose.
How to disable the Apply button when there isn’t an app chosen.
Thanks for your help.
(If you’re wondering what the app is, it’s a nicer front-end to the UNIX Nice command. No pun intended.)
1.) if you bind the elements the way you described, it should work.
2.)
something like this, replace the references to the popup button and the window to your appropriate ones
on updateApplications()
tell application "System Events" to set apps to name of processes whose background only is false
tell window "main"
delete every menu item of menu of popup button "applications"
repeat with i in apps
make new menu item at the end of menu items of menu of popup button "applications" with properties {title:i, enabled:true}
end repeat
end tell
end updateApplications
3.) connect the popup menu to the choose menu item handler
use this handler, adjust the reference to the image view
on choose menu item theObject
set currentApp to title of current menu item of theObject
tell application "System Events" to set thePath to file of process currentApp as text
set imageView to a reference to image view "imageView" of window "main"
call method "displayApplicationIcon:forView:" of class "DisplayIcon" with parameters {(POSIX path of thePath), imageView}
end choose menu item
In Xcode define an Objective-C class with menu File > New File > Objective-C class with name DisplayIcon
replace the contents of DisplayIcon.h with
[code]#import <Cocoa/Cocoa.h>
@interface DisplayIcon : NSObject {
}
(void)displayApplicationIcon:(NSString *)path forView:(NSImageView *)view; @end[/code]
replace the contents of DisplayIcon.m with
Thanks for all the help! This all worked perfectly. However, I now have several more questions.
How can I use getpriority() to (of all things) get the priority of an application?
How can I add a submenu? I want to add a divider to the popup that is populated with all the applications, and then under that an “Background Processes” item with the contents of
tell application "System Events" to set bgApps to name of processes whose background only is true
Can I set the image view to the generic application icon before an application is selected?
Why the heck won’t my scroll view and my combo view? They work in IBCocoaSimulator, when run from Interface Builder, but in the actual application the combo view will update the slider but the other way 'round doesn’t do anything. The only code I have referring to these gets the value of the slider, and sets it to 0 when a button is pressed.
Uhm. Hem. Haw. I don’t actually know where to put the code in #4 of my first set of questions. I told you I was a beginner! Where do I stick that?
(int)priority:(int)pid
{
int which = PRIO_PROCESS;
return getpriority(which, pid);
} @end[/code]
This is an example for Applescript Code
in the awake from nib handler the menu is populated (updateApplications()) and the icon is displayed (displayIcon())
in the updateApplications() handler an entry “none” is created at the beginning of the menu
in the displayIcon() handler the variable priority contains the value of the priority of the current application and the enabled status of the button is set
on awake from nib theObject
updateApplications()
displayIcon()
end awake from nib
on choose menu item theObject
displayIcon()
end choose menu item
on updateApplications()
tell application "System Events" to set apps to name of processes whose background only is false
set beginning of apps to "none"
tell window "main"
delete every menu item of menu of popup button "applications"
repeat with i in apps
make new menu item at the end of menu items of menu of popup button "applications" with properties {title:i, enabled:true}
end repeat
end tell
end updateApplications
on displayIcon()
set priority to 0
set currentApp to title of current menu item of popup button "applications" of window "main"
try
tell application "System Events"
set {file:thePath, unix id:pid} to process currentApp
end tell
set thePath to POSIX path of thePath
set priority to call method "priority:" of class "DisplayIcon" with parameter pid
-- log currentApp & ": " & priority
on error
set thePath to ""
end try
set imageView to a reference to image view "imageView" of window "main"
call method "displayApplicationIcon:forView:" of class "DisplayIcon" with parameters {thePath, imageView}
set enabled of button "myButton" of window "main" to (currentApp is not "none")
end displayIcon
question #2: I don’t know, whether you can create a submenu of a popup button programmatically
question #4: You should set the bindings in both directions, probably accessing one of the UI elements in your code causes the error