Solved

Launch AppleScript from Python

Posted on 2014-04-20
3
845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android development question 2 110
Python -- Read from csv for sending mail 9 85
Send Email from iOS Swift 3 2 38
Does iMessage let me access my iPhone's Text Messages? 3 55
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…

751 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