LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sleep Apple Event not being issued in System Process (https://www.linuxquestions.org/questions/programming-9/sleep-apple-event-not-being-issued-in-system-process-4175616368/)

xedge 10-25-2017 09:42 AM

Sleep Apple Event not being issued in System Process
 
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

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

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


astrogeek 10-26-2017 06:08 PM

Your questions are both welcome and appropriate in the LQ Programming forum!

However, as you are working with Apple-centric languages and events, for an Apple OS and not getting much traction here, you might do better to ask in an Apple oriented developer forum. I cannot personally recommend any, but a quick search turns up many hits, including of course, developer.apple.com/.

Otherwise, perhaps someone here will take note of your question and chime in!

Good luck!

xedge 10-27-2017 04:13 PM

Thanks!
 
I have done as you suggested, seems no one answered yet, but still I think I may be wanting to translate the function to swift if possible, all this bridging thing may be the cause for it to not work, or so I think, so that's why I'm now focusing on that, still I there's someone who knows about it in here it would be good if someone could answer my question, if there's any problem on leaving this thread open, close it if you want. If not, I would like to wait a little more if someone can actually answer it!

astrogeek 10-29-2017 12:17 AM

There is no time limit on LQ threads, and no reason to close this one. Hopefully, someone will happen by with the answer soon!

Also, should you find an answer elsewhere, please post your solution here as well to make the information available to others!

Good luck!


All times are GMT -5. The time now is 01:39 AM.