-
Notifications
You must be signed in to change notification settings - Fork 1
/
AFIScriptabilityCategory.m
120 lines (93 loc) · 3.22 KB
/
AFIScriptabilityCategory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// AFIScriptabilityCategory.m
// Add Folder Icons
//
// Created by Andrew Hodgkinson on 28/03/16.
// Copyright © 2016 Hipposoft. All rights reserved.
//
// Based on:
// https://www.apeth.net/matt/scriptability/scriptabilityTutorial.html
//
#import "AFIScriptabilityCategory.h"
#import "IconStyle.h"
#import "IconStyleManager.h"
#pragma mark - NSObject extensions
@implementation NSObject ( AFIScriptabilityCategory )
- ( void ) returnError: ( int ) errorNumber string: ( NSString * ) message
{
NSScriptCommand * command = [ NSScriptCommand currentCommand ];
[ command setScriptErrorNumber: errorNumber ];
if ( message ) [ command setScriptErrorString: message ];
}
@end
#pragma mark - MainMenuController extensions
@implementation NSApplication ( AFIScriptabilityCategory )
// This is deprecated on OS X 10.10 or later and the documentation says "do
// not implement this method" (!) but we've no choice, because we run on OS
// X 10.6 and later - it's required for 10.6-10.9.
//
// Given its uncertain status, always return YES!
//
- ( BOOL ) application: ( NSApplication * ) sender
delegateHandlesKey: ( NSString * ) key
{
return YES;
}
- ( NSArray * ) iconStyleArray
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
return [ iconStyleManager getStyles ];
}
- ( unsigned long ) countOfIconStyleArray
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
return [ [ iconStyleManager getStyles ] count ];
}
- ( IconStyle * ) objectInIconStyleArrayAtIndex: ( unsigned int ) index
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
return [ iconStyleManager getStyles ][ index ];
}
- ( IconStyle * ) valueInIconStyleArrayAtIndex: ( unsigned int ) index
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
NSArray * iconStyles = [ iconStyleManager getStyles ];
if ( ! [ [ NSScriptCommand currentCommand ] isKindOfClass: [ NSExistsCommand class ] ] )
{
if ( index >= iconStyles.count )
{
[ self returnError: errAENoSuchObject string: @"No such icon style." ];
return nil;
}
}
return iconStyles[ index ];
}
- ( IconStyle * ) valueInIconStyleArrayWithName: ( NSString * ) name
{
IconStyleManager * iconStyleManager = [ IconStyleManager iconStyleManager ];
IconStyle * iconStyle = [ iconStyleManager findStyleByName: name ];
if ( ! [ [ NSScriptCommand currentCommand ] isKindOfClass: [ NSExistsCommand class ] ] )
{
if ( iconStyle == nil )
{
[ self returnError: errAENoSuchObject string: @"No such icon style." ];
return nil;
}
}
return iconStyle;
}
@end
@implementation IconStyle ( AFIScriptabilityCategory )
- ( NSScriptObjectSpecifier * ) objectSpecifier
{
NSScriptClassDescription * appDesc = ( NSScriptClassDescription * ) [ NSApp classDescription ];
return
[
[ NSNameSpecifier alloc]
initWithContainerClassDescription: appDesc
containerSpecifier: nil
key: @"iconStyleArray"
name: [ self name ]
];
}
@end