Solved

Launch AppleScript from Python

Posted on 2014-04-20
3
817 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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.
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

685 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