I'm developing my first MacOS application where I'm linking Objective-C with Swift, I still got a lot to learn.
I think that I have everything declared and linked, but for some reason the compiler prompts this error.
Use of undeclared identifier 'EventToSend'
EventToSend is an AEEventID type, and I declared that in the function prototype and in the Header file, the bridging header file has the import statement too. What I'm trying to do is to implement a Timer into the sleep and restart functions so it would wait for the timer to expire before execution.
Could someone point out what I'm doing wrong?
classFunctions.m
#include <stdio.h>
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>
#include <Foundation/Foundation.h>
#import "Header.h"
@implementation intOb : NSObject
static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);
int executeSleep(void);
int executeRestart(void);
int executeShutDown(void);
- (OSStatus) SendAppleEventToSystemProcess{
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);
}
//the error appears here
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
Header.h
#ifndef Header_h
#define Header_h
#import <Foundation/Foundation.h>
@interface intOb : NSObject
- (OSStatus) SendAppleEventToSystemProcess;
- (int) executeSleep;
- (int) executeRestart;
- (int) executeShutDown;
@end
#endif /* Header_h */
FTest-Bridging-Header.h
#import "Header.h"
ViewController.swift
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let obj: intOb = intOb()
var sequentialAction: Timer!
sequentialAction = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(obj.executeSleep), userInfo: nil, repeats: true)
sequentialAction.invalidate()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}