Python实现的MAC系统下的剪贴板代码,pythonmac,This is a cl
Python实现的MAC系统下的剪贴板代码,pythonmac,This is a cl
This is a clipboard manipulation module that demonstrates some simple use ofCarbon API. It can double as a Unix-style command-line tool that prints theclipboard contents to stdout or, if specified, copies its stdin to theclipboard, although pbcopy(1) and pbpaste(1) are better suited for that.
#!/usr/bin/env python"""Manipulate Mac OS clipboard ("scrap") from Python.See also: pbcopy(1), pbpaste(1)"""from sys import stdin, stdoutfrom optparse import OptionParserfrom Carbon.Scrap import GetCurrentScrap, ClearCurrentScrapimport MacOSdef paste(flavorType='TEXT', verbose=False): try: scrap = GetCurrentScrap() return scrap.GetScrapFlavorData(flavorType) except MacOS.Error, e: if verbose or e[0] != -102: # -102 == noTypeErr raise return ""def copy(text, flavorType='TEXT'): ClearCurrentScrap() scrap = GetCurrentScrap() scrap.PutScrapFlavor(flavorType, 0, text)def list_flavors(): scrap = GetCurrentScrap() return [(name, scrap.GetScrapFlavorSize(name)) for name, flags in scrap.GetScrapFlavorInfoList()]def main(): parser = OptionParser() parser.set_defaults(flavor='TEXT', translate=True, copy=False, list_=False, verbose=False) parser.add_option("-c", "--copy", dest="copy", action="store_true", help="copy stdin to clipboard [default: paste clipboard" " to stdout]") parser.add_option("-l", "--list", dest="list_", action="store_true", help="list currently available flavors with data sizes") parser.add_option("-x", "--notrans", dest="translate", action="store_false", help="don't translate CR to LF on output") parser.add_option("-f", "--flavor", dest="flavor", action="store", help="specify flavor [default: TEXT]") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="complain if scrap flavor not found [default:" " treat as empty]") options, args = parser.parse_args() if options.list_: for flavor in list_flavors(): print "'%s' %9d" % flavor elif options.copy: copy(stdin.read(), options.flavor) else: text = paste(options.flavor, options.verbose) if options.translate: text = text.replace('\r', '\n') stdout.write(text)__all__ = ['paste', 'copy', 'list_flavors', 'main']if __name__ == "__main__": main()
By default, the most reasonable choice of the 'TEXT' flavour is used. Otherflavors for special uses can be specified. If there is no "scrap" of a givenflavor, Mac OS API report this as an error. Normally this is suppressed andtreated as empty clipboard, but user can request the program to complain.
By default, the line endings are translated from '\r' (Mac) to '\n' (Unix)when printing to stdout.
相关内容
- python方法参数默认值示例,python默认值示例,def boxVolu
- python如何删除文件,文件夹,python删除文件,python删除文
- Python将jpeg或jpg图片转换为png图片,,python的pil库可
- python将png图片转换为jpeg图片,,如下代码使用PIL库将p
- Python判断图片是否是jpeg格式(非扩展名方式),,用扩
- django上传图片并生成缩略图代码示例,,django 处理上传
- python创建临时文件代码,python创建代码,python有一个te
- python UDP 协议通信,pythonudp协议通信,import socke
- python计算GPS两点之间的中间点,python计算GPS两点,impor
- python判断unicode是否是汉字,数字,英文,或者其他字符
评论关闭