Sunday, September 25. 2005
Posted by Jay
Comments (8) Trackbacks () Defined tags for this entry: cocoascriptmenu, niceplayer, object oriented design, open source, programming
My Sunday Project - Reusable Cocoa Script MenuThe Sunday ProjectSo last sunday I started on something new, really it’s a feature for NicePlayer, but also a feature in a lot of other existing apps out there, and could be useful in a lot of cocoa programs that don’t have this feature yet, so I wrote my implementation as an embedable framework, and am releasing under the MPL/LGPL/GPL (my latest preferred OSI approved license for those who notice what license I release under). So here’s a riddle, what do iTunes, DVD Player, Xcode, FlySketch, NetNewsWire, MarsEdit and BBEdit all have in common? Give up? They all have one of these (more or less):
Their own script menu. Uses in NicePlayerHaving a script menu in NicePlayer has been something in the back of my mind for a while. There are some features requests, while really simple, are very specific to individual user needs and we can’t justify adding a feature. Sometimes there are features requests that just don’t fit into Robert’s or my idea of NicePlayer, and we barely have enough time to add the features we want to add, so in the next release users can add their own menu commands in this script menu. One of the features introduced in 0.92 of NicePlayer was an option to remove the fixed aspect ratio. Maybe you want to distort the movie, maybe the aspect ratio is just slightly off, this feature works in those cases, and only adds one more menu to the window and is inline with adding basic window options in the Window menu that we did before. However this isn’t useful when someone has a lot of media that is consistently using non-square pixels. In the case of standard media formats, the proper behavior of NicePlayer should be to automatically adjust (this feature has been added for the DV codec when using the CoreVideo plugin in the next version of NicePlayer 0.93). However there are people out there for some reason, how have media that is encoded with non-square pixels for no standard reason, and there isn’t a way to detect the correct aspect ratio. It turns out writing an AppleScript with the current NicePlayer dictionary to set a Window to a different aspect ratio is pretty trivial, so this is one of the scripts we’ll likely include in the next release (since its one the scripts i’ve been using to test anyway). I think the main benefit of having the script menu, will be nicer integration with other apps. Whether having it integrate with a cataloging app, Toast, or just organizing with the finder, there seem to be many potential uses in this respect. CocoaScriptMenu.Framework in Your Own ProgramThe framework is called CocoaScriptMenu.Framework and available on my software page. It’s not my favorite name of the software I’ve written, but I think it’ll help in being google-able for those wanting it’s feature. I’m releasing it as version 1.0, and as I said before under the MPL/LGPL/GPL license. The basic way to use it is to:
And that will give you a typical script menu once you compile and run your app. FeaturesI wrote typical, as not all script menus are the same. I tried to add what I felt were the best features of all the script menus, while being able to behave, depending on how you use it, like 80% of the script menus I’ve seen with out any extra customization. Some of the features are
Extending w/o ModifyingThe singleton [CSMScriptMenu sharedMenuGenerator] has 4 optional delegate methods that allow you to keep the core functionality but make some slightly different script menus without having to modify the source (although modification is certainly an option. (warning most of this is untested as I use only the default implementation of each of these in NicePlayer) -(NSMenuItem*)showScriptFolderMenuItem; -(NSArray*)argumentsForShellScripts; -(id)scriptMenuItemOrItems; -(NSArray*)scriptLocations; Extending by ModifyingSo in version 1.0 the script running implementations are very basic. They are setup as a Class Cluster, with the CSMCommand class providing the public interface and several subclasses that implement script running for various types of scripts or executables. Diagram of class hierarchy: alloc on CSMCommand returns a singleton instance of CSMPlaceholderCommand. CSMPlaceholderCommand’s initWithScriptPath: works as parameterized factory method and depending on the path passed in returns the correct concrete implementation allocated and initialized. So to extend the implementation of script execution for an existing file type, you would just modify one of the subclasses. To add a new filetype you would modify the initWithScriptPath: factory method and add a new subclass. initWithScriptPath: uses Apple’s Uniform Type Identifiers to determine which concrete class to instantiate, so order does matter, make sure you add the more specific type checks in the beginning of the method and the more general towards the end. There are a lot of ways that I can think of that the concrete script execution classes could be improved, however for NicePlayer these all work well, thus I figured for version 1.0 it was better to stick with the basic implementation and release it now, rather than try and over engineer. So I’ll wait and see if people need more or not, not to mention they have the option of contributing code. Updated at Cocoa Script Menu Revised 1.01 Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Your project reminded me of the old "Menu Sharing Tool Kit" that was part of Userland Frontier. I think This old framework allowed menus to be added to applications from the Frontier scripting system. Many apps supported this technology: Netscape, IE, Quark, BBEdit, the Finder, etc. The Cool thing about Menu Sharing was that apps could add menu items to other apps automatically by registering with the Frontier Server. Under OS X, the Services sub-menu is kinda like this. It might be worth your while to review this old concept to see if any of its ideas could be useful in your API. http://www.scripting.com/frontier/sdk/menuSharing.html Very nice concept! I wonder if you included code so that executing a compiled script or applet will preserve the value of persistent properties across executions? There is a long-standing bug in Cocoa’s -[NSAppleScript executeAndReturnError] method that fails to do this. [...] tle> Saturday, October 1, 2005 Reusable Cocoa Script Menu James Tuley’s CocoaScriptMenu.Framework (MPL/LGPL/GPL) includes most of the standard script menu features [...] Thanks Bill. And in regards to your question, all the concrete constructors are the bare minimum implementation, at least at this stage, so nothing even remotely special is going on with the CSMAppleScriptCommand class. Thanks for the heads up about the NSAppleScript and properties. Lee, Thanks for pointing out the "Menu Sharing Tool Kit", it does pose some interesting ideas. I wonder if you included code so that executing a compiled script or applet will preserve the value of persistent properties across executions? I wanted to test this out, so I wrote this test script. property theTest : 0 Running the script does increment every time you call it from the script menu, but restarts starts at 1 again when after relaunching the the host application of CocoaScriptMenu. An applet is just an application to CocoaScriptMenu so it just gets launched and isn’t used by NSAppleScript and thus it keeps incrementing no matter what. This seems to be very similar to FastScripts (www.red-sweater.com)! [...] so prominent apple apps with their own menu too. NicePlayer as taken a side and it’s CocoaScriptMenu (since I wrote it for this app). The other thing I added in this release somewhat Baddish was an [...] |
QuicksearchStatic PagesCalendar
ArchivesSyndicate This BlogShow tagged entries |
|||||||||||||||||||||||||||||||||||||||||||||||||
