Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScriptObjC Menulet Template

Hey guys,

N00b question but could anyone give me a template for making a menu let in ASOC? I have the rest of my code ready to be input into a template. Again, I know this is a beginner question but it would really help, thanks!

Mac OS X (10.6.8)

Posted on Dec 1, 2011 2:57 PM

Reply
Question marked as Best reply

Posted on Dec 1, 2011 3:23 PM

I have a conversion example project (ObjC to ASObjC) that uses a menulet (status menu). It doesn't do much, just puts the local IP addresses into the menu, but it should get you started (a link to the original tutorial is in there also) - you can download it here.

18 replies

Dec 1, 2011 4:07 PM in response to XcodeDeveloper

Well, that was the purpose of my example project. You just need to convert to ASObjC, for example"


statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];


becomes


set statusItem to NSStatusBar's systemStatusBar's statusItemWithLength_(current application's NSSquareStatusItemLength)


and


statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];


becomes


set highlightIcon to NSImage's alloc's initWithContentsOfFile_(|bundle|'s pathForResource_ofType_("HighlightIcon", "png"))


and so on...

Dec 2, 2011 6:39 PM in response to XcodeDeveloper

Kind of hard to tell since we are flying blind, but let's review:


  • you have your IB menu connected to a script property
  • the status bar item instance is created
  • images are created for your status bar item (if desired), and the images (or just a title) are set
  • the menu for the status bar item is set
  • an error is not shown in the run console


If you are just setting a title for the menu, there isn't much to it (despite this forums formatting), for example:


propertymyMenu : missing value-- connected to the menu to use in the status bar item

propertystatusItem : missing value-- this will be the status bar item instance


onawakeFromNib()


# Create the NSStatusBar and set its length


-- statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];


setstatusItemtocurrent application's NSStatusBar's systemStatusBar's statusItemWithLength_(current application's NSSquareStatusItemLength)



# Set the menu title to a character


statusItem's setTitle_("⬇") -- ⌘⁇✖⍢➤



# Tell the NSStatusItem what menu to load


-- [statusItem setMenu:statusMenu];


statusItem's setMenu_(myMenu)

endawakeFromNib

Dec 3, 2011 7:13 PM in response to XcodeDeveloper

You need to be careful of some of the coersions/differences between the various AppleScriptObjC and ObjC classes. In this case when sending a boolean value to a Cocoa method it needs to be true or 1 - the value yes doesn't get coerced to the right class. This error would have shown up in the run console log as:


... unable to set argument 2 - the AppleScript value <NSAppleEventDescriptor: 'yes '> could not be coerced to type c. (error -10000)

Dec 4, 2011 11:30 AM in response to XcodeDeveloper

In my example project, I added a couple of png files to the project called "NormalIcon" and "HighlightIcon". Once those are in there, getting the contents of the files and setting the icon images goes something like:


setmyBundletocurrent application's NSBundle's mainBundle()

setnormalIcontocurrent application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("NormalIcon", "png"))

sethighlightIcontocurrent application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("HighlightIcon", "png"))


statusItem's setImage_(normalIcon)

statusItem's setAlternateImage_(highlightIcon)

AppleScriptObjC Menulet Template

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.