Package pycocoa :: Module nstypes
[frames] | no frames]

Module nstypes

ObjC classes NS... and conversions from NS... ObjC to Python instances.


Version: 20.01.08

Classes
  NSPoint_t
ObjC struct with fields x and y (both CGFloat_t).
  NSRect4_t
ObjC struct, like NSRect_t with different signature and properties.
  NSExceptionHandler_t
  NSDecimal
Optimized, Python NSDecimalNumber class.
  NSStr
Python wrapper for the ObjC NS[Constant]String.
  at
Acronym NSStr.
Functions
 
isAlias(path)
Resolve a macOS file or folder alias.
 
isLink(path)
Resolve a file or folder link or alias.
 
isNone(obj)
Return True if obj is None, NSMain.nil, NSMain.Null, etc.
 
nsArray2listuple(ns, ctype=<class 'ctypes.c_void_p'>)
Create a Python list or tuple from an NS[Mutable]Array.
 
nsBoolean2bool(ns, dflt=missing)
Create a Python bool from an NSBoolean.
 
nsBundleRename(ns_title, match='Python')
Change the bundle title if the current title matches.
 
nsData2bytes(ns, dflt='')
Create Python bytes from NSData.
 
nsDecimal2decimal(ns)
Create a Python Decimal from an NSDecimalNumber.
 
nsDictionary2dict(ns, ctype_keys=<class 'ctypes.c_void_p'>, ctype_vals=<class 'ctypes.c_void_p'>)
Create a Python dict from an NS[Mutable]Dictionary.
 
nsIter(ns, reverse=False)
Iterate over an NS.. ObjC objects's (reverse) enumerator.
 
nsIter2(ns, reverse=False)
Iterate over an NS.. ObjC objects's (reverse) enumerator.
 
nsLog(ns_fmt, *ns_args)
Formatted ObjC write to the console.
 
nsLogf(fmt, *args)
Formatted write to the console.
 
nsNull2none(ns)
Return Python None for an NS/CFNull or nil.
 
nsNumber2num(ns, dflt=missing)
Create a Python Decimal, int or float from an NSNumber.
 
nsOf(inst)
Return the .NS ObjC object of a Python wrapper or Type instance.
 
nsSet2set(ns, ctype=<class 'ctypes.c_void_p'>)
Create a Python set or frozenset from an NS[Mutable]Set.
 
nsString2str(ns, dflt=None)
Create a Python str or unicode from an NS[Mutable]Str[ing].
 
nsTextSize3(text, ns_font=None)
Return the size of a multi-line text.
 
nsTextView(text, ns_font)
Return an NSTextView for the given text string.
 
nsThrow(ns_exception)
Mimick ObjC's @throw NSException to raise an exception.
 
nsUncaughtExceptionHandler(handler)
Install an ObjC NSException handler, the handler signature must match NSExceptionHandler_t def handler(ns_exc): ....
 
nsURL2str(ns)
Create a Python str from NSURL string.
 
ns2py(ns, dflt=missing)
Convert (an instance of) an ObjC class to an instance of the equivalent Python standard type or wrapper and value.
 
