def main(): global wk UIScreen = objc_util.ObjCClass('UIScreen') if len(UIScreen.screens()) > 1: second_screen = UIScreen.screens()[1] second_screen.overscanCompensation = 0 bounds = second_screen.bounds() UIWindow = objc_util.ObjCClass('UIWindow') second_window = UIWindow.alloc().initWithFrame_(bounds) second_window.setScreen(second_screen) second_window.makeKeyAndVisible() wk = objc_util.ObjCClass('WKWebView').alloc().initWithFrame_(objc_util.CGRect((0, 0), (second_screen.bounds().size.width, second_screen.bounds().size.height - 1))).autorelease() second_window.addSubview(wk) request = objc_util.ObjCClass('NSURLRequest').alloc().init() nsurl = objc_util.nsurl('http://localhost:8080') x = request.initWithURL_(nsurl) wk.loadRequest_(x) else: print('No secondary screen detected. Connect your Looking Glass.') v.close() s.stop_server() quit()
def _configure_window(): '''Returns external screen window (UIWindow) if available. Otherwise it returns None. ''' global _screen, _window if _screen is None: # Useful during initialization where external screen is already connected # -> notification will not fire screens = objc_util.ObjCClass('UIScreen').screens() if len(screens) > 1: _screen = screens[1] else: _logger.debug( 'Unable to configure window, external screen not available') return None if _screen == None: _logger.debug( 'Unable to configure window, external screen not available') return None if _window is None: UIWindow = objc_util.ObjCClass('UIWindow') _window = UIWindow.alloc().initWithFrame(_screen.bounds()) _window.setScreen(_screen) _window.setHidden(False) _logger.debug('External screen window configured') return _window
def main(): NSMutableDictionary = objc_util.ObjCClass('NSMutableDictionary') settings = NSMutableDictionary.dictionary() kAudioFormatMPEG4AAC = 1633772320 settings.setObject_forKey_(kAudioFormatMPEG4AAC, 'AVFormatIDKey') settings.setObject_forKey_(44100.0, 'AVSampleRateKey') settings.setObject_forKey_(2, 'AVNumberOfChannelsKey') AVAudioRecorder = objc_util.ObjCClass('AVAudioRecorder') recorder = AVAudioRecorder.alloc() output_path = os.path.abspath('Recording.m4a') out_url = objc_util.nsurl(output_path) recorder = recorder.initWithURL_settings_error_(out_url, settings, None) started_recording = recorder.record() if started_recording: print( 'Recording started, press the "stop script" button to end recording...' ) try: while True: pass except KeyboardInterrupt: print('Stopping...') recorder.stop() recorder.release() print('Stopped recording.') import console console.quicklook(os.path.abspath('Recording.m4a'))
def get(self, url=None, auth=None, headers=None, params=None): # Make url if params: params_encoded = urlencode(params) else: params_encoded = "" url = objc_util.nsurl("{}?{}".format(url, params_encoded)) #request = objc_util.ObjCClass("NSURLRequest").request(URL=url) request = objc_util.ObjCClass( 'NSMutableURLRequest').alloc().initWithURL_(url) # Make headers if headers: for key in headers: request.setValue_forHTTPHeaderField_(headers[key], key) if auth: userName, password = auth authStr = "%s:%s" % (userName, password) authencode = base64.b64encode(bytes(authStr)) request.addValue_forHTTPHeaderField_("Basic %s" % authencode, "Authorization") configuration = objc_util.ObjCClass( "NSURLSessionConfiguration").defaultSessionConfiguration() session = objc_util.ObjCClass( "NSURLSession").sessionWithConfiguration_(configuration) completionHandler = objc_util.ObjCBlock( self.responseHandlerBlock, restype=None, argtypes=[c_void_p, c_void_p, c_void_p, c_void_p]) objc_util.retain_global(completionHandler) #dataTask = session.dataTask(Request=request, completionHandler=completionHandler) dataTask = session.dataTaskForRequest_completion_( request, completionHandler) dataTask.resume() # Wait for completions wait = True while wait: if self.data != None: wait = False return json.loads(self.data) elif self.error != None: wait = False raise RequestsException(["Error in request", self.error])
def _add_shortcut(shortcut, function, title=None): """Bind a function to a keyboard shortcut.""" # Wrap function to accept and ignore arguments def wrapper(*args, **kwargs): function() # Parse shortcut tokens = _tokenize_shortcut_string(shortcut) _validate_tokens(tokens) modifiers = tokens[:-1] inp = tokens[-1] # Process components mod_bitmask = functools.reduce(operator.ior, [_modifiers[mod] for mod in modifiers], 0) if inp in _special_keys: inp = _special_keys[inp] # Make the command sel = _add_method(_controller, wrapper) kc = objc_util.ObjCClass("UIKeyCommand") if title is not None: c = kc.keyCommandWithInput_modifierFlags_action_discoverabilityTitle_( inp, mod_bitmask, sel, title) else: c = kc.keyCommandWithInput_modifierFlags_action_(inp, mod_bitmask, sel) _registered_commands[frozenset(tokens)] = cp _controller.addKeyCommand_(c)
def add_text(self, text, color='black', new_line=True): import objc_util import traceback self.limit.acquire() try: text = unicode(text) self.all_text += text + "\n" if new_line else text self.str_object = objc_util.ObjCClass( 'NSMutableAttributedString').alloc().initWithString_( self.all_text) if self.colors.has_key(color): range = len(text) + 1 if new_line else len(text) start = len(self.all_text) - range color_obj = self.colors[color] self.color_data.append((color_obj, start, range)) for color_obj, start, range in self.color_data: self.str_object.addAttribute_value_range_( 'NSColor', color_obj, objc_util.NSRange(start, range)) self.setAttribs() except: print('{}のテキスト処理に失敗'.format(text)) print(traceback.format_exc()) finally: self.limit.release()
def VersionInStatusBar(version=False): # called at begin of a script with version=text # end version=False root, ext = os.path.splitext(sys.argv[0]) # script path without .py script_name = os.path.basename(root) # script name without the path app = objc_util.UIApplication.sharedApplication() w = objc_util.ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = w.rootViewController().view() bar = app.statusBar() b = bar.bounds() sv = bar.subviews() for v in sv: if v._get_objc_classname().startswith(b'SUILabel_PY3'): v.removeFromSuperview() del v if not version: # remove version label return lbl = ui.Label() lbl.frame = (200,2,23,16) lbl.text_color = 'blue' lbl.border_width = 1 lbl.border_color = 'blue' lbl.font = ('Courier-Bold',12) lbl.text = script_name+': version='+version lbl.width = ui.measure_string(lbl.text,font=lbl.font)[0] lbl = lbl bar.addSubview_(lbl)
def get_battery_info(): device = objc_util.ObjCClass('UIDevice').currentDevice() device.setBatteryMonitoringEnabled_(True) try: return battery_info(int(device.batteryLevel() * 100), battery_states[device.batteryState()]) finally: device.setBatteryMonitoringEnabled_(False)
def get_battery_info(): device = objc_util.ObjCClass('UIDevice').currentDevice() device.setBatteryMonitoringEnabled_(True) try: return battery_info( int(device.batteryLevel() * 100), 'unknown unplugged charging full'.split()[device.batteryState()]) finally: device.setBatteryMonitoringEnabled_(False)
def setFilePath(self, fpath: str): if not os.path.exists(fpath): ex_fpath = os.path.expanduser(fpath) if os.path.exists(ex_fpath): fpath = ex_fpath else: raise FileNotFoundError(fpath) self.player = objc_util.ObjCClass('AVAudioPlayer').alloc() self.player.initWithContentsOfURL_error_(objc_util.nsurl(fpath), None) self.fpath = fpath self.isPlayed = False
def textureSelect(sender): global wk, rgbData if textureSelector.selected_index == 0: rgbData = chosen_pic_photo_image_buffer.getvalue() elif textureSelector.selected_index == 1: rgbData = chosen_pic_colormap_image_buffer.getvalue() request = objc_util.ObjCClass('NSURLRequest').alloc().init() nsurl = objc_util.nsurl('http://localhost:8080') x = request.initWithURL_(nsurl) wk.loadRequest_(x)
def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self._view = objc_util.ObjCClass("MKMapView").new() self._view.setFrame_( objc_util.CGRect(objc_util.CGPoint(0, 0), objc_util.CGSize(self.width, self.height))) self._view.setAutoresizingMask_(18) # W+H objc_util.ObjCInstance(self).addSubview_(self._view) self.animated = True
def _stop_notification_listener(): global _listener if _listener is None: _logger.debug('External screen notification listener is not running') return NSNotificationCenter = objc_util.ObjCClass('NSNotificationCenter') default_center = NSNotificationCenter.defaultCenter() default_center.removeObserver(_listener) _listener = None _logger.debug('External screen notification listener stopped')
def __init__(self, url=None, params=None): self.data = None if params: params_encoded = urlencode(params) else: params_encoded = "" url = objc_util.nsurl("{}?{}".format(url, params_encoded)) request = objc_util.ObjCClass("NSURLRequest").request(URL=url) configuration = objc_util.ObjCClass( "NSURLSessionConfiguration").defaultSessionConfiguration() session = objc_util.ObjCClass("NSURLSession").session( Configuration=configuration) completionHandler = objc_util.ObjCBlock( self.responseHandlerBlock, restype=None, argtypes=[c_void_p, c_void_p, c_void_p, c_void_p]) objc_util.retain_global(completionHandler) dataTask = session.dataTask(Request=request, completionHandler=completionHandler) dataTask.resume()
def to_png(self): global depthSource ctx = objc_util.ObjCClass('CIContext').context() try: extent = self.ci_img.extent() except: if allow_ML: raise('The selected portrait photo does not contain a depth map.') else: print('The selected portrait photo does not contain a depth map.') quit() m = ctx.outputImageMaximumSize() cg_img = ctx.createCGImage_fromRect_(self.ci_img, extent) ui_img = objc_util.UIImage.imageWithCGImage_(cg_img) png_data = objc_util.uiimage_to_png(objc_util.ObjCInstance(ui_img)) depthSource = 'Embedded' return png_data
def get_presented_size(mode,hide_title_bar=False): ''' see https://forum.omz-software.com/topic/1618/any-ios-device/7''' f=objc_util.ObjCClass('UIApplication').sharedApplication().keyWindow().frame() sz= ui.Size(f.size.width,f.size.height) if sz[1]<=320: status_height=0 title_height=32 else: status_height=20 title_height=44 if mode=='sheet': maxsize=min( sz-(0,title_height*(not hide_title_bar))) return ui.Size(maxsize,maxsize) elif mode=='panel': return sz-(0,status_height+(title_height*(not hide_title_bar))) elif mode=='fullscreen': return sz-(0,(title_height*(not hide_title_bar)))
def __init__(self, frame, **value): self.auto_scroll = True self.text_view = ui.TextView() self.text_view.frame = self.frame = frame self.text_view.flex = "WHLRTB" self.flex = "WHLRTB" self.text_view.frame = self.bounds self.add_subview(self.text_view) self.limit = threading.Semaphore(1) for param in value.keys(): if param == "auto_scroll": self.auto_scroll = value[param] if param == "editable": self.text_view.editable = value[param] if param == "selectable": self.text_view.selectable = value[param] self.all_text = '' self.color_data = [] self.UIColor = objc_util.ObjCClass('UIColor') '''for _ in dir(UIColor): if len(_)<15 and "Color" in _: print _''' self.colors = { 'red': objc_util.UIColor.redColor(), 'green': objc_util.UIColor.greenColor(), 'blue': objc_util.UIColor.blueColor(), 'cyan': objc_util.UIColor.cyanColor(), 'magenta': objc_util.UIColor.magentaColor(), 'black': objc_util.UIColor.blackColor(), 'yellow': objc_util.UIColor.yellowColor(), 'orange': objc_util.UIColor.orangeColor(), 'purple': objc_util.UIColor.purpleColor(), 'white': objc_util.UIColor.whiteColor(), 'blown': objc_util.UIColor.brownColor(), 'darkgray': objc_util.UIColor.darkGrayColor(), 'lightgray': objc_util.UIColor.lightGrayColor(), 'gray': objc_util.UIColor.grayColor() }
def modeSelect(sender): global wk, mode, control_startcamera, control_sphere, cameracontrol if modeSelector.selected_index == 0: mode = mesh control_startcamera = '0, 0, 220' control_sphere = '14.667, 8, 8' elif modeSelector.selected_index == 1: mode = wireframe control_startcamera = '0, 0, 15' control_sphere = '1, 8, 8' elif modeSelector.selected_index == 2: mode = pointcloud control_startcamera = '0, 0, 450' control_sphere = '30, 8, 8' request = objc_util.ObjCClass('NSURLRequest').alloc().init() nsurl = objc_util.nsurl('http://localhost:8080') x = request.initWithURL_(nsurl) wk.loadRequest_(x) cameracontrol.load_url('http://localhost:8080/cameracontrol.html')
def _start_notification_listener(): global _listener if _listener is not None: _logger.debug( 'External screen notification listener is already running') return methods = [screenDidConnectNotification_, screenDidDisconnectNotification_] listener_class = objc_util.create_objc_class('ExternalScreenListener', methods=methods) _listener = listener_class.alloc().init() NSNotificationCenter = objc_util.ObjCClass('NSNotificationCenter') default_center = NSNotificationCenter.defaultCenter() default_center.addObserver_selector_name_object_( _listener, objc_util.sel('screenDidConnectNotification:'), 'UIScreenDidConnectNotification', None) default_center.addObserver_selector_name_object_( _listener, objc_util.sel('screenDidDisconnectNotification:'), 'UIScreenDidDisconnectNotification', None) _logger.debug('External screen notification listener started')
def __init__(self, chosen_pic): self.MLModel = objc_util.ObjCClass('MLModel') self.VNCoreMLModel = objc_util.ObjCClass('VNCoreMLModel') self.VNCoreMLRequest = objc_util.ObjCClass('VNCoreMLRequest') self.VNImageRequestHandler = objc_util.ObjCClass('VNImageRequestHandler') result = self.classify_asset(chosen_pic) if result: resultString = str(result) resultWidth = int(resultString[resultString.find('width=') + 6:resultString.find(' ', resultString.find('width=') + 6)]) resultHeight = int(resultString[resultString.find('height=') + 7:resultString.find(' ', resultString.find('height=') + 7)]) CIImage = objc_util.ObjCClass('CIImage') pixelBuffer = result.pixelBuffer ci_img = CIImage.imageWithCVPixelBuffer_(pixelBuffer()) ctx = objc_util.ObjCClass('CIContext').context() cg_img = ctx.createCGImage_fromRect_(ci_img, objc_util.CGRect(objc_util.CGPoint(0, 0), objc_util.CGSize(resultWidth, resultHeight))) ui_img = objc_util.UIImage.imageWithCGImage_(cg_img) self.png_data = objc_util.uiimage_to_png(objc_util.ObjCInstance(ui_img))
# https://files.slack.com/files-pri/T0M854UF2-F2JESH4HK/notificationstuff.py # import ctypes import objc_util import time UNMutableNotificationContent = objc_util.ObjCClass("UNMutableNotificationContent") UNNotificationAction = objc_util.ObjCClass("UNNotificationAction") UNNotificationCategory = objc_util.ObjCClass("UNNotificationCategory") UNNotificationRequest = objc_util.ObjCClass("UNNotificationRequest") UNTimeIntervalNotificationTrigger = objc_util.ObjCClass("UNTimeIntervalNotificationTrigger") UNUserNotificationCenter = objc_util.ObjCClass("UNUserNotificationCenter") def userNotificationCenter_willPresentNotification_withCompletionHandler_(self, _cmd, unc, notification, handler): pass def userNotificationCenter_didReceiveNotificationResponse_withCompletionHandler_(self, _cmd, unc, response, handler): print(objc_util.ObjCInstance(response)) DGUNUNCDelegate = objc_util.create_objc_class( "DGUNUNCDelegate", methods=[ userNotificationCenter_willPresentNotification_withCompletionHandler_, userNotificationCenter_didReceiveNotificationResponse_withCompletionHandler_ ], protocols=[ "UNUserNotificationCenterDelegate" ] )
""" __author__ = "Lukas Kollmer<*****@*****.**>" __copyright__ = "Copyright (c) 2016 Lukas Kollmer<*****@*****.**>" from pythonista import _utils from pythonista import defaults _utils.guard_objc_util() import objc_util import ctypes # --- Custom Fonts UIFont = objc_util.ObjCClass("UIFont") OMBarButton = objc_util.ObjCClass('OMBarButton') UIButton = objc_util.ObjCClass('UIButton') PA2UniversalTextEditorViewController = objc_util.ObjCClass( "PA2UniversalTextEditorViewController") PA2EmptyTabViewController = objc_util.ObjCClass("PA2EmptyTabViewController") CTFontManagerRegisterFontsForURL = objc_util.c.CTFontManagerRegisterFontsForURL CTFontManagerRegisterFontsForURL.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p ] CTFontManagerRegisterFontsForURL.restype = ctypes.c_bool CFURLCreateWithString = objc_util.c.CFURLCreateWithString CFURLCreateWithString.argtypes = [
# coding: utf-8 # https://forum.omz-software.com/topic/2460/mac-address-using-uuid-getnode #from uuid import getnode #print getnode() # You can get your device's name: import objc_util str(objc_util.ObjCClass('UIDevice').currentDevice().name()) # Which mean's you need to give your devices unique names, but that doesn't seem too objectionable. # You could also use str( objc_util.ObjCClass( 'UIDevice').currentDevice().identifierForVendor().UUIDString()) # Which I haven't tested through reboots, but this is supposed to be the Apple approved way of getting unique id's. It does not survive remove/reinstalls apparantly. Of course, you could generate your own random uuid, and store it in the keychain -- either way you need to know this in advance before you can deploy anything. Storing your own would make you robust to changes in identifierForVendor across ios versions.
import platform import ctypes #needed for workaround import objc_util #used if workaround is unnecessary workaround = True if platform.machine( ) == 'iPhone8,1' else False #iphone 6s is the first device with haptic engine and supports haptic feedback differently than newer models #for workaround and vibration: AudioServicesPlaySystemSound = ctypes.CDLL(None).AudioServicesPlaySystemSound impactID, selectionID, notificationID, vibrateID = 1519, 1520, 1521, 4095 #if workaround is unnecessary: objc_util.NSBundle.bundle( Path="/System/Library/Frameworks/UIKit.framework").load() UIImpactFeedbackGenerator = objc_util.ObjCClass( 'UIImpactFeedbackGenerator').new() UISelectionFeedbackGenerator = objc_util.ObjCClass( 'UISelectionFeedbackGenerator').new() UINotificationFeedbackGenerator = objc_util.ObjCClass( 'UINotificationFeedbackGenerator').new() def selectionChanged(workaround=workaround): if workaround: AudioServicesPlaySystemSound(selectionID) else: #UISelectionFeedbackGenerator.prepare() UISelectionFeedbackGenerator.selectionChanged() def impactOccured(workaround=workaround): if workaround:
import objc_util as objc UIFont = objc.ObjCClass('UIFont') FONT_FAMILIES = [i.cString().decode() for i in UIFont.familyNames()] FONT_FAMILIES.extend(['<System>', '<System-Bold>']) UIColor = objc.ObjCClass('UIColor') def validate_font(value): if type(value) is tuple and len(value) == 2: x, y = value if isinstance(y, (int, float)) and type(x) is str: if x in FONT_FAMILIES and y > 0: return True return False def validate_color(value): if type(value) is str: if len(value) == 7 or len(value) == 9 and value[0] == '#': if all(i in '0123456789abcdef' for i in value[1:]): return True elif UIColor.colorWithName_(value): return True elif type(value) is tuple: if 3 <= len(value) <= 4 and all( isinstance(i, (int, float)) for i in value): if all(0 <= i <= 1 for i in value): return True elif type(value) == type(None):
import ui import objc_util import console from tinysync import track from scripter import * from genr import genr import sfsymbol import gestures import anchor import onedriver import onedrive_ids UIScreen = objc_util.ObjCClass('UIScreen') UIWindow = objc_util.ObjCClass('UIWindow') UIColor = objc_util.ObjCClass('UIColor') class SlideShow(ui.View): def __init__(self, folder='~/Documents/slideshow', keep=True, **kwargs): super().__init__(**kwargs) self.slide_delay = 5 # seconds self.label_display_time = 3 # seconds self.label_font = 'Source Sans Pro' self.transition = self.transition_slide_and_fade self.airplaying = False self.keep = keep self.target_strategy = self.quadrant_targets self.position_jigger = 10
# coding: utf-8 # https://github.com/jsbain/objc_hacks/blob/master/objcstuff.py import objc_util, ui v = ui.View() vv = objc_util.ObjCInstance(v._objc_ptr) wnd = objc_util.ObjCClass('UIWindow').alloc() r = objc_util.CGRect(objc_util.CGPoint(0, 0), objc_util.CGSize(100, 100)) wnd.initWithFrame_(r)
import ui, console, objc_util, keychain from types import SimpleNamespace as ns font = 'Apple SD Gothic Neo' UIColor = objc_util.ObjCClass('UIColor') objc_black = UIColor.darkGrayColor().CGColor() light_theme = ns(front='black', back='white', secondary='darkgrey', shadow=objc_black) dark_theme = ns(front='white', back='black', secondary='grey', shadow=objc_black) blue_theme = ns(front='#1976D2', back='white', secondary='#03A9F4', shadow=objc_black) green_theme = ns(front='#009688', back='white', secondary='#80CBC4', shadow=objc_black) red_theme = ns(front='#E53935', back='white', secondary='#FFA726', shadow=objc_black) cyan_dark_theme = ns(front='#4DD0E1', back='black', secondary='#00897B', shadow=objc_black)
import ctypes from functools import partial import objc_util UIMenu = objc_util.ObjCClass('UIMenu') UIAction = objc_util.ObjCClass('UIAction') class Action: DISABLED = 1 DESTRUCTIVE = 2 HIDDEN = 4 REGULAR = 0 SELECTED = 1 def __init__(self, title, handler, image=None, attributes=None, state=False, discoverability_title=None, ): self._menu = None self._handler = handler self._title = title
import ast import ctypes from itertools import chain import types import bs4 import objc_util import ui NSMutableAttributedString = objc_util.ObjCClass('NSMutableAttributedString') UIFont = objc_util.ObjCClass('UIFont') NSShadow = objc_util.ObjCClass('NSShadow') def get_fonts(): families = [str(family) for family in UIFont.familyNames()] fonts = [ str(font).lower() for fonts in [UIFont.fontNamesForFamilyName_(family) for family in families] for font in fonts ] return (font_name.lower() for font_name in chain(families, fonts)) class RichLabel: # No default root default = None