Hi everyone!
I'm developing my first Mac OS X application, and I'm having some problems with an AppleEvent that I want to send to the system.
Firstly, I found an article on how to programmatically shutdown, restart, sleep or logout an apple computer Link, which is the main objective I want to achieve with the application.
Well, I've tested that code and works perfectly fine as it is, but what I need to do is to use it on a GUI and run it on a cycle loop defined by the user so the computer will sleep/wake up or restart (power off/power on) automatically when certain period of time has passed.
This is for testing purposes on certain PCI SSDs, so when the computer can't wake up/restart itself the piece it's considered a failure and it's segregated.
I'm totally new on the Objective C/Swift programming environment, I have read some tutorials, but can't quite understand Objective C completely whereas I do understand Swift.
I adapted the code and bridged the two languages so I can make use of an Objective-C object that has the functions that perform the previous mentioned processes.
The problem here is:
*The code prints a certain line that says that the computer is going to sleep, (which was on the example code previously provided), but now as it interfaces with Swift and the GUI it appears to do nothing but show the date I formatted to be shown on a TextField I declared before.
I really don't know what's going on here, and I'm sorry if I'm being too clumsy on my coding but I will really appreciate any help.
The code of every important file on the project is shown below:
classFunctions.m
Header.h
FFTest-Bridging-Header.h
ViewController.swift
I'm developing my first Mac OS X application, and I'm having some problems with an AppleEvent that I want to send to the system.
Firstly, I found an article on how to programmatically shutdown, restart, sleep or logout an apple computer Link, which is the main objective I want to achieve with the application.
Well, I've tested that code and works perfectly fine as it is, but what I need to do is to use it on a GUI and run it on a cycle loop defined by the user so the computer will sleep/wake up or restart (power off/power on) automatically when certain period of time has passed.
This is for testing purposes on certain PCI SSDs, so when the computer can't wake up/restart itself the piece it's considered a failure and it's segregated.
I'm totally new on the Objective C/Swift programming environment, I have read some tutorials, but can't quite understand Objective C completely whereas I do understand Swift.
I adapted the code and bridged the two languages so I can make use of an Objective-C object that has the functions that perform the previous mentioned processes.
The problem here is:
*The code prints a certain line that says that the computer is going to sleep, (which was on the example code previously provided), but now as it interfaces with Swift and the GUI it appears to do nothing but show the date I formatted to be shown on a TextField I declared before.
I really don't know what's going on here, and I'm sorry if I'm being too clumsy on my coding but I will really appreciate any help.
The code of every important file on the project is shown below:
classFunctions.m
Code:
#include <stdio.h>
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>
#include <Foundation/Foundation.h>
#import "Header.h"
@implementation intOb
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);
int executeSleep(void);
int executeRestart(void);
int executeShutDown(void);
- (OSStatus) SendAppleEventToSystemProcess: (AEEventID)EventToSend{
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
AppleEvent eventReply = {typeNull, NULL};
AppleEvent appleEventToSend = {typeNull, NULL};
OSStatus error = noErr;
error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr)
{
return(error);
}
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc);
if (error != noErr)
{
return(error);
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply,
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&appleEventToSend);
if (error != noErr)
{
return(error);
}
AEDisposeDesc(&eventReply);
return(error);
}
- (int) executeSleep{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAESleep);
if (error == noErr)
{
printf("Computer is going to sleep!\n");
return error;
}
else
{
printf("Computer wouldn't sleep");
return error;
}
}
- (int) executeRestart{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAERestart);
if (error == noErr)
{
printf("Computer is going to restart!\n");
return error;
}
else
{
printf("Computer wouldn't restart\n");
return error;
}
}
- (int) executeShutDown{
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAEShutDown);
if (error == noErr)
{
printf("Computer is going to shutdown!\n");
return error;
}
else
{
printf("Computer wouldn't shutdown\n");
return error;
}
}
@end
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);
int executeSleep(void);
int executeRestart(void);
int executeShutDown(void);
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend)
{
AEAddressDesc targetDesc;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
AppleEvent eventReply = {typeNull, NULL};
AppleEvent appleEventToSend = {typeNull, NULL};
OSStatus error = noErr;
error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr)
{
return(error);
}
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc);
if (error != noErr)
{
return(error);
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply,
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&appleEventToSend);
if (error != noErr)
{
return(error);
}
AEDisposeDesc(&eventReply);
return(error);
}
int executeRestart(void){
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAERestart);
if (error == noErr)
{
printf("Computer is going to restart!\n");
return error;
}
else
{
printf("Computer wouldn't restart\n");
return error;
}
}
int executeSleep(void){
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAESleep);
if (error == noErr)
{
printf("Computer is going to sleep!\n");
return error;
}
else
{
printf("Computer wouldn't sleep");
return error;
}
}
int executeShutDown(void){
OSStatus error = noErr;
error = SendAppleEventToSystemProcess(kAEShutDown);
if (error == noErr)
{
printf("Computer is going to shutdown!\n");
return error;
}
else
{
printf("Computer wouldn't shutdown\n");
return error;
}
}
Header.h
Code:
#import <Foundation/Foundation.h>
@interface intOb : NSObject
//@property (nonatomic, strong) AEEventID *EventToSend;
//@property (nonatomic, strong) NSString *EventToSend;
- (OSStatus) SendAppleEventToSystemProcess: (AEEventID)EventToSend;
- (int) executeSleep;
- (int) executeRestart;
- (int) executeShutDown;
@end
[/B]
FFTest-Bridging-Header.h
Code:
#import "Header.h"
ViewController.swift
Code:
import Cocoa
import CoreServices
class ViewController: NSViewController {
//MARK: Properties
@IBOutlet weak var workOrderTextField: NSTextField!
@IBOutlet weak var partNumberTextField: NSTextField!
@IBOutlet weak var operatorTextField: NSTextField!
@IBOutlet weak var stationPopUpBtn: NSPopUpButton!
@IBOutlet weak var confirmBtn: NSButton!
@IBOutlet weak var serialNumberTextField: NSTextField!
@IBOutlet weak var validationStatusImage: NSImageView!
@IBOutlet weak var validationErrorBtn: NSButton!
@IBOutlet weak var wakeTimeTextField: NSTextField!
@IBOutlet weak var sleepTimeTextField: NSTextField!
@IBOutlet weak var testLengthTextField: NSTextField!
@IBOutlet weak var sleepCtDownLabel: NSTextField!
@IBOutlet weak var sleepCycleCountLabel: NSTextField!
@IBOutlet weak var sleepTestStartedLabel: NSTextField!
@IBOutlet weak var lastCycleLabel: NSTextField!
@IBOutlet weak var totalTimeLabel: NSTextField!
@IBOutlet weak var sleepStatusLabel: NSTextField!
@IBOutlet weak var sleepResetBtn: NSButton!
@IBOutlet weak var sleepStopBtn: NSButton!
@IBOutlet weak var sleepStartBtn: NSButton!
@IBOutlet weak var countdownTextField: NSTextField!
@IBOutlet weak var pwrOffLabel: NSTextField!
@IBOutlet weak var restartCtDownLabel: NSTextField!
@IBOutlet weak var restartTestStartedLabel: NSTextField!
@IBOutlet weak var restartCountLabel: NSTextField!
@IBOutlet weak var restartStatusLabel: NSTextField!
@IBOutlet weak var restartResetBtn: NSButton!
@IBOutlet weak var restartStartBtn: NSButton!
let obj: intOb = intOb()
@objc func systemSleep() -> Void{
obj.executeSleep()
}
func getDate() -> String{
let currentDateTime = Date()
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .short
return formatter.string(from: currentDateTime)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
//MARK: Actions
@IBAction func StartSleep(_ sender: Any) {
sleepTestStartedLabel.stringValue = getDate()
obj.executeSleep()
}
}
[/B]