기본 콘텐츠로 건너뛰기

XCode OS X Application : Drag & Drop 후 파일 처리


개인적으로 필요한 유틸이 있어서 appleScript 로 만들까하다가
XCode 4.4 로 OS X Application 을 만들어 본 적이 없어서
Drag & Drop, Pipe, File 처리 같은 걸 해봤다.

xib 에 드래그할 대상인 NSImageView 를 놓고
그 NSImageView 를 Customize 한 NSCImageView 를 만들어서 구현했다.
performDragOperation 이벤트에서 

NSPasteboard  *paste = [sender draggingPasteboard];
로 NSPasteboard 객체에 드래그 한 것들을 가지고
NSFilenamesPboardType 인것들을 타입으로 추출하여 NSData로 받았다.


NSArray *fileArray = [paste propertyListForType:@"NSFilenamesPboardType"];
파일 목록은 propertyListForType으로 string array 를 받을 수 있다.


shell 실행하고 결과 stdout 을 받는 것 처리하는데 위의 링크를 참조했다.

  NSTask *task = [[NSTask alloc] init];
  [task setLaunchPath:@"<SHELL COMMAND>"];

NSTask 를 setLaunchPath 메서드를 사용하여 콜할 커맨드를 지정하고

  [task setArguments:[NSArray arrayWithObjects:<@args>, file, nil]];

N개의 argument 를 지정

  NSPipe *pipe=[NSPipe pipe];
  [task setStandardOutput:pipe];


실행 후 stdout 을 출력할 대상을 pipe 로 돌려놓고

  [task launch];

로 실행.

  NSFileHandle *fileHandle = [pipe fileHandleForReading];

파일 핸들은 아까 설정한 pipe 로부터 받았다.

  [fileHandle readDataToEndOfFile];

로 받으면 된다.
근데 기껏 만들고 나니 배포하는 법을 모르겠네;



#import "NSCImageView.h"

@implementation NSCImageView

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])==NSDragOperationGeneric) {
    return NSDragOperationGeneric;
  } else {
    return NSDragOperationNone;
  }
}

- (void)draggingExited:(id<NSDraggingInfo>)sender {
}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
  NSPasteboard *paste = [sender draggingPasteboard];
  NSString *desiredType = [paste availableTypeFromArray:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
  NSData *carriedData = [paste dataForType:desiredType];
  
  if (nil==carriedData) {
    return NO;
  } else {
    if ([desiredType isEqualToString:NSFilenamesPboardType]) {
      NSArray *fileArray = [paste propertyListForType:@"NSFilenamesPboardType"];
      NSTask *task = [[NSTask alloc] init];
      [task setLaunchPath:@"<SHELL COMMAND>"];
      for (NSString *file in fileArray) {
        [task setArguments:[NSArray arrayWithObjects:<@args>, file, nil]];
        NSPipe *pipe=[NSPipe pipe];
        [task setStandardOutput:pipe];
        NSFileHandle *fileHandle = [pipe fileHandleForReading];
        [task launch];
        NSData *data;
        data = [fileHandle readDataToEndOfFile];
        NSString *string;
        string=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"filename : %@", file);
        [fileHandle closeFile];
        
        [[NSFileManager defaultManager] createFileAtPath:file contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
      }
    } else {
      NSLog(@"Nothing happened");
    }
  }
  [self setNeedsDisplay:YES];
  return YES;
}
@end

댓글

이 블로그의 인기 게시물

세상 간단한 https(+secured websocket): Caddy

nginx, apache2 같은 걸로 매번 certbot 연동을 통해 https 하는게 지겨워서 알아보니 Caddy라는게 있더라. 설치법은 강하게 크기 위해 알아서 해본다. # cat /etc/caddy/Caddyfile your.shitty.site:8123 {   proxy / localhost:9123 {     websocket     transparent   } } 이건 외부에서 8123으로 들어오는 걸 내부적으로 9123로 맞춰서 https 맞춰주는 reverse proxy. 만일 port를 생략하면 # cat /etc/caddy/Caddyfile your.shitty.site {   proxy / localhost:9123 {     websocket     transparent   } } 기본포트인 443으로 되어 https://your.shitty.site 로 접속이 된다. 요새 말썽이던 cloudflare 버리고 netlify domain이랑 caddy를 쓰니까 앓던 이가 빠진 느낌. 이 조합은 당분간 계속 써야겠다 싶다.

LG G Pro 2 Custom Rom +Rooting 기록

선 루팅 https://forum.xda-developers.com/lg-g3/general/guide-root-lg-firmwares-kitkat-lollipop-t3056951 1.2버전 스크립트 모드로. echo Looking for LG serial port.. echo. adb.exe wait-for-device 여기에서 정체 상태여서 강제로 adb kill-server 했더니 알아서 재부팅하심. 폰에선 "펌웨어 업데이트"화면이 나오고 Phone found at COM5! Rooting phone.. If you don't see the SuperSu installer script runs within about a minute, then the root failed. Author : blog.lvu.kr SPECIAL COMMAND : ENTER, LEAVE ## 이런 상황이 됨. #이 느린 속도로 하나씩 증가하고 있음. 한 시간 기다려도 성과없음. 뭐 몇번해봐도 안되고 결국 불안하긴 하지만 kingroot.net  로 설치 롤리팝이니까  https://drive.google.com/file/d/0B4kfGU1z_PClQ3AyM2lKOGR1S3M/view  이걸 받아서 압축 풀기. d838recovery.bat 만 있어서 당황스럽긴한데 실행하고 1번 하니까 리커버리 모드 진입까진 잘 되는 듯. adb sideload 라는게 있어서 해봄. Advance에서 adb sideload 선택하고 USB연결한 PC에서 adb sideload RR-N-v5.8.5-20171010-d838-Final.zip 하니까 착착 잘 진행됨. 이제 흉측한 U+LTE 가 없어짐. ㅇㅋ 군더더기 없고 깔끔한데 남은 건 gapps 를 설치해야함. http://opengapps.org/  를 권해줘서 갔는데 지옥 느림. ARM + 7.1 + stock 이 기본이...