0

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

  • 启动屏幕保护程序
  • 睡觉
  • 关闭
  • 清空垃圾桶
  • 弹出量
  • 打开应用程序
  • 重新开始
  • 登出

本质上,我想在用户单击 NSButton 时启动其中一个命令,因此这将是一种通过这种方式实现的方法。

4

1 回答 1

1

对于屏幕保护程序:

- (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

用于重启、关机、睡眠、注销

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]); 
}
于 2013-04-04T10:41:50.310 回答