Objective-cosx系统命令

Objective-cosx系统命令,objective-c,macos,cocoa,Objective C,Macos,Cocoa,是否有人知道可以使用Objective-C执行的任何有用的系统命令,下面列出了一些有用的命令: 启动屏幕保护程序 睡眠 关闭 空垃圾 喷射量 公开申请 锁 重新启动 注销 基本上,我想在用户单击NSButton时启动其中一个命令,这样它就可以通过这种方式实现。对于屏幕保护程序: - (IBAction)screensaver:(id)sender { NSString *script=@"tell application \"ScreenSaverEngine\" \

是否有人知道可以使用Objective-C执行的任何有用的系统命令,下面列出了一些有用的命令:

  • 启动屏幕保护程序
  • 睡眠
  • 关闭
  • 空垃圾
  • 喷射量
  • 公开申请
  • 重新启动
  • 注销
基本上,我想在用户单击NSButton时启动其中一个命令,这样它就可以通过这种方式实现。

对于屏幕保护程序:

- (IBAction)screensaver:(id)sender {
    NSString *script=@"tell application \"ScreenSaverEngine\" \
                             \nactivate \
                             \nend tell";
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
    [appleScript executeAndReturnError:nil];

}
对于空垃圾箱:

- (IBAction)emptyTrash:(id)sender {
    NSString *script=@"tell application \"Finder\" \
                             \nempty the trash \
                             \nend tell";
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
    [appleScript executeAndReturnError:nil];
}
公开申请

用这个

NSString *script=@"tell application \
                   \n\"Name of application\" \
                    \nto activate";
要卸载卷,需要放入大量applescript。如下所示:将其设置为字符串并按上述步骤传递给NSAppleScript:

set diskName to "YourDiskNameHere"
tell application "Finder"
 if disk diskName exists then
  eject disk diskName
 else
  tell current application
   set deviceLine to (do shell script "diskutil list | grep \"" & diskName & "\" | awk '{ print $NF }' }'")
   if deviceLine = "" then
    display dialog "The disk \"" & diskName & "\" cannot be found." buttons {"OK"} default button 1 with title "Error" with icon caution
   end if
   set foundDisks to paragraphs of deviceLine
   repeat with i from 1 to number of items in foundDisks
    set this_item to item i of foundDisks
    if this_item contains "disk" then
     do shell script "diskutil mountDisk /dev/" & this_item
    end if
   end repeat
  end tell
 end if
end tell

为了


你为什么不把它作为9个不同的问题,大量的投票,甚至是接受点…同样是回答供应商…@dbramhall,这个链接可能会帮助你…我认为它不仅有利于我的应用程序的开发,而且对许多在一个地方有列表的用户也很有用。Applescript应该使用还是不应该使用仍在讨论中。
NSString *scriptAction = @"restart"; // @"restart"/@"shut down"/@"sleep"/@"log out"
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"Finder\" to %@", scriptAction];
NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:scriptSource] autorelease];
NSDictionary *errDict = nil;
if (![appleScript executeAndReturnError:&errDict]) {
    NSLog([scriptError description]); 
}