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 に答える