kAESleep not being issued thru AESendMessage

Hi everyone!

 

I’m developing my first Mac OS X application in High Sierra 10.13, and I’m having some problems with an AppleEvent that I want to send to the system.

 

I’m adapting a code to send sleep, restart and shutdown messages through AppleEvents in Objective-C to use it in Swift on a GUI.

 

The mentioned code is in the following link: https://stackoverflow.com/questions/6271300/finder-scripting-bridge-to-shutdown/6283690#6283690

 

I’ve already tested that code in objective-C and works flawlessly.

 

I don’t know much about bridging objective-c and swift, but I think I managed to do it because I can use the object I’ve created for that purpose.

 

Well the thing here is that after I linked everything to make it work with the GUI it shows this message:

2017-10-27 09:47:38.951262-0500 FFTest[1726:57020] AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference.

 

And it doesn’t make anything, even tho in objective-c is working.

 

I think I could’ve made a mistake in the linking of headers, declaring the functions or on the variable types because I’m a total newb on the subject.

 

Anyway, I hope someone could please tell me what I’m doing wrong, I already searched for an answer to the previous error but had no luck.

 

Thanks in advance!

 

Code below:

 

classFunctions.m

 

#include 
#include 
#include 
#import "Header.h"

@implementation intOb
static OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSendID);

- (OSStatus) MDSendAppleEventToSystemProcess: (AEEventID)eventToSendID{
    AEAddressDesc targetDesc;
    static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess };
    AppleEvent eventReply = {typeNull, NULL};
    AppleEvent eventToSend = {typeNull, NULL};

    OSStatus status = AECreateDesc(typeProcessSerialNumber,
                                   &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);

    if (status != noErr) return status;

    status = AECreateAppleEvent(kCoreEventClass, eventToSendID,
                                &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);

    AEDisposeDesc(&targetDesc);

    if (status != noErr) return status;

    status = AESendMessage(&eventToSend, &eventReply,
                           kAENormalPriority, kAEDefaultTimeout);

    AEDisposeDesc(&eventToSend);
    if (status != noErr) return status;
    AEDisposeDesc(&eventReply);
    return status;
}
@end
static OSStatus MDSendAppleEventToSystemProcess(AEEventID EventToSend);
OSStatus MDSendAppleEventToSystemProcess(AEEventID eventToSendID) {
    AEAddressDesc targetDesc;
    static const ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess };
    AppleEvent eventReply = {typeNull, NULL};
    AppleEvent eventToSend = {typeNull, NULL};

    OSStatus status = AECreateDesc(typeProcessSerialNumber,
                                   &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);

    if (status != noErr) return status;

    status = AECreateAppleEvent(kCoreEventClass, eventToSendID,
                                &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);

    AEDisposeDesc(&targetDesc);

    if (status != noErr) return status;

    status = AESendMessage(&eventToSend, &eventReply,
                           kAENormalPriority, kAEDefaultTimeout);

    AEDisposeDesc(&eventToSend);
    if (status != noErr) return status;
    AEDisposeDesc(&eventReply);
    return status;
}

 

Header.h

#import 
#import 
/*
*    kAERestart        will cause system to restart
*    kAEShutDown       will cause system to shutdown
*    kAEReallyLogout   will cause system to logout
*    kAESleep          will cause system to sleep
*/
@interface intOb : NSObject
//extern
- (OSStatus) MDSendAppleEventToSystemProcess: (AEEventID) eventToSendID;

 

FFTest-Bridging-Header.h

 

#import "Header.h"

relevant code on ViewController.swift

 

import Cocoa
import CoreServices


class ViewController: NSViewController {
    //MARK: Properties
     //...
    @IBOutlet weak var sleepTestStartedLabel: NSTextField!
    @IBOutlet weak var sleepStartBtn: NSButton!
     //...

    let obj: intOb = intOb()

    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.mdSendAppleEvent(toSystemProcess: kAESleep)
    }

}

Powered by WPeMatico

About

You may also like...

Comments are closed.