Using Python and AppleScript Togetherby Noah Gift
Applescript and Python are quite different, but can be used together to make very efficient tools without compromising the modularity of Python. This is a quick introduction to the major concepts with some real examples. I will also briefly cover using bash aliases with your code to make it even more effective.
Before we can even start I need to introduce you to being lazy with bash aliases and osascript, via the Script Editor application. The Script Editor is an IDE for AppleScript that works pretty well. But first, be lazy! Alias Script Editor to your .profile or .bashrc., here is an example of mine:
alias ose='open /Applications/AppleScript/Script\ Editor.app/'
(Note to add this to your .profile using vim:
Now from the command line, in a new shell type in: (Command + N for you keyboard shortcut guys like me)
Pretty cool right? Next, let's get even lazier. Let's write an embedded AppleScript to close Script Editor from bash and put it in your bash profile.
First, export two variables in
export OSA='osascript -e ' export SE='"Script Editor"'
Next, alias a close command:
alias cse='$OSA "tell app $SE to quit"'
Try it out, open a new shell and open the Script Editor again:
Now be lazy and close that shell from the terminal:
Pretty nifty; but you might be wondering, "isn't this about Python and AppleScript?" We're getting there. Let's use these shortcuts to actually write a simple AppleScript with embedded Python.
Python and AppleScript
Here is our first example of calling Python from AppleScript:
Example 1: Call Python Command from AppleScript
do shell script "python -c 'print \"Hello World\"'"
(Note: \ is used for double quote escape)
Click run and you will see the expected ouput at the bottom of the window. Not a very exciting example, I hate hello world, but tradition, tradition...
It does bring up one of the limitations of this method. Python was not designed to be run from the command line like Perl. The only way to "trick" Python into executing big, complicated one liners is to import a module that will execute code upon import.
This is generally a bad idea, but I can show you one fun example:
Example 2: Using Python import Command from AppleScript
do shell script "python -c 'import this'"
(This is the famous "Zen of Python" by Tim Peters. I will supress the output for the sake of the article, but you will see the "Zen of Python")
So remember, this is one option to execute complex commands via the command line with Python, but it's generally a bad idea as there are better ways to use Python to solve your problems.
You might be thinking that if you can all Python from AppleScript, you can probably call AppleScript from Python. You would be correct in thinking this is possible.
First make a sandbox if you don't have one:
mkdir ~/sandbox cd ~/sandbox
Example 3: Call AppleScript from Python with osascript
(You can cut and paste this into your favorite text editor. I like GVim quite a bit.)
#!/usr/bin/env python #sleepy-mac.py #makes my mac very sleepy import os cmd = """osascript -e 'tell app "Finder" to sleep'""" def stupidtrick(): os.system(cmd) stupidtrick()
Now let's make it executable:
chmod +x ~/sandbox/sleepy-mac.py
And we can call this from the command line:
This should have just put your Mac to sleep. Pretty cool, and it can actually be very useful. Let's alias this to your bash profile so whenever you feel like telling your Mac to sleep, you just type in a command. In
Now, whenever you feel the urge to put your Mac to sleep you can just type in sleepymac from the command line.
Pages: 1, 2