Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: chrome/browser/cocoa/applescript/window_applescript.mm

Issue 3046042: Added AppleScript support (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/cocoa/applescript/window_applescript.h"
6
7 #include "base/logging.h"
8 #import "base/scoped_nsobject.h"
9 #include "base/scoped_ptr.h"
10 #include "base/time.h"
11 #import "chrome/browser/app_controller_mac.h"
12 #include "chrome/browser/browser.h"
13 #include "chrome/browser/browser_list.h"
14 #include "chrome/browser/browser_window.h"
15 #import "chrome/browser/chrome_browser_application_mac.h"
16 #include "chrome/browser/cocoa/applescript/constants_applescript.h"
17 #include "chrome/browser/cocoa/applescript/error_applescript.h"
18 #import "chrome/browser/cocoa/applescript/tab_applescript.h"
19 #include "chrome/browser/profile.h"
20 #include "chrome/browser/tab_contents/tab_contents.h"
21 #include "chrome/common/url_constants.h"
22
23 @interface WindowAppleScript(WindowAppleScriptPrivateMethods)
24 // The NSWindow that corresponds to this window.
25 - (NSWindow*)nativeHandle;
26 @end
27
28 @implementation WindowAppleScript
29
30 - (id)init {
31 // Check which mode to open a new window.
32 NSScriptCommand* command = [NSScriptCommand currentCommand];
33 NSString* mode = [[[command evaluatedArguments]
34 objectForKey:@"KeyDictionary"] objectForKey:@"mode"];
35 AppController* appDelegate = [NSApp delegate];
36
37 Profile* defaultProfile = [appDelegate defaultProfile];
38
39 if (!defaultProfile) {
40 AppleScript::SetError(AppleScript::errGetProfile);
41 return nil;
42 }
43
44 Profile* profile;
45 if ([mode isEqualToString:AppleScript::kIncognitoWindowMode]) {
46 profile = defaultProfile->GetOffTheRecordProfile();
47 }
48 else if ([mode isEqualToString:AppleScript::kNormalWindowMode] || !mode) {
49 profile = defaultProfile;
50 } else {
51 // Mode cannot be anything else
52 AppleScript::SetError(AppleScript::errInvalidMode);
53 return nil;
54 }
55 // Set the mode to nil, to ensure that it is not set once more.
56 [[[command evaluatedArguments] objectForKey:@"KeyDictionary"]
57 setValue:nil forKey:@"mode"];
58 return [self initWithProfile:profile];
59 }
60
61 - (id)initWithProfile:(Profile*)aProfile {
62 if (!aProfile) {
63 [self release];
64 return nil;
65 }
66
67 if ((self = [super init])) {
68 browser_ = Browser::Create(aProfile);
69 browser_->NewTab();
70 browser_->window()->Show();
71 scoped_nsobject<NSNumber> numID(
72 [[NSNumber alloc] initWithInt:browser_->session_id().id()]);
73 [self setUniqueID:numID];
74 [self setContainer:
75 (BrowserCrApplication*)[BrowserCrApplication sharedApplication]
76 property:AppleScript::kWindowsProperty];
77 }
78 return self;
79 }
80
81 - (id)initWithBrowser:(Browser*)aBrowser {
82 if (!aBrowser) {
83 [self release];
84 return nil;
85 }
86
87 if ((self = [super init])) {
88 // It is safe to be weak, if a window goes away (eg user closing a window)
89 // the applescript runtime calls appleScriptWindows in
90 // BrowserCrApplication and this particular window is never returned.
91 browser_ = aBrowser;
92 scoped_nsobject<NSNumber> numID(
93 [[NSNumber alloc] initWithInt:browser_->session_id().id()]);
94 [self setUniqueID:numID];
95 [self setContainer:NSApp
96 property:AppleScript::kWindowsProperty];
97 }
98 return self;
99 }
100
101 - (NSWindow*)nativeHandle {
102 // window() can be NULL during startup.
103 if (browser_->window())
104 return browser_->window()->GetNativeHandle();
105 return nil;
106 }
107
108 - (NSNumber*)activeTabIndex {
109 // Note: applescript is 1-based, that is lists begin with index 1.
110 int activeTabIndex = browser_->selected_index() + 1;
111 if (!activeTabIndex) {
112 return nil;
113 }
114 return [NSNumber numberWithInt:activeTabIndex];
115 }
116
117 - (void)setActiveTabIndex:(NSNumber*)anActiveTabIndex {
118 // Note: applescript is 1-based, that is lists begin with index 1.
119 int atIndex = [anActiveTabIndex intValue] - 1;
120 if (atIndex >= 0 && atIndex < browser_->tab_count())
121 browser_->SelectTabContentsAt(atIndex, true);
122 else
123 AppleScript::SetError(AppleScript::errInvalidTabIndex);
124 }
125
126 - (NSString*)mode {
127 Profile* profile = browser_->profile();
128 if (profile->IsOffTheRecord())
129 return AppleScript::kIncognitoWindowMode;
130 return AppleScript::kNormalWindowMode;
131 }
132
133 - (void)setMode:(NSString*)theMode {
134 // cannot set mode after window is created.
135 if (theMode) {
136 AppleScript::SetError(AppleScript::errSetMode);
137 }
138 }
139
140 - (TabAppleScript*)activeTab {
141 TabAppleScript* currentTab = [[[TabAppleScript alloc]
142 initWithTabContent:browser_->GetSelectedTabContents()] autorelease];
143 [currentTab setContainer:self
144 property:AppleScript::kTabsProperty];
145 return currentTab;
146 }
147
148 - (NSArray*)tabs {
149 NSMutableArray* tabs = [NSMutableArray
150 arrayWithCapacity:browser_->tab_count()];
151
152 for (int i = 0; i < browser_->tab_count(); ++i) {
153 // Check to see if tab is closing.
154 if (browser_->GetTabContentsAt(i)->is_being_destroyed()) {
155 continue;
156 }
157
158 scoped_nsobject<TabAppleScript> tab(
159 [[TabAppleScript alloc]
160 initWithTabContent:(browser_->GetTabContentsAt(i))]);
161 [tab setContainer:self
162 property:AppleScript::kTabsProperty];
163 [tabs addObject:tab];
164 }
165 return tabs;
166 }
167
168 - (void)insertInTabs:(TabAppleScript*)aTab {
169 // This method gets called when a new tab is created so
170 // the container and property are set here.
171 [aTab setContainer:self
172 property:AppleScript::kTabsProperty];
173
174 // Set how long it takes a tab to be created.
175 base::TimeTicks newTabStartTime = base::TimeTicks::Now();
176 TabContents* tabContents = browser_->AddTabWithURL(
177 GURL(chrome::kChromeUINewTabURL),
178 GURL(),
179 PageTransition::TYPED,
180 -1, // To indicate tab is inserted at end.
181 TabStripModel::ADD_SELECTED,
182 NULL,
183 std::string());
184 tabContents->set_new_tab_start_time(newTabStartTime);
185
186 [aTab setTabContent:tabContents];
187 }
188
189 - (void)insertInTabs:(TabAppleScript*)aTab atIndex:(int)index {
190 // This method gets called when a new tab is created so
191 // the container and property are set here.
192 [aTab setContainer:self
193 property:AppleScript::kTabsProperty];
194
195 // Set how long it takes a tab to be created.
196 base::TimeTicks newTabStartTime = base::TimeTicks::Now();
197 TabContents* tabContents = browser_->AddTabWithURL(
198 GURL(chrome::kChromeUINewTabURL),
199 GURL(),
200 PageTransition::TYPED,
201 index,
202 TabStripModel::ADD_SELECTED,
203 NULL,
204 std::string());
205 tabContents->set_new_tab_start_time(newTabStartTime);
206
207 [aTab setTabContent:tabContents];
208 }
209
210 - (void)removeFromTabsAtIndex:(int)index {
211 browser_->tabstrip_model()->DetachTabContentsAt(index);
212 }
213
214 - (NSNumber*)orderedIndex{
215 return [NSNumber numberWithInt:[[self nativeHandle] orderedIndex]];
216 }
217
218 - (void)setOrderedIndex:(NSNumber*)anIndex {
219 int index = [anIndex intValue] - 1;
220 if (index < 0 || index >= (int)BrowserList::size()) {
221 AppleScript::SetError(AppleScript::errWrongIndex);
222 return;
223 }
224 [[self nativeHandle] setOrderedIndex:index];
225 }
226
227 - (NSComparisonResult)windowComparator:(WindowAppleScript*)otherWindow {
228 int thisIndex = [[self orderedIndex] intValue];
229 int otherIndex = [[otherWindow orderedIndex] intValue];
230 if (thisIndex < otherIndex)
231 return NSOrderedAscending;
232 else if (thisIndex > otherIndex)
233 return NSOrderedDescending;
234 // Indexes can never be same.
235 NOTREACHED();
236 return NSOrderedSame;
237 }
238
239 // Get and set values from the associated NSWindow.
240 - (id)valueForUndefinedKey:(NSString*)key {
241 return [[self nativeHandle] valueForKey:key];
242 }
243
244 - (void)setValue:(id)value forUndefinedKey:(NSString*)key {
245 [[self nativeHandle] setValue:(id)value forKey:key];
246 }
247
248 - (void)handlesCloseScriptCommand:(NSCloseCommand*)command {
249 // window() can be NULL during startup.
250 if (browser_->window())
251 browser_->window()->Close();
252 }
253
254 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/applescript/window_applescript.h ('k') | chrome/browser/cocoa/applescript/window_applescript_test.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698