Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Launch AppleScript from Python

Posted on 2014-04-20
3
Medium Priority
?
938 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

730 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