这段时间一直在看Mac开发相关的书籍,相对于iOS来说,市面上写的比较好的数据少之又少,《macOSObjc》算是找到的写的比较好的了,但是大部分都是介绍控件的使用。与之相比的《Learn Objective-C on the Mac》写的很详细,不过只有英文版的,看了之后受益匪浅。
下面是看书时总结的笔记,接下来会持续更新。
1、Mac下获取电脑Application icon、name、bundle url
NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSSystemDomainMask, YES).firstObject;
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:path] includingPropertiesForKeys:@[NSURLLocalizedNameKey, NSURLIsApplicationKey, NSURLEffectiveIconKey] options:NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFiles errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
if(error) {
NSLog(@"error: %@", error.description);
}
return YES;
}];
for(NSURL *applicationURL in enumerator) {
NSImage *applicationImage = nil;
NSError *error;
[applicationURL getResourceValue:&applicationImage forKey:NSURLEffectiveIconKey error:&error];
NSString *applicationName;
[applicationURL getResourceValue:&applicationName forKey:NSURLLocalizedNameKey error:&error];
if(error) {
NSLog(@"error ---> %@", error.description);
return;
}
if(applicationName && applicationImage) {
ApplicationModel *model = [[ApplicationModel alloc] init];
model.image = applicationImage;
model.name = applicationName;
model.url = applicationURL;
[self.dataArray addObject:model];
}
}
NSLog(@"array - > %@", self.dataArray);
2、Mac下打开应用程序的代码
[[NSWorkspace sharedWorkspace] openURL:url];
3、获取Mac下所有在运行的app的代码
@property (nonatomic, strong) NSMutableArray *dataArr;
NSArray <NSRunningApplication *> *apps = [[NSWorkspace sharedWorkspace] runningApplications];
for(NSRunningApplication *app in apps) {
[self.dataArr addObject:app.localizedName];
}
[self.tableView reloadData];
4、监听Mac上所有已经运行的app
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidRunning:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
5、kill进程代码
- (void)tableViewDoubleClick:(NSTableView *)tableView {
NSInteger selectedIndex = [tableView selectedRow];
ApplicationModel *model = self.dataArr[selectedIndex];
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleWarning;
alert.messageText = @"温馨提示";
alert.informativeText = @"确定要杀死该进城吗";
[alert addButtonWithTitle:@"确定"];
[alert addButtonWithTitle:@"取消"];
__weak typeof(self) weakSelf = self;
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
if(returnCode == 1000) {
kill(model.pid, SIGKILL);
[weakSelf.dataArr removeObject:model];
[weakSelf.tableView reloadData];
}
}];
}
6、NSTableView 双击事件
[self.tableView setDoubleAction:@selector(tableViewDoubleClick:)];
7、长按拖动NSTableViewCell,首先注册tableview
[self.tableView registerForDraggedTypes:[NSArray arrayWithObject:CustomTableViewCellDrap]];
实现tableview拖动的delegate
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:CustomTableViewCellDrap] owner:self];
[pboard setData:data forType:CustomTableViewCellDrap];
self.originIndex = rowIndexes.firstIndex;
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation {
return NSDragOperationMove;
}
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation {
id obj = self.dataArr[self.originIndex];
[self.dataArr insertObject:obj atIndex:row];
if(self.originIndex < row) {
[self.dataArr removeObjectAtIndex:self.originIndex];
}
else {
[self.dataArr removeObjectAtIndex:self.originIndex + 1];
}
return YES;
}
8、拖动NSCollectionViewCell,首先注册拖动事件
[self.collection registerForDraggedTypes:@[NSPasteboardTypeString]];
[self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:yearMask];
[self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
其次实现拖动代理函数
@property (nonatomic, assign) NSIndexPath *currentIndex;
- (void)collectionView:(NSCollectionView *)collectionView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
self.currentIndex = indexPaths.allObjects.firstObject;
}
- (id<NSPasteboardWriting>)collectionView:(NSCollectionView *)collectionView pasteboardWriterForItemAtIndex:(NSUInteger)index {
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
ApplicationModel *model = self.dataArr[index];
NSString *indexString = [NSString stringWithFormat:@"%@", model.path];
[item setString:indexString forType:NSPasteboardTypeString];
return item;
}
- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation {
return YES;
}
- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndexPath:(NSIndexPath *__autoreleasing _Nonnull *)proposedDropIndexPath dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation {
if(*proposedDropOperation == NSCollectionViewDropBefore) {
*proposedDropOperation = NSCollectionViewDropOn;
return NSDragOperationNone;
}
[collectionView moveItemAtIndexPath:self.currentIndex toIndexPath:*proposedDropIndexPath];
return NSDragOperationMove;
}
网友评论