ns2Type(ns)
Convert an NS... ObjC object to an instance of the corresponding Python Type and value.
Variables
  __all__ = _ALL_LAZY.nstypes
  NSAlert = ObjCClass('NSAlert')
  NSApplication = ObjCClass('NSApplication')
  NSArray = ObjCClass('NSArray')
  NSAttributedString = ObjCClass('NSAttributedString')
  NSAutoreleasePool = ObjCClass('NSAutoreleasePool')
  NSBezierPath = ObjCClass('NSBezierPath')
  NSBundle = ObjCClass('NSBundle')
  NSColor = ObjCClass('NSColor')
  NSConcreteNotification = ObjCClass('NSConcreteNotification')
  NSConstantString = ObjCClass('NSConstantString')
  NSData = ObjCClass('NSData')
  NSDecimalNumber = ObjCClass('NSDecimalNumber')
  NSDictionary = ObjCClass('NSDictionary')
  NSDockTile = ObjCClass('NSDockTile')
  NSEnumerator = ObjCClass('NSEnumerator')
  NSError = ObjCClass('NSError')
  NSException = ObjCClass('NSException')
  NSFont = ObjCClass('NSFont')
  NSFontDescriptor = ObjCClass('NSFontDescriptor')
  NSFontManager = ObjCClass('NSFontManager')
  NSFontPanel = ObjCClass('NSFontPanel')
  NSImage = ObjCClass('NSImage')
  NSImageView = ObjCClass('NSImageView')
  NSLayoutManager = ObjCClass('NSLayoutManager')
  NSMenu = ObjCClass('NSMenu')
  NSMenuItem = ObjCClass('NSMenuItem')
  NSMutableArray = ObjCClass('NSMutableArray')
  NSMutableData = ObjCClass('NSMutableData')
  NSMutableDictionary = ObjCClass('NSMutableDictionary')
  NSMutableSet = ObjCClass('NSMutableSet')
  NSMutableString = ObjCClass('NSMutableString')
  NSNotification = ObjCClass('NSNotification')
  NSNotificationCenter = ObjCClass('NSNotificationCenter')
  NSNull = ObjCClass('NSNull')
  NSNumber = ObjCClass('NSNumber')
  NSObject = ObjCClass('NSObject')
  NSOpenPanel = ObjCClass('NSOpenPanel')
  NSPageLayout = ObjCClass('NSPageLayout')
  NSPrinter = ObjCClass('NSPrinter')
  NSPrintInfo = ObjCClass('NSPrintInfo')
  NSPrintOperation = ObjCClass('NSPrintOperation')
  NSPrintPanel = ObjCClass('NSPrintPanel')
  NSSavePanel = ObjCClass('NSSavePanel')
  NSScreen = ObjCClass('NSScreen')
  NSScrollView = ObjCClass('NSScrollView')
  NSSet = ObjCClass('NSSet')
  NSStatusBar = ObjCClass('NSStatusBar')
  NSString = ObjCClass('NSString')
  NSTableColumn = ObjCClass('NSTableColumn')
  NSTableView = ObjCClass('NSTableView')
  NSTextField = ObjCClass('NSTextField')
  NSTextView = ObjCClass('NSTextView')
  NSThread = ObjCClass('NSThread')
  NSURL = ObjCClass('NSURL')
  NSView = ObjCClass('NSView')
  NSWindow = ObjCClass('NSWindow')
  NSBoolean = NSNumber.numberWithBool_
  NSDouble = NSNumber.numberWithDouble_
  NSFloat = NSNumber.numberWithDouble_
  NSInt = NSNumber.numberWithInt_
  NSLong = NSNumber.numberWithLong_
  NSLongLong = NSNumber.numberWithLongLong_
  NSMain = NSMain.Application=NSApplication(<Id_t at 0x7fc65d607...
Global NS... singletons (const).
Function Details

isAlias(path)

 

Resolve a macOS file or folder alias.

Parameters:
  • path - The alias name (str or NSStr).
Returns:
The alias' target (str) or None if path isn't a macOS alias.

See Also: mac-alias and here.

isLink(path)

 

Resolve a file or folder link or alias.

Parameters:
  • path - The link or alias name (str or NSStr).
Returns:
The link's or alias' target (str) or None if path isn't a link or alias.

isNone(obj)

 

Return True if obj is None, NSMain.nil, NSMain.Null, etc.

Parameters:
Returns:
True or False (bool).

nsArray2listuple(ns, ctype=<class 'ctypes.c_void_p'>)

 

Create a Python list or tuple from an NS[Mutable]Array.

Parameters:
  • ns - The NS[Mutable]Array (ObjCInstance).
  • ctype - The array item type (ctypes).
Returns:
The array (list or tuple).

nsBoolean2bool(ns, dflt=missing)

 

Create a Python bool from an NSBoolean.

Parameters:
  • ns - The NSBoolean (ObjCInstance).
  • dflt - Default for a missing, unobtainable value (missing).
Returns:
The bool (bool) of dlft.
Raises:
  • TypeError - Unexpected NumberType.

nsBundleRename(ns_title, match='Python')

 

Change the bundle title if the current title matches.

Parameters:
  • ns_title - New bundle title (NSStr).
  • match - Optional, previous title to match (str).
Returns:
The previous bundle title (str) or None.

Note: Used to mimick NSApplication.setTitle_(ns_title), the name of an App shown in the menu bar.

nsData2bytes(ns, dflt='')

 

Create Python bytes from NSData.

Parameters:
  • ns - The NSData (ObjCInstance).
  • dflt - Default for empty NSData (bytes).
Returns:
The bytes (bytes) or dflt.

nsDecimal2decimal(ns)

 

Create a Python Decimal from an NSDecimalNumber.

Parameters:
Returns:
The decimal (Decimal).
Raises:
  • ValueError - If ns not an NSNumber.

nsDictionary2dict(ns, ctype_keys=<class 'ctypes.c_void_p'>, ctype_vals=<class 'ctypes.c_void_p'>)

 

Create a Python dict from an NS[Mutable]Dictionary.

Parameters:
  • ns - The NSDictionary instance (ObjCInstance).
  • ctype_keys - The dictionary keys type (ctypes).
  • ctype_vals - The dictionary values type (ctypes).
Returns:
The dict (dict).

nsIter(ns, reverse=False)

 

Iterate over an NS.. ObjC objects's (reverse) enumerator.

Parameters:
  • ns - The NS.. object to iterate over (ObjCInstance).
  • reverse - Iterate in reverse order (bool), forward otherwise.
Returns:
Each object (NS...).

nsIter2(ns, reverse=False)

 

Iterate over an NS.. ObjC objects's (reverse) enumerator.

Parameters:
  • ns - The NS.. object to iterate over (ObjCInstance).
  • reverse - Iterate in reverse order (bool), foward otherwise.
Returns:
Each object as 2-Tuple (py, ns) where py is a Python Type instance and ns the ObjC object NS....

nsLog(ns_fmt, *ns_args)

 

Formatted ObjC write to the console.

Parameters:
  • ns_fmt - A printf-like format string (NSStr).
  • ns_args - Optional arguments to format (all positional).

Note: The ns_fmt and all ns_args must be NS... ObjC instances.

nsLogf(fmt, *args)

 

Formatted write to the console.

Parameters:
  • fmt - A printf-like format string (str).
  • args - Optional arguments to format (all positional).

nsNull2none(ns)

 

Return Python None for an NS/CFNull or nil.

Parameters:
Returns:
The singleton (None).
Raises:
  • ValueError - If ns not isNone.

nsNumber2num(ns, dflt=missing)

 

Create a Python Decimal, int or float from an NSNumber.

Parameters:
  • ns - The NSNumber (ObjCInstance).
  • dflt - Default for missing, unobtainable value (missing).
Returns:
The number (Decimal, int or float).
Raises:
  • TypeError - Unexpected NumberType.
  • ValueError - If ns not an NSNumber.

nsOf(inst)

 

Return the .NS ObjC object of a Python wrapper or Type instance.

Parameters:
Returns:
The .NS object (NS...).
Raises:
  • TypeError - No .NS for this inst.

nsSet2set(ns, ctype=<class 'ctypes.c_void_p'>)

 

Create a Python set or frozenset from an NS[Mutable]Set.

Parameters:
  • ns - The NS[Mutable]Set (ObjCInstance).
  • ctype - The set item type (ctypes).
Returns:
The set (set or frozenset).

nsString2str(ns, dflt=None)

 

Create a Python str or unicode from an NS[Mutable]Str[ing].

Parameters:
Returns:
The string (str or unicode) or dflt.

nsTextSize3(text, ns_font=None)

 

Return the size of a multi-line text.

Parameters:
  • text - The text (str), including lineseparators.
  • ns_font - The text font (NSFont) or None.
Returns:
3-Tuple (width, height, lines) in (pixels, pixels) or in (characters, lines, lines) if ns_font is None.

nsThrow(ns_exception)

 

Mimick ObjC's @throw NSException to raise an exception.

Parameters:
  • ns_exception - The exception to raise (NSException).

nsUncaughtExceptionHandler(handler)

 

Install an ObjC NSException handler, the handler signature must match NSExceptionHandler_t def handler(ns_exc): ....

Parameters:
  • handler - A callable (NSExceptionHandler_t).
Returns:
Previously installed handler (NSExceptionHandler_t).

Note: Faults like SIGILL, SIGSEGV, etc. do not throw uncaught NSExceptions and will not invoke this handler.

nsURL2str(ns)

 

Create a Python str from NSURL string.

Parameters:
Returns:
The URL as string (str).

ns2py(ns, dflt=missing)

 

Convert (an instance of) an ObjC class to an instance of the equivalent Python standard type or wrapper and value.

Parameters:
  • ns - The NS... (ObjCInstance).
  • dflt - Default for unhandled, unexpected NS...s (missing).
Returns:
The value (Python type) or dflt if provided.
Raises:
  • TypeError - Unhandled, unexpected TypeID.

Note: Conversion map:

  • NSArray -> tuple
  • NSBoolean -> bool
  • NSConstantString -> str
  • NSData -> bytes
  • NSDecimalNumber -> Decimal
  • NSDictionary -> dict
  • NSMutableArray -> list
  • NSMutableSet -> set
  • NSMutableString -> str
  • NSNumber -> int or float
  • NSNull -> None
  • NSSet -> frozenset
  • NSString -> str
  • NSStr -> str

See Also: Converting values between Python and Objective-C

ns2Type(ns)

 

Convert an NS... ObjC object to an instance of the corresponding Python Type and value.

Parameters:
Returns:
The value (Python Type).

Variables Details

NSMain

Global NS... singletons (const).
Value:
NSMain.Application=NSApplication(<Id_t at 0x7fc65d607c20>) of 0x7fc65d\
36bc60,
      .BooleanNO=NSBoolean(<Id_t at 0x7fc65d607e60>) of 0x7fff86785230\
,
      .BooleanYES=NSBoolean(<Id_t at 0x7fc65d611200>) of 0x7fff8678522\
0,
      .Bundle=NSBundle(<Id_t at 0x7fc65d611320>) of 0x7fc657e78400,
      .BundleName=NSConstantString('CFBundleName'),
...