1 // Copyright (c) 2011 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/ui/cocoa/applescript/browsercrapplication+applescript.h" 6 7 #include "base/logging.h" 8 #import "base/memory/scoped_nsobject.h" 9 #import "chrome/browser/app_controller_mac.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/ui/browser_list.h" 13 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h" 14 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h" 15 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h" 16 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h" 17 18 @implementation BrowserCrApplication (AppleScriptAdditions) 19 20 - (NSArray*)appleScriptWindows { 21 NSMutableArray* appleScriptWindows = [NSMutableArray 22 arrayWithCapacity:BrowserList::size()]; 23 // Iterate through all browsers and check if it closing, 24 // if not add it to list. 25 for (BrowserList::const_iterator browserIterator = BrowserList::begin(); 26 browserIterator != BrowserList::end(); ++browserIterator) { 27 if ((*browserIterator)->IsAttemptingToCloseBrowser()) 28 continue; 29 30 scoped_nsobject<WindowAppleScript> window( 31 [[WindowAppleScript alloc] initWithBrowser:*browserIterator]); 32 [window setContainer:NSApp 33 property:AppleScript::kWindowsProperty]; 34 [appleScriptWindows addObject:window]; 35 } 36 // Windows sorted by their index value, which is obtained by calling 37 // orderedIndex: on each window. 38 [appleScriptWindows sortUsingSelector:@selector(windowComparator:)]; 39 return appleScriptWindows; 40 } 41 42 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow { 43 // This method gets called when a new window is created so 44 // the container and property are set here. 45 [aWindow setContainer:self 46 property:AppleScript::kWindowsProperty]; 47 } 48 49 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow 50 atIndex:(int)index { 51 // This method gets called when a new window is created so 52 // the container and property are set here. 53 [aWindow setContainer:self 54 property:AppleScript::kWindowsProperty]; 55 // Note: AppleScript is 1-based. 56 index--; 57 [aWindow setOrderedIndex:[NSNumber numberWithInt:index]]; 58 } 59 60 - (void)removeFromAppleScriptWindowsAtIndex:(int)index { 61 [[[self appleScriptWindows] objectAtIndex:index] 62 handlesCloseScriptCommand:nil]; 63 } 64 65 - (NSScriptObjectSpecifier*)objectSpecifier { 66 return nil; 67 } 68 69 - (BookmarkFolderAppleScript*)otherBookmarks { 70 AppController* appDelegate = [NSApp delegate]; 71 72 Profile* defaultProfile = [appDelegate defaultProfile]; 73 if (!defaultProfile) { 74 AppleScript::SetError(AppleScript::errGetProfile); 75 return nil; 76 } 77 78 BookmarkModel* model = defaultProfile->GetBookmarkModel(); 79 if (!model->IsLoaded()) { 80 AppleScript::SetError(AppleScript::errBookmarkModelLoad); 81 return nil; 82 } 83 84 BookmarkFolderAppleScript* otherBookmarks = 85 [[[BookmarkFolderAppleScript alloc] 86 initWithBookmarkNode:model->other_node()] autorelease]; 87 [otherBookmarks setContainer:self 88 property:AppleScript::kBookmarkFoldersProperty]; 89 return otherBookmarks; 90 } 91 92 - (BookmarkFolderAppleScript*)bookmarksBar { 93 AppController* appDelegate = [NSApp delegate]; 94 95 Profile* defaultProfile = [appDelegate defaultProfile]; 96 if (!defaultProfile) { 97 AppleScript::SetError(AppleScript::errGetProfile); 98 return nil; 99 } 100 101 BookmarkModel* model = defaultProfile->GetBookmarkModel(); 102 if (!model->IsLoaded()) { 103 AppleScript::SetError(AppleScript::errBookmarkModelLoad); 104 return NULL; 105 } 106 107 BookmarkFolderAppleScript* bookmarksBar = 108 [[[BookmarkFolderAppleScript alloc] 109 initWithBookmarkNode:model->GetBookmarkBarNode()] autorelease]; 110 [bookmarksBar setContainer:self 111 property:AppleScript::kBookmarkFoldersProperty]; 112 return bookmarksBar; 113 } 114 115 - (NSArray*)bookmarkFolders { 116 BookmarkFolderAppleScript* otherBookmarks = [self otherBookmarks]; 117 BookmarkFolderAppleScript* bookmarksBar = [self bookmarksBar]; 118 NSArray* folderArray = [NSArray arrayWithObjects:otherBookmarks, 119 bookmarksBar, 120 nil]; 121 return folderArray; 122 } 123 124 - (void)insertInBookmarksFolders:(id)aBookmarkFolder { 125 NOTIMPLEMENTED(); 126 } 127 128 - (void)insertInBookmarksFolders:(id)aBookmarkFolder atIndex:(int)index { 129 NOTIMPLEMENTED(); 130 } 131 132 - (void)removeFromBookmarksFoldersAtIndex:(int)index { 133 NOTIMPLEMENTED(); 134 } 135 136 @end 137