Project Template (Dock Menu, Status Menu, & Background-O

My new PB should be here tomorrow (it’s somewhere in Ohio, Rob). So, while waiting, I thought I’d share a demo project I’ve created that has a number of very useful features all in one that I haven’t seen comprehensively detailed before.

This project, MenuApp, is a template of sorts demonstrating the following features:

  1. A Dock menu with a dynamic submenu.
  2. A Status Menu (similar to a menu extra) with a dynamic submenu.
  3. A background-only toggle to make the app modify itself to be either a standard application with a Dock menu, application menu, and listed in the basic applications list (i.e., the list shown when you do a force quit) or a background-only app with only the status menu visible.
  4. An AppleScript to Objective-C bridge for calling Objective-C methods from AppleScript.
  5. An Objective-C to AppleScript bridge for calling AppleScript handlers from Objective-C.

All from within AppleScript Studio. When building and running, you may receive an “NSReceiverEvaluationScriptError: 3 (1)” error. This seems somewhat arbitrary and, if it appears, the application won’t run properly. This error is related to waking elements from the NIB and can sometimes be remedied by modifying and saving the MainMenu NIB and then rebuilding the app. There are some basic comments in the main AppleScript as well as the Objective-C Application.m file that describe what is going on in the code.

To enable the background-only feature in your project, modify the internal Info.plist to create the LSUIElement property which toggles the background only bit of an app (you can add this by going to “Build>Edit Active Target For…” from PB, go to Expert View under Info.plist Entries, and add a new sibling with a property list of LSUIElement, a class of string, and a value of 1 (for background only) or 0 (for standard)). This has already been enabled in the MenuApp project.

If you incorporate some (or all) of the code from this project into your own (which you are free to do), I’d love to hear about it and a little credit would be nice…

I’m not sure how else to publicize this on the MacScripter site since it should probably be in Code Exchange yet the nature of it (i.e., that it requires an external file) is better suited for ScriptBuilders. Comments from the admins?

Jon

We can accept it at ScriptBuilders and add it to the latest AS Studio apps with or without a news blurb on the cover page.

http://macscripter.net/studio_news.php

I need to create a new category at ScripBuilders for AS Studio projects anyway.
(I went ahead and made a new category for AppleScript-Studio projects at ScriptBuilders 01:05 CST)

Cool! Maybe I can intercept it and keep it in Ohio. :wink: Thanks for sharing the project, Jon. While I haven’t taken the dive into Studio development, I’m sure that your template will be appreciated by those who have.

– Rob

I’d love to integrate a status menu into my project.

i’m a bit stumped about how to use the status menu from the above app in my own app.

any guidance? (i’ll be happy to give credit if i can ever get this monster done…)

I’ve never found this to be stable so I’m not sure I recommend this code for distribution apps. So, what are you trying to do? Is this an app for you or something you want to distribute? If it is only for internal use, I might recommend this code.

You can also take a look at some of my other AS Studio examples on my website:

[url=http://homepage.mac.com/jonn8/as/]http://homepage.mac.com/jonn8/as/[/url]

Jon

yeah, i’d like to distribute it down the road, if i can keep making progress slogging through this jungle.

all i’m trying to do is to have a status menu showing a generated list of items. would that part of the code be unstable? i can live without it being a background-only app, although that would be a nice option.

(thanks for mentioning your site. i’m looking for all the learning resources i can get my hands on.)

It’s the status menu code that is unstable. All the rest (bg only, Dock menu, the bridges) all seem to work fine. Perhaps this tutorial will help:

[url=http://cocoadevcentral.com/articles/000052.php]http://cocoadevcentral.com/articles/000052.php[/url]

Jon

ugh. i checked out the cocoadevcentral article, but i don’t know a lick of cocoa, so it’s all gibberish to me.

i guess my flood of unfocused questions are:

is the instability a function of the way you’ve written your code, or is implementing a Status Menu in AS Studio basically impossible?

would it be possible to have any kind of Status Menu be stable when called from an AS Studio project? in other words, is the problem in some aspect of the functionality you’ve put in the menu, or just the basic existance of the menu itself?

am i correct in understanding that Status Menus are supported in cocoa, but not AppleScript?

and the bottomline question: i kinda need a status menu for this project. do i have to either abandon the project or learn cocoa?

Could you provide some more advice about this please? I am trying to create a dynamic Dock menu. Your code doesn’t quite work under 10.3 - it gets an error message, and the full submenu in the Dock menu is not generated. I tried to copy your code technique into my project, and I was able to get the menu items to appear in the Dock, but they crashed the program anytime you chose any of them. When I tried to fix this by connecting the Dock menu items to events, the Dock menu items disappeared entirely, even if I change it back. Now I don’t know what to do.

Thanks.

For just the Dock menu, see my DockMenu standalone project:

http://homepage.mac.com/jonn8/as/html/DockMenu.html

Jon

Yes, that’s what I’m using. I now have it at the point where it draws the Dock menu, you can select an item, and it displays a dialog saying “You selected…” I can see in the Obj-C code where it is calling an AppleScript to create that dialog, but I can’t figure out how to change it to call my own AppleScript function. I have tried the way in your template as well as other ways that seemed like a good idea, but they don’t work. How exactly do you link the menu items to an action?

Specifically,
call method “populateMenu:withData:” with parameters {theObject, {“A”,“B”,“C”}}
works, but
call method “populateMenu:withData:withAction:” with parameters {theObject, {theString, "Record: ", “Erase Record”},“my_action”}
does not even draw the menu.

EDIT: Never mind, I think I figured it out. See the result here (soon)]:
http://scriptbuilders.net/download.php?id=1872