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.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_iterator.h"
16 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"
17 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
18 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h"
19 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
21 @implementation BrowserCrApplication (AppleScriptAdditions)
23 - (NSArray*)appleScriptWindows {
24 NSMutableArray* appleScriptWindows = [NSMutableArray
25 arrayWithCapacity:chrome::GetTotalBrowserCount()];
26 // Iterate through all browsers and check if it closing,
27 // if not add it to list.
28 for (chrome::BrowserIterator browserIterator; !browserIterator.done();
29 browserIterator.Next()) {
30 if ((*browserIterator)->IsAttemptingToCloseBrowser())
33 base::scoped_nsobject<WindowAppleScript> window(
34 [[WindowAppleScript alloc] initWithBrowser:*browserIterator]);
35 [window setContainer:NSApp
36 property:AppleScript::kWindowsProperty];
37 [appleScriptWindows addObject:window];
39 // Windows sorted by their index value, which is obtained by calling
40 // orderedIndex: on each window.
41 [appleScriptWindows sortUsingSelector:@selector(windowComparator:)];
42 return appleScriptWindows;
45 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow {
46 // This method gets called when a new window is created so
47 // the container and property are set here.
48 [aWindow setContainer:self
49 property:AppleScript::kWindowsProperty];
52 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow
54 // This method gets called when a new window is created so
55 // the container and property are set here.
56 [aWindow setContainer:self
57 property:AppleScript::kWindowsProperty];
58 // Note: AppleScript is 1-based.
60 [aWindow setOrderedIndex:[NSNumber numberWithInt:index]];
63 - (void)removeFromAppleScriptWindowsAtIndex:(int)index {
64 [[[self appleScriptWindows] objectAtIndex:index]
65 handlesCloseScriptCommand:nil];
68 - (NSScriptObjectSpecifier*)objectSpecifier {
72 - (BookmarkFolderAppleScript*)otherBookmarks {
73 AppController* appDelegate = [NSApp delegate];
75 Profile* lastProfile = [appDelegate lastProfile];
77 AppleScript::SetError(AppleScript::errGetProfile);
81 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
82 if (!model->loaded()) {
83 AppleScript::SetError(AppleScript::errBookmarkModelLoad);
87 BookmarkFolderAppleScript* otherBookmarks =
88 [[[BookmarkFolderAppleScript alloc]
89 initWithBookmarkNode:model->other_node()] autorelease];
90 [otherBookmarks setContainer:self
91 property:AppleScript::kBookmarkFoldersProperty];
92 return otherBookmarks;
95 - (BookmarkFolderAppleScript*)bookmarksBar {
96 AppController* appDelegate = [NSApp delegate];
98 Profile* lastProfile = [appDelegate lastProfile];
100 AppleScript::SetError(AppleScript::errGetProfile);
104 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
105 if (!model->loaded()) {
106 AppleScript::SetError(AppleScript::errBookmarkModelLoad);
110 BookmarkFolderAppleScript* bookmarksBar =
111 [[[BookmarkFolderAppleScript alloc]
112 initWithBookmarkNode:model->bookmark_bar_node()] autorelease];
113 [bookmarksBar setContainer:self
114 property:AppleScript::kBookmarkFoldersProperty];
118 - (NSArray*)bookmarkFolders {
119 BookmarkFolderAppleScript* otherBookmarks = [self otherBookmarks];
120 BookmarkFolderAppleScript* bookmarksBar = [self bookmarksBar];
121 NSArray* folderArray = [NSArray arrayWithObjects:otherBookmarks,
127 - (void)insertInBookmarksFolders:(id)aBookmarkFolder {
131 - (void)insertInBookmarksFolders:(id)aBookmarkFolder atIndex:(int)index {
135 - (void)removeFromBookmarksFoldersAtIndex:(int)index {