• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:Objective-CのNSTaskの引数に全角/半角スペース及び日本)

Objective-CのNSTaskの引数の指定方法について

このQ&Aのポイント
  • Objective-CのNSTaskを使用して引数に全角/半角スペースや日本語を含むパスを指定すると正常に処理が行えない問題が発生しています。
  • アプリのパスとファイルのパスを含む引数配列を作成する際に、スペース部がバックスラッシュでエスケープされていたり、日本語がエンコードされていたりしましたが、うまく解決できませんでした。
  • どのように引数を指定すればいいのか、分かる方がいらっしゃいましたら教えていただきたいです。

質問者が選んだベストアンサー

  • ベストアンサー
  • xjd
  • ベストアンサー率63% (1021/1612)
回答No.1

こんちは。 NSTaskは使った事無いのですが、ファイルパス名をURLで指定してみてはいかがでしょう? NSTaskはFoundationフレームワークのクラスなので、ファイルやディレクトリの場所は、 ファイルパス名/URLどちらでも指定できると思います。 で、URLはスペースや英数字以外の文字は使用できないので、UTF-8でテキストエンコーディングします。 NSStringクラスには、stringWithUTF8Stringみたいなエンコードメソッドが多数あるので試してみては?

S202
質問者

お礼

アドバイスありがとうございました。 ご指摘のURLで指定したところ正常に処理ができました。 ただ、この処理は一度の実行で複数毎の画像に対して処理を行うのですが、1回だけの実行だと正常に終了するのですが、2回以降の実行がある場合失敗してしまいます。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • Objective-CのNSTaskからAutomatorで作成したア

    Objective-CのNSTaskからAutomatorで作成したアプリを複数回実行するとフリーズするのですが、原因が分からないのでアドバイスをいただきたく書き込みさせていただきます。 Automatorの処理は、「イメージファイルにサムネールアイコンを追加」するだけの処理です。1度の実行で複数枚の画像を1枚ずつAutomatorに渡して処理を行います。 Automatorの実行は以下のような内容で実行しています。 -(BOOL)imageIcon(NSString *)filePath // 画像ファイルパス { NSTask *task = [[NSTask alloc]init]; NSURL *urlfilePath = [NSURL fileURLWithPath:filePath]; NSString *Command = [NSString stringWithFormat:@"/usr/bin/open"]; NSArray *argumets = [NSArray arrayWithObjects:@"-a", @"Automatorアプリのパス", [NSString stringWithFormat:@"%@",urlfilePath], nil]; [task setLaunchPath:Command]; [task setArguments:argumets]; [task launch]; [task waitUntilExit]; // 処理が完了していないのに処理を継続するのを防ぐ為、sleep [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]]; if([self processList:@"Automatorアプリ名"]){ //psコマンドを実行して、処理中ならYESを返す。 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]]; } [task release]; return YES; } 上記の関数を各画像の処理時に呼び出しています。 上記の関数の先頭部にreturn YESで強制的に処理をすると、フリーズは発生しません。 また、フリーズした時は、Automatorアプリが実行中のままとなりますが、アクテビティモニタで見るとAutomatorアプリはありません。 Xcodeのコンソールにも、何も表示はありません。 説明がまとまっていなくてすいません。 どなたか、Objective-Cに精通されておられるかたおられまいたら、アドバイスをお願い致します。

  • Objective-CからImageMagickを実行する

    過去に、画像変換処理などで質問させていただいたものです。 その節はありがとうございました。 開発環境では正常に画像の反転・回転が行えたのですが、開発したアプリを他のMac(Xcode未インストール)にコピーすると、画像の90度回転処理が異なる結果となってしまいました。 そこで、ImageMagickをコマンドから実行して、画像の加工処理を行おうと思っています。 しかし、コマンドを起動すると「launch path not accessible」と表示されます。 調べてみたところ、コマンドのパスが違うみたいなのですが、 ImageMagickのconvertにはパスは通しています。 また、コマンドをフルパスで指定しても、同じ結果になります。 NSTask *task = [[NSTask alloc]init]; [task setLaunchPath:@"コマンド"]; [NSArray *arguments = [NSArray arrayWithObjects:@"処理オプション",     @"参照元画像",     @"保存先画像",     nil]; [task setArguments:arguments]; [task launch]; [task waitUnitExit]; task release]; どなたか、お分かりになる方おられましたら、問題点・アドバイスをお願い致します。

  • Objective-C

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Dat"ofType:@"plist"]; NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:path]; NSArray *array = [[NSArray alloc]init]; for (id keys in dic) { array = [dic objectForKey:keys]; } *dicには、下記のキー、値が入ってます。(NSLOGで確認しました。) D = ( DAT,DIOP,DSE );... N = ( NET,NW );... Y = (YEA,YAC ); 上記のコードだと、値が全てarrayに格納されませんでした。 なぜか、キーがNの値だけが格納されてしまいます。 全てのキーの値を格納するには、どうしたらよいのでしょうか? よろしくお願いします。

  • [再]objective-c 描画処理について

    前回質問させて頂きましたが、まだ抜け出せていないので教えて下さい。 ↓↓↓ -------------------------------------------------------------------------------------------------------------- 初心者ですが、iPhoneアプリを作成しています。 画像を並べてスクロールできるように配列で描画処理を行ないたいのですが、うまくいきません。 今のところのコードはこんな感じです↓ - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray* imageList = [NSMutableArray array]; for (int i=1; i < 20; i++) { UIImage* image = [UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg", i+1]]; [imageList addObject:image]; } MyImageView* imageView = [[MyImageView alloc] initWithImage:imageList]; scrollView.contentSize = imageView.bounds.size; [scrollView addSubview:imageView]; [imageView release]; } どなたか分かる方、教えて頂けますよう宜しくお願いします。 -------------------------------------------------------------------------------------------------------------- ↓ NSArray *initWithImage;と引数で渡しましたが、 「MyImageView* imageView = [[MyImageView alloc] initWithImage:imageList];」 の部分で、「Incompatible pointer sending 'NSMutableArray *' to parameter of type 'UIImage *'」とエラー表示され、実行すると、 「Tread1 : Program received signal: "SIGABRT"」で落ちてしまいます。

  • objective-c 画像の反転と回転

    Cについては、全くの素人です。 読み込んだ画像を回転および反転して保存したいのですが、 画像の取り扱いが全くわからないのでご教授をお願いします。 webや書籍を探してみたのですが、画像の取り扱いについて記載しているものがなく困っています。 処理としては、複数毎の画像を読み取り、2枚目の画像を反転、3枚目を回転したいのです。 処理した画像を別名で保存するという流れです。 画像を読み取り、保存する処理は正常に行えています。 for(int i=0; i< [myImages count]; i++){     NSImage *bitmap = [[NSImage alloc] initWithContentsOfFile:[[myImages objectAtIndex:i]imageRepresentation]];     //反転          // 回転処理          // 画像保存     NSData* indata = [bitmap TIFFRepresentation];     NSBitmapImageRep* bitmapImageRep = [NSBitmapImageRep imageRepWithData:indata];     NSData* outdata = [bitmapImageRep representationUsingType:NSJPEGFileType properties:[NSDictionary dictionary]];     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);     NSString* path = [NSString stringWithFormat:@"%@/test_%d.jpg",[paths objectAtIndex:0],i, nil];     [outdata writeToFile:path atomically:YES];     [bitmap release]; } 回転処理については、調べた結果、NSAffineTransformを使う?ところまではわかったのですが、書式やらが全くわからない状態です。 反転処理は、setFlipped:YESで反転されるような文献を見つけたのですが、全く反転されません。 ご教授のほど、よろしくお願いします。

  • C(char *s1)の文字列をObjective-Cの文字列(NSS

    C(char *s1)の文字列をObjective-Cの文字列(NSString *s2)にするにはどうしたらよいのでしょうか? char s1[] = "abc"; NSString *s2 = [[NSString alloc] initWithUTF8String:s1];  // (A) NSLog(@"s2=[%s]", s2); 結果がabcかなと思っていましたが、読めない変な文字が表示されました。 (A)を NSString *s2 = [NSString stringWithFormat:@"%s", s1]; に変えても同じでした。 s1のC文字列は日本語を含まないアルファベットの文字列なのになぜ?と 困っています。 Objective-Cの初心者で申し訳ありません。

  • Objective-CのTableViewについて

    つまづいてなかなか先に進めません。回答の方をよろしくお願いします。 [ソース] - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch ([indexPath section]) { case 1: { switch ([indexPath row]) { case 0: { DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; if ([cell isOpen]) { [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } else { [cell setOpen]; dropDown1Open = [cell isOpen]; [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } break; } default: { dropDown1 = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text]; NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]]; DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path]; [[cell textLabel] setText:dropDown1]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; break; } } } } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } このようなプログラムを書いたのですが、 UITableViewのCellがなぜか反転してしまいます。 つまり、画像のような状態になった後、再びCellを押すと閉じ、さらに押すともう一度画像のような状態になるのですが、Object1 → Object3 という状態から Object3 → Object1 と順番が逆になってしまうのです。 全く理由がわかりません。ぜひとも回答の方をお願い致します。 初めての投稿ですので、どのように説明していけばいいのかわかりませんでした。 ですが、どうしても直したい問題ですので、よろしくお願い致します。

  • objective-c 描画処理について

    初心者ですが、iPhoneアプリを作成しています。 画像を並べてスクロールできるように配列で描画処理を行ないたいのですが、うまくいきません。 今のところのコードはこんな感じです↓ - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray* imageList = [NSMutableArray array]; for (int i=1; i < 20; i++) { UIImage* image = [UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg", i+1]]; [imageList addObject:image]; } MyImageView* imageView = [[MyImageView alloc] initWithImage:imageList]; scrollView.contentSize = imageView.bounds.size; [scrollView addSubview:imageView]; [imageView release]; } どなたか分かる方、教えて頂けますよう宜しくお願いします。

  • Objecive-C

    NSDictionary *row1 = [[NSDictionary alloc]initWithObjectsAndKeys:            @"MacBook",@"Name",@"White",@"Color",nil]; NSDictionary *row2 = [[NSDictionary alloc]initWithObjectsAndKeys:        @"MacPro",@"Name",@"Silver",@"Color",nil]; NSArray *array● = [[NSArray alloc]initWithObjects:row1,row2,nil]; これをplistを使って実現させようと思ったのですが、 NSString *path =[[NSBundle mainBundle]pathForResource:@"Celldata" ofType:@"plist"]; NSArray *array○ = [[NSArray alloc]initWithContentsOfFile:path]; *array●と*array○は同様の配列で実現可能ですか? plistに問題があるのでしょうか?。 plistは、下記です。 key Type Value Root Dictionary row1 Dictionary ( 2 items) Name String MacBooc Color String White

  • Objective-c 画像処理

    Cについては、全くの素人です。 「Xcodeプログラミング大全」という書籍を参考に、開発を進めているのですが、行き詰まってしまったので、質問させて下さい。 ImageKitを使ったイメージブラウザを作成して、取り込んだ画像を別名で別の場所に保存するプログラムを作成しています。 前半部は文字数の関係で省略します。 ボタンをクリックして画像を保存します。 - (IBAction)myButton:(id)sender; { // imagebrowserに保存した画像を順番に処理 for(int i=0; i< [myImages count]; i++){ NSData *data = nil; NSString *errorDescription; //NSPasteboard *pasteboard = [myImages objectAtIndex:i]; if([[[myImages objectAtIndex:i] types] containsObject:NSFilenamesPboardType]) data = [[myImages objectAtIndex:i] dataForType:NSFilenamesPboardType]; if(data){ NSArray *filenames = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:kCFPropertyListImmutable format:nil errorDescription:&errorDescription]; for(int j=0;j< [filenames count];j++){ //NSLog(@"%@",[filenames objectAtIndex:j]); NSBitmapImageRep* bitmap = [myImages objectAtIndex:i]; // 画像保存 NSData* outdata = [bitmap representationUsingType:NSPNGFileType properties:[NSDictionary dictionary]]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES); NSString* path = [NSString stringWithFormat:@"%@/test.png",[paths objectAtIndex:0], nil]; [outdata writeToFile:path atomically:YES]; / } } } } @end エラーは発生しないのですが、コンソールに「[MyImageObject types]: unrecognized selector sent to instance 0xf722590」と表示されます。 また、画像は全く保存されません。 どなたか、ご教授をお願いします。