Create a NSWindow under C++ - cocoa

I made a simple experiment, I wrote the following code in Xcode:
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSApplication* app = [[NSApplication alloc] init];
NSWindow* window = [[NSWindow alloc]
initWithContentRect: NSMakeRect(0, 0, 640, 480)
styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[window setTitle: #"New Window"];
[window center];
[window makeKeyAndOrderFront:nil];
[app run];
[pool release];
return 0;
}
It runs as expected, a new empty window out there, but if I compile it under terminal with command line:
$ g++ test.mm -framework Cocoa
$ ./a.out
It will breeze at [app run] without the window.
Am I doing wrong? Why it has different behaviors between Xcode and command line? Does somebody can tell me how I can achive the same behaviors in command line?
Thanks in advance.

Taking your code and compiling it works fine for me on a 10.7.2 machine... the window shows up (albeit behind my terminal window and does not appear as a running application).
If you're looking to get your app to show up as a process running in the dock, it has to be properly packaged in a .app bundle. If you do the following:
g++ test.mm -framework Cocoa
mkdir -p cli.app/Contents/MacOS
cp a.out cli.app/Contents/MacOS/cli
and then run cli.app/Contents/MacOS/cli from the command line, your process will show up as a running application.

Related

`NSStatusItem` created from `main()` doesn't show up on system status bar

I'm trying to make a simple macOS Cocoa application using NSStatusItem to create a clickable icon on the system status bar. However, when I launch my application, I get this warning and the icon doesn't show up:
2020-03-03 14:43:11.564 Mocha_bug_example[936:39572] CGSGetActiveMenuBarDrawingStyle((CGSConnectionID)[NSApp contextID], &sCachedMenuBarDrawingStyle) returned error 268435459 on line 46 in NSStatusBarMenuBarDrawingStyle _NSStatusBarGetCachedMenuBarDrawingStyle(void)
Here's a minimal reproducible example for my application:
#import <AppKit/AppKit.h>
NSStatusItem* statusItem;
int main (int argc, char* argv[]) {
statusItem = [NSStatusBar.systemStatusBar statusItemWithLength: -1];
statusItem.button.title = #"foobar";
statusItem.visible = YES;
[NSApplication.sharedApplication run];
return 0;
}
I compiled and ran the example like this:
MacBook-Air-5:Mocha ericreed$ clang -o Mocha_bug_example -framework AppKit -fobjc-arc Mocha_bug_example.m
MacBook-Air-5:Mocha ericreed$ ./Mocha_bug_example
2020-03-03 14:43:11.564 Mocha_bug_example[936:39572] CGSGetActiveMenuBarDrawingStyle((CGSConnectionID)[NSApp contextID], &sCachedMenuBarDrawingStyle) returned error 268435459 on line 46 in NSStatusBarMenuBarDrawingStyle _NSStatusBarGetCachedMenuBarDrawingStyle(void)
[Application hung until I pressed Ctrl+C]
^C
MacBook-Air-5:Mocha ericreed$
Note: disabling automatic reference counting and adding [statusItem release]; after calling run as this similar question suggested made no visible difference.
This is how to add status bar item to command line app mac osx cocoa
Adapting apodidae's answer to Swift. Just put this in the main.swift file:
let app = NSApplication()
let statusItem = NSStatusBar.system.statusItem(withLength: -1)
statusItem.button!.title = "Hello, world!"
app.run()
I don't understand the finer details of the NSReleasePool as apodidae included, but it works for me without that.
#import <Cocoa/Cocoa.h>
int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSApplication *application = [NSApplication sharedApplication];
NSStatusItem* statusItem;
statusItem = [NSStatusBar.systemStatusBar statusItemWithLength: -1];
statusItem.button.title = #"foobar";
statusItem.visible = YES;
[application run];
[pool drain];
return 0;
}
Save file with the name 'statusBar_SO.m'
Compile from Terminal:
clang statusBar_SO.m -framework Cocoa -o statusBar && ./statusBar
This is not the kind of thing you can do in main().
Except for extrememly unusual situations, you should never modify the main() that comes with the application template, and it must call NSApplicationMain():
int main(int argc, char *argv[])
{
// start the application
return NSApplicationMain(argc, (const char **) argv);
}
The Cocoa framework doesn't get initialized until you call NSApplicationMain() and is generally unusable until then.
This kind of setup should be done in applicationWillFinishLaunching or applicationDidFinishLaunching.
Update
The original poster is not using Xcode and is willing to brave the wilderness alone. ;)
This also implies that their application bundle will not have a main NIB file that would normally create and connect the application delegate object, main menu, and so forth.
There are intrepid individuals who have braved this territory and you can read about it in Creating a Cocoa application without NIB files.

