HI,
sorry for maybe double-posting it, but I got an error for textmate- dev list. (?) __
I've just had a look at the new DIALOG2 stuff. Very nice ;)
Here comes a suggestion to allow the user to display the alert as sheet bound to the current document window:
If option -e or 'sheet' is given it shows the alert as sheet.
CODE: ######################################################################## ##
#import "../Dialog2.h" #import "../TMDCommand.h" #import "../OptionParser.h"
/* echo '{alertStyle = warning; buttonTitles = ('OK'); messageTitle = 'test'; informativeText = 'Testing';}' | "$DIALOG" alert
"$DIALOG" help alert "$DIALOG" alert -s critical -m "FOOL!" -t "test" -1 foo -2 bar -3 baz "$DIALOG" alert -e -s critical -m "FOOL!" -t "test" -1 foo -2 bar -3 baz */
// ========= // = Alert = // =========
@interface TMDAlertCommand : TMDCommand { } @end
@implementation TMDAlertCommand
int sheetReturn;
+ (void)load { [super registerObject:[self new] forCommand:@"alert"]; }
static option_t const expectedOptions[] = { { "m", "message", option_t::required_argument, option_t::string, "Message title."}, { "t", "text", option_t::required_argument, option_t::string, "Informative text for the alert."}, { "s", "alert-style", option_t::required_argument, option_t::string, "One of warning, critical or informational (the default)."}, { "1", "button1", option_t::required_argument, option_t::string, "Button 1 label."}, { "2", "button2", option_t::required_argument, option_t::string, "Button 2 label."}, { "3", "button3", option_t::required_argument, option_t::string, "Button 3 label."}, { "e", "sheet", option_t::no_argument, option_t::none, "If given displays alert as sheet."}, };
- (void)handleCommand:(CLIProxy*)proxy { // NSFileHandle* fh = [options objectForKey:@"stderr"];
SetOptionTemplate(proxy, expectedOptions); NSAlertStyle alertStyle = NSInformationalAlertStyle; NSString *alertStyleString = [proxy valueForOption:@"alert-style"]; NSDictionary *resultDict = nil; NSAlert *alert = [[[NSAlert alloc] init] autorelease]; int offset = 0; if([alertStyleString isEqualToString:@"warning"]) { alertStyle = NSWarningAlertStyle; } else if([alertStyleString isEqualToString:@"critical"]) { alertStyle = NSCriticalAlertStyle; } else if([alertStyleString isEqualToString:@"informational"]) { alertStyle = NSInformationalAlertStyle; } [alert setAlertStyle:alertStyle]; if([proxy valueForOption:@"message"]) [alert setMessageText:[proxy valueForOption:@"message"]]; if([proxy valueForOption:@"text"]) [alert setInformativeText:[proxy valueForOption:@"text"]]; // Setup buttons offset = NSAlertFirstButtonReturn; if ([proxy valueForOption:@"button1"]) [alert addButtonWithTitle:[proxy valueForOption:@"button1"]]; if ([proxy valueForOption:@"button2"]) [alert addButtonWithTitle:[proxy valueForOption:@"button2"]]; if ([proxy valueForOption:@"button3"]) [alert addButtonWithTitle:[proxy valueForOption:@"button3"]]; BOOL modal = YES; if([proxy valueForOption:@"sheet"]) modal = NO; // Show the alert if(modal) { int alertResult = ([alert runModal] - NSAlertFirstButtonReturn); resultDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:alertResult] forKey:@"buttonClicked"]; [TMDCommand writePropertyList:[NSString stringWithFormat:@"%d",sheetReturn - offset] toFileHandle:[proxy outputHandle]]; } else { id documentWindow =nil; enumerate([NSApp windows], id item) if([item isKindOfClass:NSClassFromString(@"NSWindow")]) if([item isKeyWindow]) documentWindow = item; sheetReturn = -1; [alert beginSheetModalForWindow:documentWindow modalDelegate:self didEndSelector:@selector(mySheetDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [NSApp runModalForWindow:[alert window]]; [TMDCommand writePropertyList:[NSString stringWithFormat:@"%d",sheetReturn - offset] toFileHandle:[proxy outputHandle]]; } }
-(id)mySheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { [NSApp endSheet:sheet]; sheetReturn = returnCode; [NSApp stopModal]; return nil; }
- (NSString *)commandDescription { return @"Show an alert box."; }
- (NSString *)usageForInvocation:(NSString *)invocation; { return [NSString stringWithFormat:@"%@ «options»\n\nOptions:\n%@", invocation, GetOptionList(expectedOptions)]; } @end
######################################################################## ##
Any comments?
Cheers,
--Hans
Hi,
I optimized the code a bit and I changed the following:
- option -i (--icon) FILE PATH to any kind of an image, displays a user-defined icon; if nothing found it shows the default one
- option -h (--header) sets the window title; if not a sheet
- option -c (--color) sets the background color; passed as #RRGGBBAA (alpha only for sheets)
- if no button is specified now the returnCode is 0
- option -e (--sheet) displays as sheet works now also at HTML output windows via TextMate.system
- option -e now returns a clean plist
- I simplified the notation for the alert style; now you can write "-s w" or "-s war" or "-s warnung" to set it to "warning" (only the first character disambiguates the style)
Here's a tiny example:
Here's the code:
Cheers,
--Hans