Frans
Frans

Reputation: 57

Screencapture to clipboard works in Cocoa, but doesn't save to file or new mail message

I'm new to Stackoverflow as a registered user, however it helped me out a lot with learning the beginning of Cocoa. However there is one problem that I cannot solve with the help of existing topics/questions, so I decided to ask a question here myself.

I'm trying to use screencapture with NSTask, the following code works in my app:

NSArray *args = [NSArray arrayWithObjects: @"-c", nil];
[[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" arguments: args] waitUntilExit];

It saves a screenshot to my clipboard, however, every other argument that I pass to screencapture doesn't work. The same code does work in Terminal.

For example, if I pass screencapture -M mailme.jpg into the terminal, a new mail message opens (same with saving to root folder or desktop). In my app it just won't work.

The following code:

NSArray *args = [NSArray arrayWithObjects: @"**-M mailme.jpg**", nil];
[[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" arguments: args] waitUntilExit];

Results in the following output:

screencapture: illegal option --  
usage: screencapture [-icMPmwsWxSCUtoa] [files]
  -c         force screen capture to go to the clipboard
  -C         capture the cursor as well as the screen. only in non-interactive modes
  -d         display errors to the user graphically
  -i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot
  -m         only capture the main monitor, undefined if -i is set
  -M         screen capture output will go to a new Mail message
  -o         in window capture mode, do not capture the shadow of the window
  -P         screen capture output will open in Preview
  -s         only allow mouse selection mode
  -S         in window capture mode, capture the screen not the window
  -t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
  -T<seconds> Take the picture after a delay of <seconds>, default is 5
  -w         only allow window selection mode
  -W         start interaction in window selection mode
  -x         do not play sounds
  -a         do not include windows attached to selected windows
  -r         do not add dpi meta data to image
  -l<windowid> capture this windowsid
  -R<x,y,w,h> capture screen rect
  files   where to save the screen capture, 1 file per screen

When I use test.png as the argument, the same output appears as in the Terminal:

libpng warning: zero length keyword
libpng warning: Empty language field in iTXt chunk

, but no file is saved.

Does this mean something is wrong with permissions? Should I save the output in my app? like:

if ([task terminationStatus] == 0)
 {

 }

I have tried a variety of things, but I hope/suspect that this problem has a very simple solution I'm just not seeing.

Thanks in advance for your time and help.

Frans

Upvotes: 1

Views: 680

Answers (1)

MattR
MattR

Reputation: 7048

I think the problem is with the single argument "-M mailme.jpg".

It should work if you separate that into 2 arguments, ie:

NSArray *args = [NSArray arrayWithObjects: @"-M", @"mailme.jpg", nil];

Upvotes: 2

Related Questions