How can I execute an Applescript from a Python Script?

Posted on
Wed Oct 22, 2014 8:33 pm
kg23 offline
User avatar
Posts: 21
Joined: Dec 17, 2012

How can I execute an Applescript from a Python Script?

I am converting my logic from Applescript to Python. At the bottom of one of my Applescripts I execute another Applescript:
Code: Select all
execute script "Users:kg23:Documents:Indigo User Data:Scripts:set_entry_delay.scpt"

How can I trigger this from a Python script?

Thanks

Posted on
Thu Oct 23, 2014 3:58 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: How can I execute an Applescript from a Python Script?

I haven't tried it myself, but this article suggests that it might be pretty straightforward.

Using Python and AppleScript Together

Good luck!
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Oct 23, 2014 4:34 am
wysinawyg offline
Posts: 45
Joined: Oct 11, 2013

Re: How can I execute an Applescript from a Python Script?

Being a complete hack and having little general idea what I'm doing, I'd put the Applescript into an Action Group and then call the Action Group from Python.

Posted on
Thu Oct 23, 2014 12:35 pm
kg23 offline
User avatar
Posts: 21
Joined: Dec 17, 2012

Re: How can I execute an Applescript from a Python Script?

I ended up with this
Code: Select all
import os
scriptFile = "/Users/kg23/Documents/Indigo\ User\ Data/Scripts/set_entry_delay.scpt"
os.system("osascript " + scriptFile)

I guess once I convert that AppleScript to Python, I'll move it to an embedded Python script inside an action group as I understand embedded scripts run faster than file-based scripts.

Thanks!

Posted on
Thu Oct 23, 2014 1:21 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: How can I execute an Applescript from a Python Script?

Glad you found a solution.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Oct 27, 2014 10:45 pm
kg23 offline
User avatar
Posts: 21
Joined: Dec 17, 2012

Re: How can I execute an Applescript from a Python Script?

I found out that this solution only works from an external script. Trying to run from an embedded Python script causes a timeout error.
Code: Select all
  Error                           because embedded scripts execute sequentially they must complete their execution within 10 seconds.
  Error                           modify the script to exit quickly or convert it to an external file (not embedded).
  Stopping embedded script executor host (pid 6576)
  Error                           process (pid 6576) failed to quit after polite request -- forcing it to quit now


kg23 wrote:
Code: Select all
import os
scriptFile = "/Users/kg23/Documents/Indigo\ User\ Data/Scripts/set_entry_delay.scpt"
os.system("osascript " + scriptFile)


Posted on
Fri Oct 31, 2014 6:25 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: How can I execute an Applescript from a Python Script?

kg23 wrote:
I found out that this solution only works from an external script. Trying to run from an embedded Python script causes a timeout error.
Code: Select all
import os
scriptFile = "/Users/kg23/Documents/Indigo\ User\ Data/Scripts/set_entry_delay.scpt"
os.system("osascript " + scriptFile)

If your AppleScript has no output, and you do not need to wait for it to complete, you might try running the whole job in the background. This is what it would look like in the bourne shell (I have highlighted the added code):
    $ osascript /Users/kg23/Documents/Indigo\ User\ Data/Scripts/set_entry_delay.scpt >/dev/null 2>&1 &
Note the redirection of standard output (stdout) and standard error (stderr) to /dev/null is necessary. If the shell thinks there will be output, it will not run the command in the background. You will also need to force the whole job to run in the bourne shell (sh) to use this syntax. Though, similar functionality is available in bash.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 20 guests