1 // Copyright (c) 2012 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.
5 #import "chrome/browser/ui/cocoa/applescript/browsercrapplication+applescript.h"
7 #include "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
9 #import "chrome/browser/app_controller_mac.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_iterator.h"
15 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"
16 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
17 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h"
18 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
19 #include "components/bookmarks/browser/bookmark_model.h"
21 using bookmarks::BookmarkModel;
23 @implementation BrowserCrApplication (AppleScriptAdditions)
25 - (NSArray*)appleScriptWindows {
26 NSMutableArray* appleScriptWindows = [NSMutableArray
27 arrayWithCapacity:chrome::GetTotalBrowserCount()];
28 // Iterate through all browsers and check if it closing,
29 // if not add it to list.
30 for (chrome::BrowserIterator browserIterator; !browserIterator.done();
31 browserIterator.Next()) {
32 if ((*browserIterator)->IsAttemptingToCloseBrowser())
35 base::scoped_nsobject<WindowAppleScript> window(
36 [[WindowAppleScript alloc] initWithBrowser:*browserIterator]);
37 [window setContainer:NSApp
38 property:AppleScript::kWindowsProperty];
39 [appleScriptWindows addObject:window];
41 // Windows sorted by their index value, which is obtained by calling
42 // orderedIndex: on each window.
43 [appleScriptWindows sortUsingSelector:@selector(windowComparator:)];
44 return appleScriptWindows;
47 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow {
48 // This method gets called when a new window is created so
49 // the container and property are set here.
50 [aWindow setContainer:self
51 property:AppleScript::kWindowsProperty];
54 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow
56 // This method gets called when a new window is created so
57 // the container and property are set here.
58 [aWindow setContainer:self
59 property:AppleScript::kWindowsProperty];
60 // Note: AppleScript is 1-based.
62 [aWindow setOrderedIndex:[NSNumber numberWithInt:index]];
65 - (void)removeFromAppleScriptWindowsAtIndex:(int)index {
66 [[[self appleScriptWindows] objectAtIndex:index]
67 handlesCloseScriptCommand:nil];
70 - (NSScriptObjectSpecifier*)objectSpecifier {
74 - (BookmarkFolderAppleScript*)otherBookmarks {
75 AppController* appDelegate = [NSApp delegate];
77 Profile* lastProfile = [appDelegate lastProfile];
79 AppleScript::SetError(AppleScript::errGetProfile);
83 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
84 if (!model->loaded()) {
85 AppleScript::SetError(AppleScript::errBookmarkModelLoad);
89 BookmarkFolderAppleScript* otherBookmarks =
90 [[[BookmarkFolderAppleScript alloc]
91 initWithBookmarkNode:model->other_node()] autorelease];
92 [otherBookmarks setContainer:self
93 property:AppleScript::kBookmarkFoldersProperty];
94 return otherBookmarks;
97 - (BookmarkFolderAppleScript*)bookmarksBar {
98 AppController* appDelegate = [NSApp delegate];
100 Profile* lastProfile = [appDelegate lastProfile];
102 AppleScript::SetError(AppleScript::errGetProfile);
106 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
107 if (!model->loaded()) {
108 AppleScript::SetError(AppleScript::errBookmarkModelLoad);
112 BookmarkFolderAppleScript* bookmarksBar =
113 [[[BookmarkFolderAppleScript alloc]
114 initWithBookmarkNode:model->bookmark_bar_node()] autorelease];
115 [bookmarksBar setContainer:self
116 property:AppleScript::kBookmarkFoldersProperty];
120 - (NSArray*)bookmarkFolders {
121 BookmarkFolderAppleScript* otherBookmarks = [self otherBookmarks];
122 BookmarkFolderAppleScript* bookmarksBar = [self bookmarksBar];
123 NSArray* folderArray = [NSArray arrayWithObjects:otherBookmarks,
129 - (void)insertInBookmarksFolders:(id)aBookmarkFolder {
133 - (void)insertInBookmarksFolders:(id)aBookmarkFolder atIndex:(int)index {
137 - (void)removeFromBookmarksFoldersAtIndex:(int)index {