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