Solved

Launch AppleScript from Python

Posted on 2014-04-20
3
730 Views
Last Modified: 2014-05-12
I have a working Applescript that automates some tasks in photoshop. I am trying to wrap a Python script around it for batch processing of files.

What I want is this

<psudocode>
for each file in dir
     applescript.scpt filename
     (wait until applescript finishes)
     Process result files

Do some other things
Done.
</pseudocode>

How do I launch Applescript from within Python (without HEREDOC or other similar things where I define the whole script in Python) so I can pass it a variable and have it work with a given folder?

PS: This post and similar like it are NOT the answer to my question. They are using heredocs and concatenated strings. That's not what I want. I have a VERY long script that I just want to pass a variable to so I can process files in a batch.
0
Comment
Question by:DrDamnit
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Pasha Kravtsov earned 500 total points
ID: 40011987
I like to use subprocess.

from subprocess import check_output

def get_result(cmd, args):
  out_put = check_output("%s %s" % (cmd, args), shell=True)
  return out_put

dir = raw_input("Directory: ")
files = get_result("ls", dir)
for file in files.split(' '):
  for f in file.split('\n'):
    results = get_result("applescript.scpt", f)
    print results

Open in new window

hope this helps. This is for windows so I'm not sure if this exactly would work for OS X as I don't have a machine to code this on... tell me if anything doesn't work etc. Hopefully you get the idea :)
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 40059462
I ultimately used:
on run {somevar}
    tell application "app"
    --Do things---
   end tell
end run

This mean that I could use subprocess.Popen to pass a variable the script and it will work.
0
 
LVL 32

Author Closing Comment

by:DrDamnit
ID: 40059463
See my comments about the recieving applescript.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now