1 | //
|
---|
2 | // PSScriptAlert.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Sat Oct 26 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSScriptAlert.h"
|
---|
10 | #import "BDAlias.h"
|
---|
11 | #import "NSDictionary-NJRExtensions.h"
|
---|
12 |
|
---|
13 | // property list keys
|
---|
14 | static NSString * const PLAlertAlias = @"alias"; // NSData
|
---|
15 |
|
---|
16 | @implementation PSScriptAlert
|
---|
17 |
|
---|
18 | + (PSScriptAlert *)alertWithScriptFileAlias:(BDAlias *)anAlias;
|
---|
19 | {
|
---|
20 | return [[[self alloc] initWithScriptFileAlias: anAlias] autorelease];
|
---|
21 | }
|
---|
22 |
|
---|
23 | - (id)initWithScriptFileAlias:(BDAlias *)anAlias;
|
---|
24 | {
|
---|
25 | if ( (self = [super init]) != nil) {
|
---|
26 | alias = [anAlias retain];
|
---|
27 | }
|
---|
28 | return self;
|
---|
29 | }
|
---|
30 |
|
---|
31 | - (BDAlias *)scriptFileAlias;
|
---|
32 | {
|
---|
33 | return alias;
|
---|
34 | }
|
---|
35 |
|
---|
36 | - (void)triggerForAlarm:(PSAlarm *)alarm;
|
---|
37 | {
|
---|
38 | NSString *scriptPath = [alias fullPath];
|
---|
39 |
|
---|
40 | if (scriptPath == nil) {
|
---|
41 | NSRunAlertPanel(NSLocalizedString(@"Can't find script", "Title of alert sheet when alias to script didn't resolve"), NSLocalizedString(@"Pester couldn't find the script for the alarm '%@'.", "Message displayed in alert sheet when alias to script didn't resolve"),
|
---|
42 | nil, nil, nil, [alarm message]);
|
---|
43 | } else {
|
---|
44 | NSDictionary *errorInfo;
|
---|
45 | NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath: scriptPath] error: &errorInfo];
|
---|
46 | if (script == nil) {
|
---|
47 | NSString *errorMessage = [errorInfo objectForKey: NSAppleScriptErrorMessage];
|
---|
48 | NSNumber *errorNumber = [errorInfo objectForKey: NSAppleScriptErrorNumber];
|
---|
49 | NSString *appName = [errorInfo objectForKey: NSAppleScriptErrorAppName];
|
---|
50 | if (errorMessage == nil) errorMessage = [errorInfo objectForKey: NSAppleScriptErrorBriefMessage];
|
---|
51 | NSRunAlertPanel(@"Script loading error",
|
---|
52 | @"Pester encountered an error while attempting to load Ò%@Ó%@ %@",
|
---|
53 | nil, nil, nil,
|
---|
54 | [[NSFileManager defaultManager] displayNameAtPath: scriptPath],
|
---|
55 | errorMessage == nil ? @"" : [NSString stringWithFormat: @":\n\n%@%@", appName == nil ? @"" : @"Ò%@Ó reported an error: ", errorMessage],
|
---|
56 | errorNumber == nil ? @"" : [NSString stringWithFormat: @"(%@)", errorNumber]);
|
---|
57 | } else {
|
---|
58 | NSAppleEventDescriptor *scriptResult = [script executeAndReturnError: &errorInfo];
|
---|
59 | if (scriptResult == nil) {
|
---|
60 | NSString *errorMessage = [errorInfo objectForKey: NSAppleScriptErrorMessage];
|
---|
61 | NSNumber *errorNumber = [errorInfo objectForKey: NSAppleScriptErrorNumber];
|
---|
62 | NSString *appName = [errorInfo objectForKey: NSAppleScriptErrorAppName];
|
---|
63 | if (errorMessage == nil) errorMessage = [errorInfo objectForKey: NSAppleScriptErrorBriefMessage];
|
---|
64 | NSRunAlertPanel(@"Script execution error",
|
---|
65 | @"Pester encountered an error while attempting to execute the script Ò%@Ó%@ %@",
|
---|
66 | nil, nil, nil,
|
---|
67 | [[NSFileManager defaultManager] displayNameAtPath: scriptPath],
|
---|
68 | errorMessage == nil ? @"" : [NSString stringWithFormat: @":\n\n%@%@", appName == nil ? @"" : @"Ò%@Ó reported an error: ", errorMessage],
|
---|
69 | errorNumber == nil ? @"" : [NSString stringWithFormat: @"(%@)", errorNumber]);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | [self completedForAlarm: alarm];
|
---|
74 | }
|
---|
75 |
|
---|
76 | - (NSAttributedString *)actionDescription;
|
---|
77 | {
|
---|
78 | NSMutableAttributedString *string = [[@"Do script " small] mutableCopy];
|
---|
79 | NSString *scriptName = [alias displayNameWithKindString: NULL];
|
---|
80 | if (scriptName == nil) scriptName = NSLocalizedString(@"<<can't locate script>>", "Script alert description surrogate for script name when alias doesn't resolve");
|
---|
81 | [string appendAttributedString: [scriptName underlined]];
|
---|
82 | return [string autorelease];
|
---|
83 | }
|
---|
84 |
|
---|
85 | #pragma mark property list serialization (Pester 1.1)
|
---|
86 |
|
---|
87 | - (NSDictionary *)propertyListRepresentation;
|
---|
88 | {
|
---|
89 | NSMutableDictionary *plAlert = [[super propertyListRepresentation] mutableCopy];
|
---|
90 | [plAlert setObject: [alias aliasData] forKey: PLAlertAlias];
|
---|
91 | return [plAlert autorelease];
|
---|
92 | }
|
---|
93 |
|
---|
94 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
95 | {
|
---|
96 | return [self initWithScriptFileAlias: [BDAlias aliasWithData: [dict objectForRequiredKey: PLAlertAlias]]];
|
---|
97 | }
|
---|
98 |
|
---|
99 | @end
|
---|