Solved

Launch AppleScript from Python

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

Suggested Solutions

Title # Comments Views Activity
how to split this type of line? 5 92
Ruckus Zoneflex 7982 multiple SSIDs disconnects 11 47
Thoughts on PC Doctor 11 44
MS Remote Desktop 8 for MAC 6 14
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
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 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…

815 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

8 Experts available now in Live!

Get 1:1 Help Now