views:

120

answers:

3

I am trying to embed an AppleScript in a Python script. I don't want to have to save the AppleScript as a file and then load it in my Python script. Is there a way to enter the AppleScript as a string in Python and have Python execute the AppleScript? Thanks a bunch.

Here is my script: import subprocess import re import os

def get_window_title():
    cmd = """osascript<<END
    tell application "System Events"
        set frontApp to name of first application process whose frontmost is true
    end tell
    tell application frontApp
        if the (count of windows) is not 0 then
            set window_name to name of front window
        end if
    end tell
    return window_name
    END"""

    p = subprocess.Popen(cmd, shell=True)
    p.terminate()
    return p

def get_class_name(input_str):
    re_expression = re.compile(r"(\w+)\.java")
    full_match = re_expression.search(input_str)
    class_name = full_match.group(1)
    return class_name

print get_window_title()
+1  A: 

Example 3 in this article suggests:

#!/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()

These days, however, subsystem.Popen is usually preferred over os.system (the article is from three years ago, when nobody screamed on seeing an os.system call;-).

Alex Martelli
I looked at that link before, and I couldn't quite get my script working. I edited my question to include the script I am trying to perform. I think the error is that I can't retrieve the return value from the AppleScript. I looked into the Popen object, but I couldn't find anything to operate on return values.
dbmikus
A: 

Rather than embedding AppleScript, I would instead use appscript. I've never used the Python version, but it was very nice in Ruby. And make sure that, if you're installing it on Snow Leopard, you have the latest version of XCode. However, I've so far been unable to install it on Snow Leopard. But I've only had Snow Leopard for ~1 day, so your mileage may vary.

Antal S-Z
Sorry to hear you're having problems; it should work (it does here). Have you tried posting on rb-appscript-discuss (http://rubyforge.org/mail/?group_id=2346) for help?
has
has: Thanks for the link, but I only installed Snow Leopard 1.5 days ago, and I spent today traveling; I haven't really tried very hard yet. Still, I'll check it out if I can't make it work; thanks again.
Antal S-Z
has: And indeed, all I needed to do was [update XCode](http://soenkerohde.com/2010/03/running-project-sprouts/). Thanks again, though!
Antal S-Z
Glad you got it sorted. And yes, if the OP doesn't mind a third-party install, then py-appscript is a much nicer solution. e.g. This'll get the names of all windows in the frontmost process: `app('System Events').application_processes[its.frontmost==True].first.windows.name()`
has
+2  A: 

Use subprocess:

from subprocess import Popen, PIPE

scpt = '''
    on run {x, y}
        return x + y
    end run'''
args = ['2', '2']

p = Popen(['osascript', '-'] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(scpt)
print (p.returncode, stdout, stderr)
has