1

I am trying to use a variable defined in python file in an embedded applescript. I want the path in currentpath to be appended where I mentioned the variable in the osascript statement.

I have tried all the methods given in the link and some other links too but none of them work: Pass variable into AppleScript from python

import os
import sys
currentpath = os.path.dirname(sys.argv[0])
com  = ("""osascript -e 'tell application "Terminal" to do script "afplay {0}/Sounds/storm-9s.mp3"'""".format(currentpath))
os.system(com)

All the tricks lead me to the same errors and I am not sure if the problem is with my python code or applescript.

A94HDBSGHHHD:~ user$ afplay /Sounds/storm-9s.mp3
Unspecified exception
A94HDBSGHHHD:~ user$

Update: My application requires to play multiple sound files simultaneously. Using subprocesses requires my script to pipeline the sound files and play the second file only when the first file is played completely. Thus I have to start multiple terminals to play the sounds together. e.g. My code plays a.mp3 and then before it completes playing, my code decides to play b.mp3. So it should start playing b.mp3 without stopping a.mp3 and play them both simultaneously till a.mp3 stops when the file is over and then b.mp3 stops when the file is done playing.

  • Question: what you are effectively doing here is using python to call osascript to script terminal to run a shell script. Why not just call 'afplay' directly from python? see this link: https://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code – Ted Wrigley Jul 18 '19 at 20:31
  • I have tried it. Please read the update on my issue. I cannot play multiple files simultaneously with it. – Shritama Sengupta Jul 18 '19 at 22:38
  • You can get python to detach and run processes simultaneously, but I'll add an answer that discusses both ways. – Ted Wrigley Jul 18 '19 at 23:03
  • Can you please share how? – Shritama Sengupta Jul 18 '19 at 23:07
  • see my answer below. I'm not really a python guru, so I've just linked a couple of other questions/answers from SO, but the procedure seems clear enough to me. – Ted Wrigley Jul 18 '19 at 23:32

1 Answers1

3

First, the best approach to this problem would be to avoid AppleScript entirely and run the 'afplay' command straight from python. If you read these two Stack Overflow questions:

You'll see you should be able to use popen to run these sound files simultaneously.

If you really want an AppleScript solution, then I suggest you forget about scripting Terminal and use do shell script instead. That is basically the same as running shell commands straight from AppleScript, without the overhead of Terminal.app. That would look like this:

com  = """osascript -e 'do shell script "afplay {0}/Sounds/storm-9s.mp3 &> /dev/null &"'""".format(currentpath)

or if you wanted to get adventuresome you could chain them all together like so:

com  = """osascript -e 'do shell script "afplay {0}/Sounds/storm-9s.mp3 &> /dev/null &"' -e 'do shell script "afplay {1}/Sounds/storm-9s.mp3 &> /dev/null &"'""".format(path0, path1)

Assuming that currentpath (path0/path1) gives a correct prefix, this should work; the &> /dev/null & bit tells do shell script to detach the process and return immediately. But this is, again, circuitous: you're telling python to tell a shell to tell AppleScript to tell a shell to do something, and every extra context shift creates an opportunity for something to go wrong.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • the first command gives me below error "47:48: syntax error: Expected expression but found “>”. (-2741) tab 1 of window id 5286" The second command cannot be used as which mp3 files will be played depends on user input and is decided in real-time. I am just making sure how the code plays it. – Shritama Sengupta Jul 18 '19 at 23:38
  • sorry, I misplaced a quotation mark. I've fixed it in the answer. – Ted Wrigley Jul 18 '19 at 23:50
  • I tried using popen and it worked as expected. Thank you so much! – Shritama Sengupta Jul 19 '19 at 00:15