Objective c 剪贴板截图在Cocoa中工作,但不';无法保存到文件或新邮件

Objective c 剪贴板截图在Cocoa中工作,但不';无法保存到文件或新邮件,objective-c,macos,cocoa,Objective C,Macos,Cocoa,作为一名注册用户,我对Stackoverflow还不熟悉,但是它在学习Cocoa的开始时帮了我很多。然而,有一个问题我无法借助现有的主题/问题来解决,因此我决定自己在这里问一个问题 我正在尝试将截屏与NSTask结合使用,以下代码在我的应用程序中起作用: NSArray *args = [NSArray arrayWithObjects: @"-c", nil]; [[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" ar

作为一名注册用户,我对Stackoverflow还不熟悉,但是它在学习Cocoa的开始时帮了我很多。然而,有一个问题我无法借助现有的主题/问题来解决,因此我决定自己在这里问一个问题

我正在尝试将截屏与NSTask结合使用,以下代码在我的应用程序中起作用:

NSArray *args = [NSArray arrayWithObjects: @"-c", nil];
[[NSTask launchedTaskWithLaunchPath: @"/usr/sbin/screencapture" arguments: args] waitUntilExit];
它将截图保存到我的剪贴板上,然而,我传递给截图的每一个参数都不起作用。同样的代码也适用于终端

例如,如果我将
screenscapture-M mailme.jpg
传递到终端,将打开一条新的邮件消息(与保存到根文件夹或桌面相同)。在我的应用程序中,它就是不起作用

以下代码:

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

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
,但不保存任何文件

这是否意味着权限有问题?我应该在我的应用程序中保存输出吗?比如:

if ([task terminationStatus] == 0)
 {

 }
我尝试了很多方法,但我希望/怀疑这个问题有一个非常简单的解决方案,我只是没有看到

提前感谢您的时间和帮助


Frans

我认为问题在于单个参数“-M mailme.jpg”

如果将其分为两个参数,即:

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

美好的就像一个符咒,还有保存到文件的理由吗?因为该实用程序的格式是:屏幕捕获[-icMPmwsWxSCUtoa][files]。当我使用:NSArray*args=[NSArray arrayWithObjects:@“screenshot.jpg”,nil];或“@”“screenshot.jpg”。那不行吗?谢谢你的帮助!这应该行得通,但你可能需要给它一个路径,试试像
@”/Users/yourusername/screenshot.jpg“,nil
我现在觉得很愚蠢,花了这么多时间在这上面:p现在一切都好了,非常感谢你的帮助!
NSArray *args = [NSArray arrayWithObjects: @"-M", @"mailme.jpg", nil];