升級 PyObjC
Mac OS X 10.8 所提供的 PyObjC 是 2.3.x 版本,在做一些事情的時候會遇到一些問題,但是升級到最新的 2.5.x 就比較沒有問題。升級的流程是 1. 首先要安裝 pip 2. 然後升級流程是,先升級 pyobjc-core,再升級 pyobjc,指令如下 sudo pip install pyobjc-core --upgrade sudo pip install pyobjc --upgrade 這樣便可以完成安裝。
seen from Switzerland
seen from Switzerland

seen from Switzerland
seen from United States

seen from Switzerland

seen from Malaysia

seen from Switzerland

seen from Argentina
seen from Switzerland

seen from Malaysia

seen from Malaysia

seen from United States

seen from Italy
seen from Mexico

seen from United States
seen from China
seen from China

seen from United States
seen from United States

seen from Malaysia
升級 PyObjC
Mac OS X 10.8 所提供的 PyObjC 是 2.3.x 版本,在做一些事情的時候會遇到一些問題,但是升級到最新的 2.5.x 就比較沒有問題。升級的流程是 1. 首先要安裝 pip 2. 然後升級流程是,先升級 pyobjc-core,再升級 pyobjc,指令如下 sudo pip install pyobjc-core --upgrade sudo pip install pyobjc --upgrade 這樣便可以完成安裝。

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Python, Quartz, GTK, and window focus.
One of my company's products involves a small GUI application for controlling a USB peripheral that we designed. Although the customer only requires it to target Windows at the moment, we are trying to keep it (mostly) working on Linux and OS X as well, since those may need to be supported in the future.
For easy of cross-platform-GUIness, and because I was already somewhat familiar with it, we decided to do windowing with PyGTK. Alternatively, we could have used Tkinter, PyQT, wxpython, or pyglet... but we didn't. Mostly because I already knew some GTK.
So, anyway, cross-platform GTK has been a pain. Maybe that would have been true with any of them.
For OS X, you have the option of building your GTK+ libraries with your windowing backend of choice: X11 or Quartz.
We originally went with X11 because it's the default of Macports, but discovered that StatusIcons ("system tray" or "notification area" icons), don't work in X11 (because X11 isn't running a notification area).
So, I rebuilt everything with the Macports variant trio: "-x11 +no_x11 +quartz", and eventually found myself with a more native-looking GTK. And StatusIcons! StatusIcons that don't work worth a damn!
With the new, native, pretty Quartz windows, GTK has focus issues. New windows pop up to the top, but do not have focus. This is true for the main application, and dialog boxes. And the StatusIcon menu pops up when you click on the icon, but it isn't selectable because -- get this -- it doesn't get focus when you click on it.
I spent hours trying different combinations of GTK *show*() and *activate*() and *present*() and *focus*() functions, all to no avail. I also found this open bug report on the same issue.
Eventually I stumbled upon an unrelated Objective-C post about window focus, and solved my problem with PyObjC:
import platform if platform.system() == "Darwin": from AppKit import NSApp def callback(target, button, time, *args): if platform.system() == "Darwin": NSApp.activateIgnoringOtherApps_(True) menu.popup(None,None, None, button, time) ... icon.connect("popup-menu", callback) window.show_all() if platform.system() == "Darwin": NSApp.activateIgnoringOtherApps_(True)
Windows actually get the system's focus when activateIgnoringOtherApps: is called, and the StatusIcon menu works (mostly) as expected now.
webkit2png ile ekran görüntüsü almak!
Uzunca bir zamandır yazmak istediğim fakat fırsat bulamadığım bir konu:
webkit2png
Python'la yazılmış "CommandLine Tool"u yani Terminal'den çalışan bir script. Bu script sayesinde istediğiniz bir web sayfasının ekran görüntüsünü istediğiniz boyutlarda PNG formatında almanızı sağlıyor. Bu proje aynı zamanda harika bir Python + ObjectiveC örneği. PyOBJC köprüsünü kullanarak WebKit objesine bağlanıyor. WebKit bildiğiniz gibi Safari ve Chrome web browser'ının temeli.
Script, sanki Safari web browser'ıyla verdiğiniz URL'i açıyor ve gelen sayfayı komple kaydediyor. Bu işi normal şartlarda yapmak için çeşitli browser extension'larına ihtiyacınız var. Daha önceleri Safari extension olayını desteklemiyordu. Chrome'a da extension olayı bildiğiniz gibi sonradan geldi. Mevcut tek sistem Firefox ve Screengrab extension'ıydı.
Firefox'un çılgınlar gibi yeni veriyon çıkartması Screengrab extension'ını yazan arkadaşı delirtmiş olacak ki artık destek vermiyor.
An itibariyle bu işi yapmak için Chrome ve Google tarafından yazılan "Screen Capture" iyi bir alternatif.
Eğer siz de benim gibi Shell'ciyseniz webkit2png'yi kullanabilirsiniz. Projenin orijinal yazılımı Paul Hammond'a ait. https://github.com/paulhammond/webkit2png.git
Ben, MacPorts ve Homebrew için küçük bir fix yapmıştım. İster orijinali ister benim fixlediğim versiyonu kullanabilirsiniz.
https://github.com/vigo/webkit2png
MacPorts ve Homebrew paketi olarak mevcut.
$ sudo port install webkit2png # macports'dan kurulum $ brew install webkit2png # homebrew'dan kurulum # benim ~/bin folder'ım local executable'lar için eğer sizde böyle yapmak isterseniz # mkdir ~/bin yapıp $PATH'e bu folder'ı eklemeniz gerekir. $ git clone httxs://github.com/vigo/webkit2png # kendi kurulumunuz
Kullanımı çok basit;
$ webkit2png -h
Hemen açıklaması geliyor. Default olarak 3 versiyon oluşturuyor. Fullsize, Thumbnail ve Clipped. Default ekran genişliği 800x600 pixel geliyor, aynı şekilde default clip 200x150 pixel. Ekran görüntüsünü kaydetmek istediğiniz folder'a gidin:
$ cd ~/Desktop $ webkit2png -o test1 httx://ugur.ozyilmazel.com # tüm siteyi alır 3 dosya üretir... $ webkit2png -o test2 -C --clipwidth=800 --clipheight=600 httx://ugur.ozyilmazel.com
test1 dev gibi ekran görüntüsü aldı. Toplam 3 dosya üretti:
test1-clipped.png # 200x150 test1-full.png # ben yaptığımda 960x31024 verdi test1-thumb.png # test1-full'ün thumbnail'i 240x7756
test2 ise sadece 800x600'lük alanı verdi. Son dönemde, web siteleri hep JavaScript kullandığı için, sitenin tam olarak yüklenmesi bazen saniye bazında da olsa zaman alıyor. Bunun için webkit2png'nin delay özelliğini kullanacağız...
$ webkit2png --delay=5 httx://google.com
Bu sayede google.com adresi request edildikten sonra 5 saniye bekleyecek ve 5 saniye sonunda ekran görüntüsü alınacak. Başka bir örnek;
$ webkit2png -o bilgi -F -W 1024 httx://bilgi.edu.tr # sayfa tam yüklenmemiştir. $ webkit2png -o bilgi-delay --delay=5 -F -W 1024 httx://bilgi.edu.tr # sayfa tam olarak yükledi
--delay ile sayfa tam olarak yüklendi...
Çok işe yarayan bir tool. Umarım sizin de işinize yarar... Yazı içinde httx şeklinde kullandığım kelime aslında http. Tumblr metin olarak girilen tüm url'leri <a href='e çeviriyor bu bakımdan da shell script garip görünüyor. Bunun için http yerine httx yaptım siz deneme yaparken düzeltmeyi unutmayın.
Playing Audio Files With PyObjC
Well... I have been developing a Cocoa application using PyObjC in which I have to convert some text to audio and then play it. After a little research a found that there is an object called NSSound used to load and play audio files. Well this is how it works:
import objc track = NSSound.alloc() track.initWithContentsOfFile_byReference_("audio.mp3", True)
As you can see track is an instance of NSSound, which I use to handle an audio file called "audio.mp3" ( I have also tried this with ".aiff" files and it works perfectly). Now, to play it:
Play the audio file:
track.play()
Stop the audio file:
track.stop()
Pause the audio file:
track.pause()
These are the three basic functions but there is another one used when the audio file is paused. To continue playing the audio file from where you paused it, simply do:
track.resume()
I hope this will be useful for you. If you are wondering about how I converted the text to audio, check out this snippet:
import os os.system("say -f foo.txt -o audio.mp3")
Well this code is pretty simple. I'm just using the MacOS X say command to convert the text in the file "foo.txt" to an audio file called "audio.mp3". This can take some time if the text is too long. If you want to be quicker, you can use another format try using "audio.aiff" instead of "audio.mp3".
So... that's it.
PyObjC viewDidAppear
Yesterday I started to develop an application using PyObjC and The Apple Interface Builder tool. At the beginning it's quite confusing and it has a bit of a learning curve, but after a while, it worth the time spent because you will be developing with Python.
One of the first thing I had to confront was to find a method with the behavior of viewDidAppear. Fortunately I found it, it's called awakeFromNib. So... let's suppose you have a class called LoginController, which will be the representation of the NSObject linked to your view. Well you will have to define the function awakeFromNib inside the class.
At the end, your code should look pretty similar to this:
import objc from Foundation import * class LoginController(NSObject): window = objc.IBOutlet() username_textfield = objc.IBOutlet() password_textfield = objc.IBOutlet() login_button = objc.IBOutlet() def awakeFromNib(self): NSLog("awakeFromNib do whatever you want")
I hope this helps you! I will try to post more information about PyObjC soon.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Holiday and PyObjC
Hello, all my imaginary readers :) Sorry for the months of silence. That has been caused by my work at an unnamed Finnish mobile phone maker. It has unfortunately taken almost all of my time and after the work I have not felt too enthusiastic to do any coding. This has meant that the nice Cocoa project I and a friend were working on last spring has been on hibernation, it will stay like that until the fall, at the minimum. Those were the bad news, but thankfully I've got some good news too. Tomorrow (Monday) will be the first day of my holiday. I'm going to spend most of my holiday in the Netherlands and France. I hope it will be a nice counterbalance to the (dull) life in Finland. A few weeks ago I had written a small but useful download-the-pic-from-an-opened-flickr-page script in Python. It had worked quite well, but frankly the usage had been quite unsophisticated. The script was 100% console based. Yesterday I decided it was time to make a GUI for it, just a simple one. The work was actually done in 3-4 LOC using EasyDialogs module. I was quite impressed with myself but in about 2 minutes I had to admit that the UI sucked, even more than the console UI. I felt almost betrayed, but thought that I could invest a bit more time. There were some Carbon based options for Python, but I wanted a proper GUI and use Cocoa via PyObjC. This showed to be the correct choice. After a few missteps, like trying to compile PyObjC myself, I got adapted to the PyObjC way of doing things and it was working quite smoothly. No compilation means faster edit-test process. In a way using Cocoa API via Python was even better than the real thing, for example there was no explicit alloccing and initting strings needed, I could just introduce a Python string and and use it transparently in place of an NSString. But there is a caveat: the ObjC style method names of Cocoa are mangled into Pythonesque names with underscores. For instance sending a message [anObject message:a using:b and:c] becomes anObject.message_using_and_(a, b, c) Also delegate names are mangled in the same manner: - (void)applicationWillTerminate:(NSNotification *)notification {} becomes def applicationWillTerminate_(self, notification): pass I found that to be quite ugly, but it works and I guess I could get used to that with time. In conclusion, PyObjC was a pleasant experience with a some drawbacks. I'm probably not going to use it to make "real" applications, but it seems to be a perfect fit to adding a proper GUI for an existing Python script.