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

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

问题描述

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

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

我正在寻找一种在 Mac 应用程序(主要是 iTunes)上运行非常简单的 Python 脚本的方法,以代替 AppleScript.我已经阅读了关于使用 Python 的 AppScript,但它不再处于开发阶段,并且从 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天全站免登陆