[Pythonmac-SIG] suggestions for an appscript FAQ

Henning Hraban Ramm hraban at fiee.net
Fri Mar 14 21:06:57 CET 2008


Am 2008-03-13 um 01:47 schrieb has:

> As part of the current upgrade of the appscript website, I'm putting
> together a list of topics for an appscript FAQ, and wondering if
> anyone has any requests or suggestions for topics it should cover?
> Both Python/Ruby/ObjC-specific and general topic ideas are welcome.

Hi Hengist, sorry for the delay, I was busy. My reply to your  
personal mail was rejected as spam, twice.

Feel free to use my snippets as examples or for the FAQ.

I struggled a lot with paths containing non-ASCII characters.
Here's my solution:

---

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
How to handle unicode paths
"""
import sys
import unicodedata
import appscript
import mactypes

myfile = sys.argv[1]
# e.g. myfile = u'/Users/user/Documents/déjà vu/rückwärts.txt'
# OSX paths are encoded in *decomposed* UTF-8
# we need normalized, composed Unicode:
myfile = unicodedata.normalize('NFD', myfile)
# then we can make a working Alias
myfile = mactypes.Alias(myfile)

finder = appscript.app('Finder')
finder.items[myfile].get() # any Finder actions

---

Perhaps you could do the normalization in mactypes?

---

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Getting and setting Finder labels
"""
import unicodedata
import appscript
import mactypes

# can we generalize that?
label_colors = [u'', u'orange', u'red', u'yellow', u'blue', u'rose',  
u'green', u'gray']

def ComposedAlias(path):
     return mactypes.Alias(unicodedata.normalize('NFD', path))

def get_label(path):
     "returns the name of the Finder label of a file/folder"
     finder = appscript.app('Finder')
     datei = finder.items[ComposedAlias(path)]
     idx = datei.label_index()
     # is sometimes 16393 for some tries, then suddenly right - why?
     return label_colors[idx]

def set_label(path, label):
     "set the Finder label of a file/folder as name or number"
     finder = app('Finder')
     macItem = finder.items[ComposedAlias(path)]
     if label in label_colors:
         label = label_colors.index(label)
     else:
         label = int(label)
     return macItem.label_index.set(label)




Greetlings from Lake Constance!
Hraban
---
http://www.fiee.net
https://www.cacert.org (I'm an assurer)




More information about the Pythonmac-SIG mailing list