[Python] How does one catch the parameters on startup when calling an application using a...

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I need my application to be called using a custom URL protocol (like MyApp://1234) and catch the arguments (/1234) in order to use them as the variable for a port when starting a tornado web server.

    I already have the info.plist setup in order to have my custom url protocol and it does start but I can’t seem to get a way to access the url the user inputs.

    I tried using apple events:

    class AppDelegate(NSObject):

    def applicationWillFinishLaunching_(self, notification):

    try:
    eventclass1 = struct.unpack('>I', b'GURL')
    eventid1 = struct.unpack('>I', b'GURL')

    eventclass2 = struct.unpack('>I', b'WWW!')
    eventid2 = struct.unpack('>I', b'OURL')

    man = NSAppleEventManager.sharedAppleEventManager()
    man.setEventHandler_andSelector_forEventClass_andEventID_(
    self,
    "openURL:withReplyEvent:",
    eventclass1[0], # Clase de evento (GetURL)
    eventid1[0] # ID del eventotid[0])
    )

    man.setEventHandler_andSelector_forEventClass_andEventID_(
    self,
    "openURL:withReplyEvent:",
    eventclass2[0], # Clase de evento (GetURL)
    eventid2[0] # ID del eventotid[0])
    )


    print('okk')
    except Exception as e:
    print(e)

    @signature(b'v@:mad:@')
    def openURL_withReplyEvent_(self, event, replyEvent):
    print("1")
    keyDirectObject = struct.unpack(">i", "----")[0]
    url = event.paramDescriptorForKeyword_(keyDirectObject).stringValue().decode('utf8')

    urlPattern = re.compile(r"^(.*?)://(.*)$")
    match = urlPattern.match(url)

    schema = match.group(1)
    hier_part = match.group(2)

    strjson = base64.b64decode(hier_part)
    params = json.loads(strjson)

    #t1.start()
    procesa_accion(params)
    #t1.cancel()`


    This doesn’t work for some reason. I’ve tried many of the Apple event ids and event classes but I can’t seem to find the correct combination and I have no idea if it’s because I’m using the wrong method, the wrong combination of classes and ids, the wrong process. Or if it’s all that’s just wrong.

    The whole code needs to be in a single Python file so I can’t just call a swift file to read the url either. And I also can’t just ask for the Port after startup since it has to be compatible with a third party device I can’t change that is expecting it to work that way

    I tried using apple events and several keys, I tried using Selenium and Urllib3 to access the URL (both require you to open a browser window first and I can’t have the program do that) and I tried the aforementioned process of asking later for the port.

    The system I’m developing this for is Sonoma 14.6 and I’m using Python 3.12

    Continue reading...

Compartilhe esta Página