Using pyobjc on Python for school project.

I am trying to use PyObjC to create a Python program for school that can enroll students using the touchid on the mac but I am not able to use the packages Foundation or AppKit. Would someone please explain to me how to properly install these packages in order to do this or if there are better ones to use. Would greatly appreciate it.

Posted on Apr 23, 2023 11:55 AM

Reply
5 replies

Apr 23, 2023 1:18 PM in response to gtorres_2000

If you are on macOS prior to macOS Monterey 12.3, then you have the Python/Objective-C bridge already inherent with Python 2.7.16. After Monterey 12.3, Apple removed the [all] Python 2.7.16 distribution from the operating system and withit went the PyObjc bridge.


If you installed Python3.11.3 from Python.org, it too must have pyobjc installed in order to have functional pyobjc.


/usr/local/bin/pip3 install -U pip
/usr/local/bin/pip3 install -U pyobjc


As of yesterday, that would install the pyobjc frameworks v9.0.1 into:


/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages


In your Python3 program:


#!/usr/bin/env python3

from Cocoa import (NSObject, NSURL, NSURLRequest, NSString, NSWindow, NSApplication,
                   NSTerminateNow, NSMakeRect, NSTitledWindowMask,
                   NSClosableWindowMask, NSResizableWindowMask,
                   NSMiniaturizableWindowMask, NSBackingStoreBuffered)
import WebKit
import sys


class AppDelegate(NSObject):

    def init(self):
        self = super(AppDelegate, self).init()
        if self is None:
            return None
        return self

    def applicationDidFinishLaunching_(self, aNotification):
        #
        self.pageurl = NSURL.URLWithString_("https://discussions.apple.com")
        #self.apath = (NSString.stringWithString_("~/Documents/CSS_Styles/ASC.css")
        #                      .stringByExpandingTildeInPath())
        # self.mycssURL = NSURL.fileURLWithPath_(self.apath)
        self.rect = NSMakeRect(100.0, 350.0, 950.0, 825.0)
        self.mask = (NSTitledWindowMask | NSClosableWindowMask |
                     NSResizableWindowMask | NSMiniaturizableWindowMask)
        self.win = (NSWindow.alloc()
                            .initWithContentRect_styleMask_backing_defer_(self.rect,
                                                                          self.mask,
                                                                          NSBackingStoreBuffered,
                                                                          False))
        self.win.display()
        self.win.orderFrontRegardless()
        self.webview = WebKit.WebView.alloc()
        self.webview.initWithFrame_(self.rect)

        self.webview.preferences().setUserStyleSheetEnabled_(True)
        # self.cssurl = NSURL.URLWithString_("file:///Users/ckhowe/Documents/ASC.css")
        # self.webview.preferences().setUserStyleSheetLocation_(self.cssurl)
        # if CSS file cannot be found, opens window anyway without error
        # self.webview.preferences().setUserStyleSheetLocation_(self.mycssURL)

        self.req = NSURLRequest.requestWithURL_(self.pageurl)
        self.webview.mainFrame().loadRequest_(self.req)

        self.win.setContentView_(self.webview)

    def windowWillClose_(self, aNotification):
        #
        NSApplication.sharedApplication().terminate_(self)

    def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
        return True

    def applicationShouldTerminate_(self, sender):
        return NSTerminateNow


def main():
    #
    app = NSApplication.sharedApplication()
    myDelegate = AppDelegate.alloc().init()
    app.effectiveAppearance()
    app.setDelegate_(myDelegate)
    app.activateIgnoringOtherApps_(True)
    app.run()


if __name__ == '__main__':
    sys.exit(main())




Use the red traffic light in the title bar to cleanly quit this app.


Launch it from the Terminal as:


./webk2.py


Apr 25, 2023 5:17 AM in response to gtorres_2000

Unless you are using a Python-aware programmer's editor (not pico, nano, TextEdit) that respects Python's code structure and indent requirements, the code will blow up with indentation errors. Unlike that linked error, one should never place two Python statements on the same line separated by ';'. Bad form.


I use a licensed version of Sublime Text 4 which allows me to copy the above Python code and then paste and indent back into Sublime Text 4, save it, make it executable, and it just runs. Sublime Text 4 is an open-ended trial, so you can use it without a license though with periodic reminders to purchase.


Also, you need to make the code executable for starters. By default, it only has read/write permissions when you save it. If the Python script is on your Desktop, do the following after you launch the Terminal application:


cd ~/Desktop
chmod +x ./webk2.py
./webk2.py




Apr 26, 2023 5:04 AM in response to gtorres_2000

There were no errors in the Python 3 code that I posted, as afterward, I copied the posted code and pasted with indent back into Sublime Text 4, saved to a webk2.py script, marked it executable, and it ran fine on macOS 13.3.1 with the Python.org Python 3.11.3 (and the installed pyobjc 9.0.1 package).


Running webk2.py should present the web page of Apple's Discussions Community.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using pyobjc on Python for school project.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.