Using Python with a script (on Mac)

I am new to programming, but I studied basic Python programming at a university course.

I am looking for a way to run very simple Python scripts in Mac applications (mainly iTunes) instead of AppleScript. I read about using AppScript for Python, but it is no longer under development and does not work with iTunes 10.6.3.

On my Windows computer, I could easily use script applications with Python using the PyWin32 module. However, since switching to Mac, I have not 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 that 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 

As you can see, my scripting needs are pretty simple, and I don't need any advanced event handling functions like AppScript. I plan to finally learn AppleScript, but right now I still feel most comfortable with Python. Anyone have any suggestions?

Thanks a lot!

+4
source share
3 answers

You need to use Scripting Bridge with PyObjC . There are quite a few, but these links will help you get started.

+1
source

One possibility is to use osascript through the subprocess module. Then you can get a small amount of AppleScript to access, for example, the required properties of the application, but save the logic in Python. Often you can find online examples of the required AppleScript reference forms, avoiding the need to delve deeper into AppleScript.

+1
source

If you started learning AppleScript;)

 tell application "iTunes" set selected_tracks to selection set track_count to count of selected_tracks repeat with i from 1 to track_count set a_track to item i of selected_tracks set a_track track number to i set a_track track count to track_count end repeat end tell 
0
source

All Articles