Quickies

[categories] [index] [all (527)] [latest]

Cocoa AppKit
  1. $ /System/Library/CoreServices/talagent -memory_pressure
    
  2. - (void)startPulsing {
        CABasicAnimation* a = [CABasicAnimation animation];
    
        a.keyPath = @"opacity";
        a.fromValue = [NSNumber numberWithFloat:1.0];
        a.toValue = [NSNumber numberWithFloat:0.8];
        a.duration = 1.0;
        a.repeatCount = HUGE_VALF;
        a.autoreverses = YES;
        a.timingFunction = [CAMediaTimingFunction
                            functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
        [self.layer addAnimation:a forKey:@"pulseAnimation"];
    }
    
  3. [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]
    
    [[NSProcessInfo processInfo] processName]
    
  4. NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertNoteIcon)];
    
  5. [CATransaction begin];
    
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];
    
    myLayer.position = CGPointMake(x, y);
    
    [CATransaction commit];
    
  6. NSWorkspace * ws = [NSWorkspace sharedWorkspace];
    NSArray *runningApps = [ws valueForKeyPath:@"launchedApplications.NSApplicationName"];
    
  7. [NSImage imageNamed:NSImageNameComputer]
    
  8. [NSCursor hide];
    [NSCursor unhide];
    [NSCursor setHiddenUntilMouseMoves:YES];
    
  9. NSWorkspace *ws = [NSWorkspace sharedWorkspace];
    BOOL appDidLaunch = [ws launchApplication:@"AnotherApp"];
    
  10. URLNameTransformer.h

    #import <Cocoa/Cocoa.h>
    
    @interface URLNameTransformer : NSValueTransformer {
    
    }
    
    + (Class)transformedValueClass;
    + (BOOL)allowsReverseTransformation;
    - (id)transformedValue:(id)value;
    
    @end
    

    URLNameTransformer.m

    #import "URLNameTransformer.h"
    
    @implementation URLNameTransformer
    
    + (Class)transformedValueClass {
        return [NSString class];
    }
    
    + (BOOL)allowsReverseTransformation {
        return NO;
    }
    
    - (id)transformedValue:(id)value {
        if (value == nil) {
            return value;
        } else {
            NSURL *url = [NSURL URLWithString:(NSString *)value];
            return [url host];
        }
    }
    
    @end
    

    To use it:

    First, register the value transformer

    - (void)awakeFromNib {
        URLNameTransformer *urlTrans = [[[URLNameTransformer alloc] init] autorelease];
        [NSValueTransformer setValueTransformer:urlTrans
                                        forName:@"URLNameTransformer"];
    }
    

    Then, set the value transformer in Interface Builder

    NSValueTransformer

  11. $ defaults write ch.seriot.TheMachine NSShowAllViews YES
    
  12. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:myURLString]];
    
  13. - (void)drawRect:(NSRect)dirtyRect {
    
        NSGraphicsContext *context = [NSGraphicsContext currentContext];
    
        [context saveGraphicsState];
    
        [context setShouldAntialias:NO];
    
        // draw here
    
        [context restoreGraphicsState];
    
        [super drawRect:dirtyRect];
    }
    
  14. $ python -c "import AppKit; print AppKit.NSAppKitVersionNumber"
    

    1187.0

  15. [[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:path];
    
  16. if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5) {
      /* On a 10.5.x or earlier system */
    } else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6) {
      /* On a 10.6 - 10.6.x system */
    } else {
      /* Lion or later system */
    }
    

    http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html

  17. Download an Xcode demo project: CocoaPython.zip.

    #import
    #import

    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        
        /* initialization */
        
        Py_Initialize();
        
        PyObject *sysModule = PyImport_ImportModule("sys");
        PyObject *sysModuleDict = PyModule_GetDict(sysModule);
        PyObject *pathObject = PyDict_GetItemString(sysModuleDict, "path");
        PyObject_CallMethod(pathObject, "insert", "(is)", 0, "../../");
        
        Py_DECREF(sysModule); // borrowed reference
        
        /* import and instantiate Cat */
        
        // from Cat import *
        PyObject *CatModule = PyImport_ImportModule("Cat");
        
        // c = Cat()
        PyObject *Cat = PyDict_GetItemString(PyModule_GetDict(CatModule), "Cat");
        PyObject *cat = PyObject_CallObject(Cat, NULL);
        
        Py_DECREF(CatModule);
        Py_DECREF(Cat);
        
        /* use Cat instance methods */
        
        // c.scream()
        // c.set_name("charly")
        // c.print_family("jack", "cathy", 4)
        
        PyObject_CallMethod(cat,"scream",NULL);
        PyObject_CallMethod(cat,"set_name", "(s)", "charly");
        PyObject_CallMethod(cat,"print_family", "(ssi)", "jack", "cathy", 4);
        
        // c.is_asleep()
        PyObject *is_asleep = PyObject_CallMethod(cat,"is_asleep",NULL);
        BOOL isAsleep = (Py_True == is_asleep);
        Py_DECREF(is_asleep);
        NSLog(@"%d", isAsleep);
        
        // c.name()
        PyObject *name = PyObject_CallMethod(cat,"name",NULL);
        NSString *catName = [NSString stringWithCString: PyString_AsString(name)];
        Py_DECREF(name);
        NSLog(@"%@", catName);
        
        /* termination */
        
        Py_DECREF(cat);
        Py_Finalize();
        
        [pool release];
        return 0;
    }