在可编写脚本的应用程序中使用Python(在Mac上) [英] Using Python with Scriptable Applications (on a Mac)

查看:167
本文介绍了在可编写脚本的应用程序中使用Python(在Mac上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很陌生,但是我在大学课程中学习了一些基本的Python编程.

I'm quite new to programming, but I've learned some basic Python programming in a university course.

我正在寻找一种在Mac应用程序(主要是iTunes)上代替AppleScript来运行非常简单的Python脚本的方法.我已经阅读过将AppScript用于Python的知识,但是它不再处于开发中,并且从iTunes 10.6.3开始已被破坏.

I'm looking for a way to run very simple Python scripts on Mac applications (mainly iTunes), in lieu of AppleScript. I've read up on using AppScript for Python, but it is no longer in development and is broken as of iTunes 10.6.3.

在Windows计算机上,我可以使用名为PyWin32的模块轻松地使用Python编写应用程序脚本.但是,自从改用Mac之后,我一直找不到一个好的选择.这是我在Windows上使用的示例脚本,目的只是为了让我了解我想使用的脚本的简单性:

On my Windows computer, I was easily able to script applications with Python using a module called PyWin32. Since switching to a Mac, however, I haven't been able to find a good alternative. Here is an example script that I used on Windows, just to give an idea of the simplicity of the scripts I would like to use:

from win32com.client import Dispatch
iTunes = Dispatch("iTunes.Application")

selected_tracks = iTunes.SelectedTracks
track_count = selected_tracks.Count

for i in range(1, track_count + 1):
    selected_tracks.Item(i).TrackNumber = i
    selected_tracks.Item(i).TrackCount = track_count

如您所见,我的脚本需求是非常基本的,并且我不需要诸如AppScript之类的任何高级事件处理功能.我计划最终学习AppleScript,但是现在我仍然对Python感到最满意.有人有什么建议吗?

As you can see, my scripting needs are quite basic, and I don't need any of the advanced event-handling features of something like AppScript. I plan on eventually learning AppleScript, but right now I still feel most comfortable with Python. Does anyone have any suggestions?

非常感谢!

推荐答案

您需要使用 PyObjC .有很多东西,但是这些链接会让您入门.

You need to use Scripting Bridge with PyObjC. There's rather a lot to it, but those links will get you started.

这篇关于在可编写脚本的应用程序中使用Python(在Mac上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