Author Topic: Trying to make an alert with python and objc  (Read 1536 times)

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Trying to make an alert with python and objc
« on: August 15, 2012, 09:04:24 am »
Ok im at work right now but when I get home I should be able to upload this objc python wrapper ive been working on.

I dont know objc,  but imagine that all the classes are wrapped using libobjc.dylib.  Does this look right for an alert?

import objc


NSAutoreleasePool = objc.objc_getClass('NSAutoreleasePool')
pool = objc.objc_msgSend(NSAutoreleasePool, objc.sel_registerName('alloc'))
pool = objc.objc_msgSend(pool, objc.sel_registerName('init'))

UIAlertView = objc.objc_getClass('UIAlertView')
Alert = objc.objc_msgSend(UIAlertView, objc.sel_registerName('initWithTitle:'))



objc.objc_msgSend(pool, objc.sel_registerName('show'))
objc.objc_msgSend(pool, objc.sel_registerName('release'))


I know im missing a few things in "Alert" but i figured it would atleast pop up a blank alert box.  Anyone with python and objc knowledge have an idea why it wont work?  I dont get any errors it just doesnt show an alert.

8BitAce

  • Hero Member
  • *****
  • Posts: 703
  • Reputation: 57
  • If at first you don't succeed; call it version 1.0
  • Computers: Toshiba L505D-GS6000
  • iDevices: iPad 2 16GB WiFi, iPod 2g, iPod 4g
Re: Trying to make an alert with python and objc
« Reply #1 on: August 15, 2012, 10:40:07 pm »
I don't know any python, but looking at your second to last line, you're sending -show to an NSAutoreleasePool. I think you meant Alert. Also, you seem to be releasing the pool too soon, but maybe I'm wrong.

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: Trying to make an alert with python and objc
« Reply #2 on: August 15, 2012, 10:42:11 pm »
Yeah just try changing the [pool show] to [Alert show] (why do you use uppercase lol?)

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #3 on: August 16, 2012, 07:59:56 am »
Yea I tried that and it didn't work,  I think im going to have to add some fields to Alert.  I dont know why i used uppercase lol.  Maybe it looked more like objc at the time.  Sorry I never uploaded it,  im going to try and get it on a google code project today though then maybe someone could try it.   

H4CK3R

  • Haxor
  • Sr. Member
  • *****
  • Posts: 407
  • Reputation: 42
  • I try and write cool tweaks!
    • Kyle Howells
  • Badges:
  • Computers: MacBook Pro
  • iDevices: iPod 1G, iPod 2G, iPod 4G, iPhone 4S, iPad 2, iPad mini
Re: Trying to make an alert with python and objc
« Reply #4 on: August 16, 2012, 12:35:13 pm »
I'm not sure why it doesn't work, but I'll change what I can see is wrong.



import objc


NSAutoreleasePool = objc.objc_getClass('NSAutoreleasePool')
pool = objc.objc_msgSend(NSAutoreleasePool, objc.sel_registerName('alloc'))
pool = objc.objc_msgSend(pool, objc.sel_registerName('init'))

UIAlertView = objc.objc_getClass('UIAlertView')
Alert = objc.objc_msgSend(UIAlertView, objc.sel_registerName('initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:'), "Announcement", "It turns out that you are playing Addicus!", nil, "OK", nil)
objc.objc_msgSend(Alert, objc.sel_registerName('show'))

objc.objc_msgSend(pool, objc.sel_registerName('release'))



I'm using this as my reference, http://stackoverflow.com/questions/10289890/how-to-write-ios-app-purely-in-c/10290255#10290255
It uses sel_getUid("") where you use sel_registerName(""). But I'm taking it for this that both work.

Don't like seeing ads? Click here to register!

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #5 on: August 16, 2012, 03:19:55 pm »
Wow thanks for that, i tried it and it ran without errors but no alert.  Then I changed sel_registerName to sel_getUid and still nothing.  Must have something to do with dyld_cache.   I can still use it for CLI but I dont think we will ever be able to interact with iOS UI with python again.  Back to the drawing board.  Ill still upload objc though.

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: Trying to make an alert with python and objc
« Reply #6 on: August 16, 2012, 06:42:47 pm »
How are you running your script? Just from the console or as an app?

H4CK3R

  • Haxor
  • Sr. Member
  • *****
  • Posts: 407
  • Reputation: 42
  • I try and write cool tweaks!
    • Kyle Howells
  • Badges:
  • Computers: MacBook Pro
  • iDevices: iPod 1G, iPod 2G, iPod 4G, iPhone 4S, iPad 2, iPad mini
Re: Trying to make an alert with python and objc
« Reply #7 on: August 16, 2012, 06:58:47 pm »
How are you running your script? Just from the console or as an app?
Yeah, that is my next idea. Is the UIKit framework even loaded when you run this?

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #8 on: August 17, 2012, 09:03:36 am »
Im just running as a script,  although in stackoverflow they loaded the UIKit framework but never used it.  This is the code I found that gave me the idea

import ctypes
import ctypes.util

# Need to do this to load the NSSpeechSynthesizer class, which is in AppKit.framework
appkit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('AppKit'))
objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))

objc.objc_getClass.restype = ctypes.c_void_p
objc.sel_registerName.restype = ctypes.c_void_p
objc.objc_msgSend.restype = ctypes.c_void_p
objc.objc_msgSend.argtypes = [ctypes.c_void_p, ctypes.c_void_p]

# Without this, it will still work, but it'll leak memory
NSAutoreleasePool = objc.objc_getClass('NSAutoreleasePool')
pool = objc.objc_msgSend(NSAutoreleasePool, objc.sel_registerName('alloc'))
pool = objc.objc_msgSend(pool, objc.sel_registerName('init'))

