123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- //
- // SKBookmark.m
- // Skim
- //
- // Created by Christiaan Hofman on 9/15/07.
- /*
- This software is Copyright (c) 2007-2018
- Christiaan Hofman. All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- - Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- - Neither the name of Christiaan Hofman nor the names of any
- contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #import "SKBookmark.h"
- #import "SKAlias.h"
- #import "NSImage_SKExtensions.h"
- //#import "NSDocument_SKExtensions.h"
- #import "SKDocumentController.h"
- //#import "NSURL_SKExtensions.h"
- //#import "SKMainDocument.h"
- //#import "SKMainWindowController.h"
- //#import "NSError_SKExtensions.h"
- //#import "NSShadow_SKExtensions.h"
- #define BOOKMARK_STRING @"bookmark"
- #define SESSION_STRING @"session"
- #define FOLDER_STRING @"folder"
- #define SEPARATOR_STRING @"separator"
- #define PROPERTIES_KEY @"properties"
- #define CHILDREN_KEY @"children"
- #define LABEL_KEY @"label"
- #define PAGEINDEX_KEY @"pageIndex"
- #define ALIASDATA_KEY @"_BDAlias"
- #define TYPE_KEY @"type"
- NSString *SKFolderDocumentType = @"public.folder";
- @interface SKFileBookmark : SKBookmark {
- SKAlias *alias;
- NSData *aliasData;
- NSString *label;
- NSUInteger pageIndex;
- NSDictionary *setup;
- }
- - (id)initWithAliasData:(NSData *)aData pageIndex:(NSUInteger)aPageIndex label:(NSString *)aLabel;
- - (SKAlias *)alias;
- - (NSData *)aliasData;
- @end
- @interface SKFolderBookmark : SKBookmark {
- NSString *label;
- NSMutableArray *children;
- }
- @end
- @interface SKRootBookmark : SKFolderBookmark
- @end
- @interface SKSessionBookmark : SKFolderBookmark
- @end
- @interface SKSeparatorBookmark : SKBookmark
- @end
- #pragma mark -
- @implementation SKBookmark
- + (id)bookmarkWithURL:(NSURL *)aURL pageIndex:(NSUInteger)aPageIndex label:(NSString *)aLabel {
- return [[self alloc] initWithURL:aURL pageIndex:aPageIndex label:aLabel];
- }
- + (id)bookmarkWithSetup:(NSDictionary *)aSetupDict label:(NSString *)aLabel {
- return [[self alloc] initWithSetup:aSetupDict label:aLabel];
- }
- + (id)bookmarkFolderWithLabel:(NSString *)aLabel {
- return [[self alloc] initFolderWithLabel:aLabel];
- }
- + (id)bookmarkSessionWithSetups:(NSArray *)aSetupDicts label:(NSString *)aLabel {
- return [[self alloc] initSessionWithSetups:aSetupDicts label:aLabel];
- }
- + (id)bookmarkSeparator {
- return [[self alloc] initSeparator];
- }
- + (NSArray *)bookmarksForURLs:(NSArray *)urls {
- NSFileManager *fm = [NSFileManager defaultManager];
- NSDocumentController *dc = [NSDocumentController sharedDocumentController];
- NSMutableArray *array = [NSMutableArray array];
-
- for (NSURL *url in urls) {
- NSString *fileType = [dc typeForContentsOfURL:url error:NULL];
- Class docClass;
- SKBookmark *bookmark;
- NSString *label = nil;
- [url getResourceValue:&label forKey:NSURLLocalizedNameKey error:NULL];
- if ([[NSWorkspace sharedWorkspace] type:fileType conformsToType:SKFolderDocumentType]) {
- NSArray *children = [self bookmarksForURLs:[fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:NULL]];
- if ([children count] && (bookmark = [[self alloc] initFolderWithChildren:children label:label])) {
- [array addObject:bookmark];
- }
- } else if ((docClass = [dc documentClassForType:fileType])) {
- if ((bookmark = [[self alloc] initWithURL:url pageIndex:0 label:label])) {
- [array addObject:bookmark];
- }
- }
- }
-
- return array;
- }
- - (id)initWithURL:(NSURL *)aURL pageIndex:(NSUInteger)aPageIndex label:(NSString *)aLabel {
- return [[SKFileBookmark alloc] initWithURL:aURL pageIndex:aPageIndex label:aLabel];
- }
- - (id)initWithSetup:(NSDictionary *)aSetupDict label:(NSString *)aLabel {
- return [[SKFileBookmark alloc] initWithSetup:aSetupDict label:aLabel];
- }
- - (id)initFolderWithChildren:(NSArray *)aChildren label:(NSString *)aLabel {
- return [[SKFolderBookmark alloc] initFolderWithChildren:aChildren label:aLabel];
- }
- - (id)initFolderWithLabel:(NSString *)aLabel {
- return [self initFolderWithChildren:nil label:aLabel];
- }
- - (id)initRootWithChildrenProperties:(NSArray *)childrenProperties {
- NSMutableArray *aChildren = [NSMutableArray array];
- SKBookmark *child;
- for (NSDictionary *dict in childrenProperties) {
- if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
- [aChildren addObject:child];
- }
- }
- return [[SKRootBookmark alloc] initFolderWithChildren:aChildren label:NSLocalizedString(@"Bookmarks Menu", @"Menu item title")];
- }
- - (id)initSessionWithSetups:(NSArray *)aSetupDicts label:(NSString *)aLabel {
- NSMutableArray *aChildren = [NSMutableArray array];
- SKBookmark *child;
- for (NSDictionary *setup in aSetupDicts) {
- if ((child = [[SKBookmark alloc] initWithSetup:setup label:@""])) {
- [aChildren addObject:child];
- }
- }
- return [[SKSessionBookmark alloc] initFolderWithChildren:aChildren label:aLabel];
- }
- - (id)initSeparator {
- return [[SKSeparatorBookmark alloc] init];
- }
- - (id)initWithProperties:(NSDictionary *)dictionary {
- NSString *type = [dictionary objectForKey:TYPE_KEY];
- if ([type isEqualToString:SEPARATOR_STRING]) {
- return (id)[[SKSeparatorBookmark alloc] init];
- } else if ([type isEqualToString:FOLDER_STRING] || [type isEqualToString:SESSION_STRING]) {
- Class bookmarkClass = [type isEqualToString:FOLDER_STRING] ? [SKFolderBookmark class] : [SKSessionBookmark class];
- NSMutableArray *newChildren = [NSMutableArray array];
- SKBookmark *child;
- for (NSDictionary *dict in [dictionary objectForKey:CHILDREN_KEY]) {
- if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
- [newChildren addObject:child];
- } else
- NSLog(@"Failed to read child bookmark: %@", dict);
- }
- return (id)[[bookmarkClass alloc] initFolderWithChildren:newChildren label:[dictionary objectForKey:LABEL_KEY]];
- } else if ([dictionary objectForKey:@"windowFrame"]) {
- return (id)[[SKFileBookmark alloc] initWithSetup:dictionary label:[dictionary objectForKey:LABEL_KEY]];
- } else {
- NSNumber *pageIndex = [dictionary objectForKey:PAGEINDEX_KEY];
- return (id)[[SKFileBookmark alloc] initWithAliasData:[dictionary objectForKey:ALIASDATA_KEY] pageIndex:(pageIndex ? [pageIndex unsignedIntegerValue] : NSNotFound) label:[dictionary objectForKey:LABEL_KEY]];
- }
- }
- - (id)copyWithZone:(NSZone *)aZone {
- return [[SKBookmark alloc] initWithProperties:[self properties]];
- }
- - (void)dealloc {
- parent = nil;
- }
- - (NSDictionary *)properties { return nil; }
- - (SKBookmarkType)bookmarkType { return SKBookmarkTypeSeparator; }
- - (NSImage *)icon { return nil; }
- - (NSImage *)alternateIcon { return [self icon]; }
- - (NSString *)label { return nil; }
- - (void)setLabel:(NSString *)newLabel {}
- - (NSURL *)fileURL { return nil; }
- - (NSUInteger)pageIndex { return NSNotFound; }
- - (void)setPageIndex:(NSUInteger)newPageIndex {}
- - (NSNumber *)pageNumber { return nil; }
- - (void)setPageNumber:(NSNumber *)newPageNumber {}
- - (NSURL *)previewItemURL { return [self fileURL]; }
- - (NSString *)previewItemTitle { return [self label]; }
- - (NSArray *)children { return nil; }
- - (NSUInteger)countOfChildren { return 0; }
- - (SKBookmark *)objectInChildrenAtIndex:(NSUInteger)anIndex { return nil; }
- - (void)insertObject:(SKBookmark *)child inChildrenAtIndex:(NSUInteger)anIndex {}
- - (void)removeObjectFromChildrenAtIndex:(NSUInteger)anIndex {}
- - (id)objectSpecifier {
- NSUInteger idx = [[parent children] indexOfObjectIdenticalTo:self];
- if (idx != NSNotFound) {
- NSScriptObjectSpecifier *containerRef = nil;
- NSScriptClassDescription *containerClassDescription = nil;
- if ([parent parent]) {
- containerRef = [parent objectSpecifier];
- containerClassDescription = [containerRef keyClassDescription];
- } else {
- containerClassDescription = [NSScriptClassDescription classDescriptionForClass:[NSApp class]];
- }
- return [[NSIndexSpecifier allocWithZone:CFBridgingRetain(self)] initWithContainerClassDescription:containerClassDescription containerSpecifier:containerRef key:@"bookmarks" index:idx];
- } else {
- return nil;
- }
- }
- - (SKBookmark *)scriptingParent {
- return [parent parent] == nil ? nil : parent;
- }
- - (NSArray *)entireContents { return nil; }
- - (NSArray *)bookmarks {
- return [self children];
- }
- - (void)insertObject:(SKBookmark *)bookmark inBookmarksAtIndex:(NSUInteger)anIndex {
- [self insertObject:bookmark inChildrenAtIndex:anIndex];
- }
- - (void)removeObjectFromBookmarksAtIndex:(NSUInteger)anIndex {
- [self removeObjectFromChildrenAtIndex:anIndex];
- }
- - (id)newScriptingObjectOfClass:(Class)objectClass forValueForKey:(NSString *)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties {
- if ([key isEqualToString:@"bookmarks"]) {
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSReceiversCantHandleCommandScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"Invalid container for new bookmark."];
- return nil;
- }
- return [super newScriptingObjectOfClass:objectClass forValueForKey:key withContentsValue:contentsValue properties:properties];
- }
- - (BOOL)isDescendantOf:(SKBookmark *)bookmark {
- if (self == bookmark)
- return YES;
- for (SKBookmark *child in [bookmark children]) {
- if ([self isDescendantOf:child])
- return YES;
- }
- return NO;
- }
- - (BOOL)isDescendantOfArray:(NSArray *)bookmarks {
- for (SKBookmark *bm in bookmarks) {
- if ([self isDescendantOf:bm]) return YES;
- }
- return NO;
- }
- - (void)open {}
- @end
- #pragma mark -
- @implementation SKFileBookmark
- + (NSImage *)missingFileImage {
- static NSImage *image = nil;
- if (image == nil) {
- image = [NSImage imageWithSize:NSMakeSize(16.0, 16.0) drawingHandler:^(NSRect rect) {
- NSImage *genericDocImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericDocumentIcon)];
- NSImage *questionMark = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kQuestionMarkIcon)];
- [genericDocImage drawInRect:rect fromRect:NSZeroRect operation:NSCompositeCopy fraction:0.7];
- [questionMark drawInRect:NSMakeRect(3.0, 2.0, 10.0, 10.0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.7];
- return YES;
- }];
- }
- return image;
- }
- - (id)initWithURL:(NSURL *)aURL pageIndex:(NSUInteger)aPageIndex label:(NSString *)aLabel {
- self = [super init];
- if (self) {
- alias = [[SKAlias alloc] initWithURL:aURL];
- if (alias) {
- aliasData = [alias data];
- pageIndex = aPageIndex;
- label = [aLabel copy];
- setup = nil;
- } else {
- self = nil;
- }
- }
- return self;
- }
- - (id)initWithAliasData:(NSData *)aData pageIndex:(NSUInteger)aPageIndex label:(NSString *)aLabel {
- self = [super init];
- if (self) {
- alias = [[SKAlias alloc] initWithData:aData];
- if (aData && alias) {
- aliasData = aData;
- pageIndex = aPageIndex;
- label = [aLabel copy];
- setup = nil;
- } else {
- self = nil;
- }
- }
- return self;
- }
- - (id)initWithSetup:(NSDictionary *)aSetupDict label:(NSString *)aLabel {
- NSNumber *pageIndexNumber = [aSetupDict objectForKey:PAGEINDEX_KEY];
- self = [self initWithAliasData:[aSetupDict objectForKey:ALIASDATA_KEY] pageIndex:(pageIndexNumber ? [pageIndexNumber unsignedIntegerValue] : NSNotFound) label:aLabel];
- if (self) {
- setup = [aSetupDict copy];
- }
- return self;
- }
- - (void)dealloc {
- // SKDESTROY(alias);
- // SKDESTROY(aliasData);
- // SKDESTROY(label);
- // SKDESTROY(setup);
- // [super dealloc];
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: label=%@, path=%@, page=%lu>", [self class], label, [[self fileURL] path], (unsigned long)pageIndex];
- }
- - (NSDictionary *)properties {
- NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:setup];
- [properties addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:BOOKMARK_STRING, TYPE_KEY, [self aliasData], ALIASDATA_KEY, [NSNumber numberWithUnsignedInteger:pageIndex], PAGEINDEX_KEY, label, LABEL_KEY, nil]];
- return properties;
- }
- - (SKBookmarkType)bookmarkType {
- return SKBookmarkTypeBookmark;
- }
- - (NSURL *)fileURL {
- return [alias fileURLNoUI];
- }
- - (SKAlias *)alias {
- return alias;
- }
- - (NSData *)aliasData {
- NSData *data = nil;
- if ([self fileURL])
- data = [alias data];
- return data ?: aliasData;
- }
- - (NSImage *)icon {
- NSURL *fileURL = [self fileURL];
- return fileURL ? [[NSWorkspace sharedWorkspace] iconForFile:[fileURL path]] : [[self class] missingFileImage];
- }
- - (NSUInteger)pageIndex {
- return pageIndex;
- }
- - (void)setPageIndex:(NSUInteger)newPageIndex { pageIndex = newPageIndex; }
- - (NSNumber *)pageNumber {
- return pageIndex == NSNotFound ? nil : [NSNumber numberWithUnsignedInteger:pageIndex + 1];
- }
- - (void)setPageNumber:(NSNumber *)newPageNumber {
- NSUInteger newNumber = [newPageNumber unsignedIntegerValue];
- if (newNumber > 0)
- [self setPageIndex:newNumber - 1];
- }
- - (NSString *)label {
- NSString *theLabel = label;
- if ([theLabel length] == 0)
- [[self fileURL] getResourceValue:&theLabel forKey:NSURLLocalizedNameKey error:NULL];
- return theLabel ?: @"";
- }
- - (void)setLabel:(NSString *)newLabel {
- if (label != newLabel) {
- label = newLabel;
- }
- }
- - (void)open {
- id document = nil;
- NSError *error = nil;
- if (setup) {
- document = [[NSDocumentController sharedDocumentController] openDocumentWithSetup:[self properties] error:&error];
- } else {
- // // we allow UI when resolving alias for opening the bookmark, so don't use -fileURL, also consistent with openDocumentWithSetup:error:
- // NSURL *fileURL = [alias fileURL];
- // if (fileURL && NO == [fileURL isTrashedFileURL] &&
- // (document = [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:fileURL display:YES error:&error]) &&
- // [document isPDFDocument] && [self pageIndex] != NSNotFound)
- // [[document mainWindowController] setPageNumber:[self pageIndex] + 1];
- }
- // if (document == nil && error && [error isUserCancelledError] == NO)
- // [NSApp presentError:error];
- }
- @end
- #pragma mark -
- @implementation SKFolderBookmark
- - (id)initFolderWithChildren:(NSArray *)aChildren label:(NSString *)aLabel {
- self = [super init];
- if (self) {
- label = [aLabel copy];
- children = [[NSMutableArray alloc] initWithArray:aChildren];
- [children makeObjectsPerformSelector:@selector(setParent:) withObject:self];
- }
- return self;
- }
- - (void)dealloc {
- // SKDESTROY(label);
- // SKDESTROY(children);
- // [super dealloc];
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: label=%@, children=%@>", [self class], label, children];
- }
- - (NSDictionary *)properties {
- return [NSDictionary dictionaryWithObjectsAndKeys:FOLDER_STRING, TYPE_KEY, [children valueForKey:PROPERTIES_KEY], CHILDREN_KEY, label, LABEL_KEY, nil];
- }
- - (SKBookmarkType)bookmarkType {
- return SKBookmarkTypeFolder;
- }
- - (NSImage *)icon {
- return [NSImage imageNamed:NSImageNameFolder];
- }
- - (NSImage *)alternateIcon {
- return [NSImage imageNamed:NSImageNameMultipleDocuments];
- }
- - (NSString *)label {
- return label ?: @"";
- }
- - (void)setLabel:(NSString *)newLabel {
- if (label != newLabel) {
- label = newLabel;
- }
- }
- - (NSArray *)children {
- return [children copy];
- }
- - (NSUInteger)countOfChildren {
- return [children count];
- }
- - (SKBookmark *)objectInChildrenAtIndex:(NSUInteger)anIndex {
- return [children objectAtIndex:anIndex];
- }
- - (void)insertObject:(SKBookmark *)child inChildrenAtIndex:(NSUInteger)anIndex {
- [children insertObject:child atIndex:anIndex];
- [child setParent:self];
- }
- - (void)removeObjectFromChildrenAtIndex:(NSUInteger)anIndex {
- [[children objectAtIndex:anIndex] setParent:nil];
- [children removeObjectAtIndex:anIndex];
- }
- - (NSArray *)entireContents {
- NSMutableArray *contents = [NSMutableArray array];
- for (SKBookmark *bookmark in [self children]) {
- [contents addObject:bookmark];
- [contents addObjectsFromArray:[bookmark entireContents]];
- }
- return contents;
- }
- - (id)newScriptingObjectOfClass:(Class)objectClass forValueForKey:(NSString *)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties {
- if ([key isEqualToString:@"bookmarks"]) {
- SKBookmark *bookmark = nil;
- NSURL *aURL = [properties objectForKey:@"scriptingFile"] ?: contentsValue;
- NSString *aLabel = [properties objectForKey:@"label"];
- NSNumber *aType = [properties objectForKey:@"bookmarkType"];
- NSInteger type;
- if ([aType respondsToSelector:@selector(integerValue)])
- type = [aType integerValue];
- else if (aURL == nil)
- type = SKBookmarkTypeSession;
- else if ([[NSWorkspace sharedWorkspace] type:[[NSWorkspace sharedWorkspace] typeOfFile:[aURL path] error:NULL] conformsToType:(NSString *)kUTTypeFolder])
- type = SKBookmarkTypeFolder;
- else
- type = SKBookmarkTypeBookmark;
- switch (type) {
- case SKBookmarkTypeBookmark:
- {
- Class docClass;
- if (aURL == nil) {
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSRequiredArgumentsMissingScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"New file bookmark requires a file."];
- } else if ([aURL checkResourceIsReachableAndReturnError:NULL] == NO) {
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSArgumentsWrongScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"New file bookmark requires an existing file."];
- } else if ((docClass = [[NSDocumentController sharedDocumentController] documentClassForContentsOfURL:aURL])) {
- NSUInteger aPageNumber = [[properties objectForKey:@"pageNumber"] unsignedIntegerValue];
- if (aPageNumber > 0)
- aPageNumber--;
- // else
- // aPageNumber = [docClass isPDFDocument] ? 0 : NSNotFound;
- if (aLabel == nil)
- [aURL getResourceValue:&aLabel forKey:NSURLLocalizedNameKey error:NULL];
- bookmark = [[SKBookmark alloc] initWithURL:aURL pageIndex:aPageNumber label:aLabel ?: @""];
- } else {
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSArgumentsWrongScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"Unsupported file type for new bookmark."];
- }
- break;
- }
- case SKBookmarkTypeFolder:
- {
- NSArray *aChildren = nil;
- if (aURL) {
- aChildren = [SKBookmark bookmarksForURLs:[[NSFileManager defaultManager] contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:NULL]];
- if (aLabel == nil)
- [aURL getResourceValue:&aLabel forKey:NSURLLocalizedNameKey error:NULL];
- }
- bookmark = [[SKBookmark alloc] initFolderWithChildren:aChildren label:aLabel ?: @""];
- break;
- }
- case SKBookmarkTypeSession:
- {
- NSArray *setups = [[NSApp orderedDocuments] valueForKey:@"currentDocumentSetup"];
- bookmark = [[SKBookmark alloc] initSessionWithSetups:setups label:aLabel ?: @""];
- break;
- }
- case SKBookmarkTypeSeparator:
- bookmark = [[SKBookmark alloc] initSeparator];
- break;
- default:
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSArgumentsWrongScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"New bookmark requires a supported bookmark type."];
- break;
- }
- return bookmark;
- }
- return [super newScriptingObjectOfClass:objectClass forValueForKey:key withContentsValue:contentsValue properties:properties];
- }
- - (void)open {
- NSInteger i = [children count];
- while (i--)
- [[children objectAtIndex:i] open];
- }
- @end
- #pragma mark -
- @implementation SKRootBookmark
- - (NSImage *)icon {
- static NSImage *menuIcon = nil;
- if (menuIcon == nil) {
- menuIcon = [NSImage imageWithSize:NSMakeSize(16.0, 16.0) drawingHandler:^(NSRect rect){
- [[NSColor colorWithCalibratedWhite:0.0 alpha:0.2] set];
- [NSBezierPath fillRect:NSMakeRect(1.0, 1.0, 14.0, 13.0)];
- [NSGraphicsContext saveGraphicsState];
- NSBezierPath *path = [NSBezierPath bezierPath];
- [path moveToPoint:NSMakePoint(2.0, 2.0)];
- [path lineToPoint:NSMakePoint(2.0, 15.0)];
- [path lineToPoint:NSMakePoint(7.0, 15.0)];
- [path lineToPoint:NSMakePoint(7.0, 13.0)];
- [path lineToPoint:NSMakePoint(14.0, 13.0)];
- [path lineToPoint:NSMakePoint(14.0, 2.0)];
- [path closePath];
- [[NSColor whiteColor] set];
- [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.33333] blurRadius:2.0 yOffset:-1.0];
- [path fill];
- [NSGraphicsContext restoreGraphicsState];
- [[NSColor colorWithCalibratedRed:0.162 green:0.304 blue:0.755 alpha:1.0] set];
- NSRectFill(NSMakeRect(2.0, 13.0, 5.0, 2.0));
- [[NSColor colorWithCalibratedRed:0.894 green:0.396 blue:0.202 alpha:1.0] set];
- NSRectFill(NSMakeRect(3.0, 4.0, 1.0, 1.0));
- NSRectFill(NSMakeRect(3.0, 7.0, 1.0, 1.0));
- NSRectFill(NSMakeRect(3.0, 10.0, 1.0, 1.0));
- [[NSColor colorWithCalibratedWhite:0.6 alpha:1.0] set];
- NSRectFill(NSMakeRect(5.0, 4.0, 1.0, 1.0));
- NSRectFill(NSMakeRect(5.0, 7.0, 1.0, 1.0));
- NSRectFill(NSMakeRect(5.0, 10.0, 1.0, 1.0));
- NSUInteger i, j;
- for (i = 0; i < 7; i++) {
- for (j = 0; j < 3; j++) {
- [[NSColor colorWithCalibratedWhite:0.45 + 0.1 * rand() / RAND_MAX alpha:1.0] set];
- NSRectFill(NSMakeRect(6.0 + i, 4.0 + 3.0 * j, 1.0, 1.0));
- }
- }
- NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.1] endingColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.0]];
- [gradient drawInRect:NSMakeRect(2.0, 2.0, 12.0,11.0) angle:90.0];
- return YES;
- }];
- }
- return menuIcon;
- }
- @end
- #pragma mark -
- @implementation SKSessionBookmark
- - (NSDictionary *)properties {
- return [NSDictionary dictionaryWithObjectsAndKeys:SESSION_STRING, TYPE_KEY, [children valueForKey:PROPERTIES_KEY], CHILDREN_KEY, label, LABEL_KEY, nil];
- }
- - (SKBookmarkType)bookmarkType {
- return SKBookmarkTypeSession;
- }
- - (NSImage *)icon {
- return [NSImage imageNamed:NSImageNameMultipleDocuments];
- }
- - (NSImage *)alternateIcon {
- return [NSImage imageNamed:NSImageNameFolder];
- }
- - (void)insertObject:(SKBookmark *)child inChildrenAtIndex:(NSUInteger)anIndex {}
- - (void)removeObjectFromChildrenAtIndex:(NSUInteger)anIndex {}
- - (NSArray *)entireContents { return nil; }
- - (id)newScriptingObjectOfClass:(Class)objectClass forValueForKey:(NSString *)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties {
- if ([key isEqualToString:@"bookmarks"]) {
- [[NSScriptCommand currentCommand] setScriptErrorNumber:NSReceiversCantHandleCommandScriptError];
- [[NSScriptCommand currentCommand] setScriptErrorString:@"Invalid container for new bookmark."];
- return nil;
- }
- return [super newScriptingObjectOfClass:objectClass forValueForKey:key withContentsValue:contentsValue properties:properties];
- }
- @end
- #pragma mark -
- @implementation SKSeparatorBookmark
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: separator>", [self class]];
- }
- - (NSDictionary *)properties {
- return [NSDictionary dictionaryWithObjectsAndKeys:SEPARATOR_STRING, TYPE_KEY, nil];
- }
- - (SKBookmarkType)bookmarkType {
- return SKBookmarkTypeSeparator;
- }
- @end
|