Mac application development notes

這段時(shí)間一直在看Mac開發(fā)相關(guān)的書籍,相對(duì)于iOS來說,市面上寫的比較好的數(shù)據(jù)少之又少变汪,《macOSObjc》算是找到的寫的比較好的了,但是大部分都是介紹控件的使用蚁趁。與之相比的《Learn Objective-C on the Mac》寫的很詳細(xì)裙盾,不過只有英文版的,看了之后受益匪淺他嫡。

下面是看書時(shí)總結(jié)的筆記番官,接下來會(huì)持續(xù)更新。
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下打開應(yīng)用程序的代碼

[[NSWorkspace sharedWorkspace] openURL:url];

3近顷、獲取Mac下所有在運(yùn)行的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、監(jiān)聽Mac上所有已經(jīng)運(yùn)行的app

  [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidRunning:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];

5宁否、kill進(jìn)程代碼

- (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 = @"確定要?dú)⑺涝撨M(jìn)城嗎";
    [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、長(zhǎng)按拖動(dòng)NSTableViewCell慕匠,首先注冊(cè)tableview

    [self.tableView registerForDraggedTypes:[NSArray arrayWithObject:CustomTableViewCellDrap]];

實(shí)現(xiàn)tableview拖動(dòng)的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饱须、拖動(dòng)NSCollectionViewCell,首先注冊(cè)拖動(dòng)事件

 [self.collection registerForDraggedTypes:@[NSPasteboardTypeString]];
    [self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:yearMask];
    [self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];

其次實(shí)現(xiàn)拖動(dòng)代理函數(shù)

@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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末台谊,一起剝皮案震驚了整個(gè)濱河市蓉媳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锅铅,老刑警劉巖酪呻,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異盐须,居然都是意外死亡玩荠,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門贼邓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來阶冈,“玉大人,你說我怎么就攤上這事塑径∨樱” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵统舀,是天一觀的道長(zhǎng)匆骗。 經(jīng)常有香客問我劳景,道長(zhǎng),這世上最難降的妖魔是什么碉就? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任枢泰,我火速辦了婚禮,結(jié)果婚禮上铝噩,老公的妹妹穿的比我還像新娘衡蚂。我一直安慰自己,他們只是感情好骏庸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布毛甲。 她就那樣靜靜地躺著,像睡著了一般具被。 火紅的嫁衣襯著肌膚如雪玻募。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天一姿,我揣著相機(jī)與錄音七咧,去河邊找鬼。 笑死叮叹,一個(gè)胖子當(dāng)著我的面吹牛艾栋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛉顽,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼蝗砾,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了携冤?” 一聲冷哼從身側(cè)響起悼粮,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎曾棕,沒想到半個(gè)月后扣猫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翘地,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年申尤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片子眶。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瀑凝,死狀恐怖序芦,靈堂內(nèi)的尸體忽然破棺而出臭杰,到底是詐尸還是另有隱情,我是刑警寧澤谚中,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布渴杆,位于F島的核電站寥枝,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏磁奖。R本人自食惡果不足惜囊拜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望比搭。 院中可真熱鬧冠跷,春花似錦、人聲如沸身诺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)霉赡。三九已至橄务,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間穴亏,已是汗流浹背蜂挪。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嗓化,地道東北人棠涮。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像刺覆,于是被迫代替她去往敵國(guó)和親故爵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • 1隅津、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_X自主閱讀 15,979評(píng)論 3 119
  • 第八章 威脅 文/張悠揚(yáng) 當(dāng)付姿穿過漆黑的園林诬垂,來到一處偏僻的別屋時(shí),周圍一片安靜伦仍,遠(yuǎn)處宴席的聲音都只能隱隱約約聽...
    張悠揚(yáng)閱讀 537評(píng)論 1 5
  • 狐貍對(duì)小王子說结窘,馴養(yǎng)我吧,那么你對(duì)于我就是宇宙中獨(dú)一無(wú)二充蓝,而我對(duì)于你也將是世界上唯一的隧枫。小王子馴養(yǎng)了狐貍,后來也離...
    BERRYSHUN閱讀 235評(píng)論 0 1
  • 楊慧霞 洛陽(yáng) 焦點(diǎn)解決講師班二期 堅(jiān)持分享第1022天 從來有病都是匆匆忙忙往醫(yī)院跑谓苟、往藥店趕官脓。這段時(shí)間單...
    yhx慧心慧語(yǔ)閱讀 297評(píng)論 0 0
  • 我學(xué)的專業(yè)是網(wǎng)絡(luò)技術(shù),其中涉及路由交換機(jī)涝焙、服務(wù)器卑笨,Linux系統(tǒng)操作。 我是李哲 來自河北邯鄲 出了校門才知道已學(xué)...
    烽火_8e5d閱讀 176評(píng)論 0 1