NSSpeechSynthesizer = objc.objc_getClass('NSSpeechSynthesizer')
availableVoices = objc.objc_msgSend(NSSpeechSynthesizer, objc.sel_registerName('availableVoices'))

count = objc.objc_msgSend(availableVoices, objc.sel_registerName('count'))
voiceNames = [
  ctypes.string_at(
    objc.objc_msgSend(
      objc.objc_msgSend(availableVoices, objc.sel_registerName('objectAtIndex:'), i),
      objc.sel_registerName('UTF8String')))
  for i in range(count)]
print voiceNames

objc.objc_msgSend(pool, objc.sel_registerName('release'))


So Ill try that and see what happens.

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #9 on: August 17, 2012, 09:23:53 am »
Yea it looks like adding UIKit something:


« Last Edit: August 17, 2012, 09:28:38 am by Trcx528 »

Don't like seeing ads? Click here to register!

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #10 on: August 17, 2012, 09:34:24 am »
Yea it looks like adding UIKit something:


2012-08-17 10:28:41.946 python[23710:707] +[U
IAlertView initWithTitle:]: unrecognized sele
ctor sent to class 0x3eb8a438
2012-08-17 10:28:41.963 python[23710:707] ***
 Terminating app due to uncaught exception 'N
SInvalidArgumentException', reason: '+[UIAler
tView initWithTitle:]: unrecognized selector
sent to class 0x3eb8a438'
*** First throw call stack:
(0x325df88f 0x34635259 0x325e292f 0x325e1915
0x3253c650 0x381834 0x1f00a4 0x1e7f8c 0x1052c
 0xd9d24 0xddcfc 0x3b698 0x1052c 0x19f78 0x10
52c 0x82b9c 0x1052c 0xda4b0 0xddcfc 0xde4cc 0
x1092dc 0x109580 0x115fa0 0x1f80 0x1ecc 0x1d6
8)
terminate called throwing an exceptionAbort t
rap: 6


maybe i did initWithTitle wrong

H4CK3R

  • Haxor
  • Sr. Member
  • *****
  • Posts: 407
  • Reputation: 42
  • I try and write cool tweaks!
    • Kyle Howells
  • Badges:
  • Computers: MacBook Pro
  • iDevices: iPod 1G, iPod 2G, iPod 4G, iPhone 4S, iPad 2, iPad mini
Re: Trying to make an alert with python and objc
« Reply #11 on: August 17, 2012, 11:19:42 am »
Did you try my code?
It has no -initWithTitle method name it's "initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:"

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: Trying to make an alert with python and objc
« Reply #12 on: August 18, 2012, 12:41:00 am »
Boo: try running it as an app.

Make a new folder in /Applications/ called py.app

In py.app, copy/paste the Info.plist from another app. Change the bundle identifier to com.boococky.pyapp and the cfbundleexecutable to "app.sh"

Make a file "app.sh" with this:
Code: [Select]
#!/bin/bash
exec "$(dirname "$0")"/python app.py

And make a symlink called "python" pointing to /usr/bin/python. chmod 0755 app.sh

Then, put your python code in app.py. First just try a open("/tmp/test.txt", "w").write("It works!") to verify your code gets executed. Then add your objc stuff in.


This is all from memory, so if it doesnt work, i blame it on sleepiness lol :P

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #13 on: August 18, 2012, 05:23:32 am »
Did you try my code?
It has no -initWithTitle method name it's "initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:"


Yea,  No dice :(

2012-08-18 06:17:11.521 python[24779:707] ***
 Terminating app due to uncaught exception 'N
SInvalidArgumentException', reason: '+[UIAler
tView initWithTitle: messege: delegate: cance
lButtonTitle:]: unrecognized selector sent to
 class 0x3eb8a438'


Isnt initWithTitle an instance method and not a class method?


Boo: try running it as an app.

Make a new folder in /Applications/ called py.app

In py.app, copy/paste the Info.plist from another app. Change the bundle identifier to com.boococky.pyapp and the cfbundleexecutable to "app.sh"

Make a file "app.sh" with this:
Code: [Select]
#!/bin/bash
exec "$(dirname "$0")"/python app.py

And make a symlink called "python" pointing to /usr/bin/python. chmod 0755 app.sh

Then, put your python code in app.py. First just try a open("/tmp/test.txt", "w").write("It works!") to verify your code gets executed. Then add your objc stuff in.


This is all from memory, so if it doesnt work, i blame it on sleepiness lol :P


Ok good idea,  I downloaded the HelloPython.app from sauriks index of debs and i can probably use that after modifying.  I didnt even think about putting a test script in the app to see if it actually executed the code though :P Im gonna do it right now.

BooCocky

  • Leader
  • Hero Member
  • *****
  • Posts: 875
  • Reputation: 81
  • All your base are belong to ininjas
  • Badges:
  • Computers: Dell Inspirion
  • iDevices: ipod shuffle
Re: Trying to make an alert with python and objc
« Reply #14 on: August 18, 2012, 08:22:53 am »
The test ran fine,  "it works!" lol. The app crashes if I try the alert.  So I changed initWithTitle to alloc just to see what happens and now I get a different error:

2012-08-18 09:20:12.110 python[25291:707] Req
uesting the window of a view (<UIAlertView: 0
x269830; frame = (0 0; 0 0); transform = [0,
0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer
 = (null)>) with a nil layer. This view proba
bly hasn't received initWithFrame: or initWit
hCoder:.


So,  I think its just a syntax error.