Forcing Python to run in 64 mode from a NSTask subprocess

What is a method that forces python to run in 64 mode from a NSTask?
Update: As per Ned's suggestion I tried referencing Python2.7 directly with objective c and that worked. Changed #"/usr/bin/python" to #"/usr/bin/python2.7". The new code is at the bottom of the question.
I have a 64 bit system. When run from terminal python runs in 64 bit.
When I run a plain shell from a NSTask running /usr/bin/uname -m it returns x86_64.
I've tried using arch but the shell running python is still in 32 bit mode.
Example method
-(void) runPython64BitScriptViaArchWithPath:(NSString*)path {
NSTask* task = [[NSTask alloc] init];
task.launchPath = #"/usr/bin/arch" ;
task.arguments = [NSArray arrayWithObjects: #"-x86_64", #"/usr/bin/python", path, nil];
[task setStandardInput:[NSPipe pipe]] ;
NSPipe *stdOutPipe = nil;
stdOutPipe = [NSPipe pipe];
[task setStandardOutput:stdOutPipe];
NSPipe* stdErrPipe = nil;
stdErrPipe = [NSPipe pipe];
[task setStandardError: stdErrPipe];
NSLog(#"%#", [task arguments]) ;
[task launch] ;
NSData* data = [[stdOutPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
NSInteger exitCode = task.terminationStatus;
if (exitCode != 0)
{
NSLog(#"Error!");
NSData *error = [[stdErrPipe fileHandleForReading] readDataToEndOfFile] ;
NSLog(#"Exit code : %ld", (long)exitCode) ;
NSString *result = [[NSString alloc] initWithBytes: error.bytes length:error.length encoding: NSUTF8StringEncoding] ;
NSLog(#"%#",result);
[result release];
} else {
NSString *result = [[NSString alloc] initWithBytes: data.bytes length:data.length encoding: NSUTF8StringEncoding] ;
NSLog(#"%#",result) ;
[result release];
}
[task release] ;
}
an example python script that works if run from terminal
#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'social_shields.settings'
try:
import hosts.models
shield = hosts.models.Shield.objects.all()[0]
shield.active = True
shield.save()
except Exception as exception:
import struct
print 'Bits : %s' % ( 8 * struct.calcsize("P"))
print exception
example log
2013-07-04 16:10:31.600 socialshield[88688:303] onShieldDown
2013-07-04 16:10:31.607 socialshield[88688:303] x86_64
2013-07-04 16:10:31.607 socialshield[88688:303] (
"-x86_64",
"/usr/bin/python",
"/source/social_shields/social_shields/shield_down.py"
)
2013-07-04 16:10:31.933 socialshield[88688:303] Bits : 32
Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): no suitable image found. Did find:
/Library/Python/2.7/site-packages/_mysql.so: mach-o, but wrong architecture
new code that works after applying Ned's suggestion :-)
-(void) runPython64BitScriptViaArchWithPath:(NSString*)path {
NSTask* task = [[NSTask alloc] init];
task.launchPath = #"/usr/bin/arch" ;
task.arguments = [NSArray arrayWithObjects: #"-x86_64", #"/usr/bin/python2.7", path, nil];
[task setStandardInput:[NSPipe pipe]] ;
NSPipe *stdOutPipe = nil;
stdOutPipe = [NSPipe pipe];
[task setStandardOutput:stdOutPipe];
NSPipe* stdErrPipe = nil;
stdErrPipe = [NSPipe pipe];
[task setStandardError: stdErrPipe];
NSLog(#"%#", [task arguments]) ;
[task launch] ;
NSData* data = [[stdOutPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
NSInteger exitCode = task.terminationStatus;
if (exitCode != 0)
{
NSLog(#"Error!");
NSData *error = [[stdErrPipe fileHandleForReading] readDataToEndOfFile] ;
NSLog(#"Exit code : %ld", (long)exitCode) ;
NSString *result = [[NSString alloc] initWithBytes: error.bytes length:error.length encoding: NSUTF8StringEncoding] ;
NSLog(#"%#",result);
[result release];
} else {
NSString *result = [[NSString alloc] initWithBytes: data.bytes length:data.length encoding: NSUTF8StringEncoding] ;
NSLog(#"%#",result) ;
[result release];
}
[task release] ;
}
I assume you have verified that /Library/Python/2.7/site-packages/_mysql.so is 64-bit and that you are really using the same Python in both cases. On OS X 10.6 and later systems, /usr/bin/python is actually a wrapper executable that determines which version of Python and which architecture (32-bit or 64-bit) to run; see man 1 python for details. Try executing /usr/bin/python2.7 directly. That should default to 64-bit. You can also check whether Python is running in 32- or 64-bit mode by using this documented test:
import sys; print(sys.maxsize > 2**32)

How do you wait for an application to close in OS X?

I am using the code below to check if an application is running and close it. Can someone provide an example of how to request an application calose and wait for it to close before proceeding?
+ (BOOL)isApplicationRunningWithName:(NSString *)applicationName {
BOOL isAppActive = NO;
NSDictionary *aDictionary;
NSArray *selectedApps = [[NSWorkspace sharedWorkspace] runningApplications];
for (aDictionary in selectedApps) {
if ([[aDictionary valueForKey:#"NSApplicationName"] isEqualToString: applicationName]) {
isAppActive = YES;
break;
}
}
return isAppActive;
}
+ (void)stopApplication:(NSString *)pathToApplication {
NSString *appPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:pathToApplication];
NSString *identifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
NSArray *selectedApps = [NSRunningApplication runningApplicationsWithBundleIdentifier:identifier];
// quit all
[selectedApps makeObjectsPerformSelector:#selector(terminate)];
}
You can use Key-Value Observing to observe the terminated property of each running application. This way, you'll get notified when each application terminates, without having to poll.
One way would be to periodically call isApplicationRunningWithName on a timer, and wait until that function returns NO.
The commandline timelimit will let you send a close signal to an app, wait x seconds, then kill it (or send any other signal you like, kill is -9) if hasn't obeyed the "warning" signal.
(Note: I haven't tried compiling it on Mac, but I believe it's fairly POSIX-compliant code and not Linux-specific as it runs on BSD and others.)

Applescript and Cocoa

I would like to call a IBAction within a cocoa app from applescript:
I want to call:
- (IBAction)reverse:(id)pId;
{
direction = -1;
}
with a line in an external applescript file like:
tell application "theapp"
reverse
end tell
Any Ideas?
Thanks in Advance
Use NSAppleScript.
NSAppleScript *as = [[NSAppleScript alloc] initWithSource:#"tell application \"theapp\"\nreverse\nend tell"];
NSDictionary *err = nil;
NSAppleEventDescriptor *desc = [as executeAndReturnError:&err];
NSLog(#"Error: %#\nData: %#", err, desc.data);
[as release];
There is also a good answer about Scripting Bridge here

-[NSWindowController window] retaining window when NSWindowController initialized with window?

In an application (OS X 10.6.7) I have a NSWindowController subclass which is initialized with -[NSWindowController initWithWindow:]—i.e., I have already created the window in code; I'm not loading it from a nib.
Normally, I refer to the window in my NSWindowController subclasses with [self window]. But in this case, every time I send [self window], the window gets retained, so I end up leaking quite a lot.
Is this intended behavior? For the moment I've worked around it by just storing the window in an instance variable in the init method and never sending [self window].
I am pretty sure this is not happening because NSWindowController is trying to load the window: -loadWindow does not retain the window and -isWindowLoaded returns YES:
(gdb) set $window = (id)[self window]
Current language: auto; currently objective-c
(gdb) p (int)[$window retainCount]
$1 = 3
(gdb) p (BOOL)[self isWindowLoaded]
$2 = 1 '\001'
(gdb) call (void)[self loadWindow]
(gdb) p (int)[$window retainCount]
$3 = 3
(gdb) p (int)[[self window] retainCount]
$4 = 4
(gdb) p (int)[[self window] retainCount]
$5 = 5
-[NSWindowController window] retaining the window is fine; the issue seems to be related to autorelease pools.
window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 200, 200)
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
NSWindowController *controller = [[NSWindowController alloc] initWithWindow:window];
[window setTitle:#"testing"];
[window makeKeyAndOrderFront:nil];
[window release];
NSLog(#"[window retainCount]: %d", [window retainCount]);
[controller window];
[controller window];
[controller window];
NSLog(#"[window retainCount]: %d", [window retainCount]);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[controller window];
[controller window];
[controller window];
NSLog(#"[window retainCount]: %d", [window retainCount]);
[pool drain];
NSLog(#"[window retainCount]: %d", [window retainCount]);
The output is:
2011-06-12 19:26:52.337 window[5517:a0b] [window retainCount]: 1
2011-06-12 19:26:52.339 window[5517:a0b] [window retainCount]: 4
2011-06-12 19:26:52.340 window[5517:a0b] [window retainCount]: 7
2011-06-12 19:26:52.340 window[5517:a0b] [window retainCount]: 4
The problem was that I forgot to create a pool when doing Cocoa stuff in a Carbon event handler (InstallApplicationEventHandler). This matches the context of the thread I linked to.
Ordinarily I see an exception when there's no autorelease pool present, so I'm guessing there is simply a pool in place that never gets drained.

Resources