Search code examples
pythonmacospy2app

MAC OS & Python script not able to read file when executed from Applescript or with Py2app


I have a simple app that I'm using wxpython and libvlc to play mp3 files. The filelist is a text file that has each mp3 file title and corresponding filename. Simple 'for loop' to read each line...

When I run my script in a terminal on MAC OS, it works fine. But when I try to execute the script via ApplesScript or by using Finder as a Py2app, it can not open the file?

Excerpt:

`

FILELIST='/tmp/music/playlist.txt'

def add_song_list(self):
        try:
            with open(FILELIST) as fh:
               for line in fh:
                   songlist=line.rstrip('\n').split(';')
                   theindex = "%s" % self.index
                   self.SongDict[theindex]=songlist
                   #need to append the song title to our ListBox
                   self.list_box.Append(str(songlist[0]))
                   self.index += 1
        except:
            print "Error reading playlist file %s" % FILELIST

`

I get a dialog box that comes up with my exception message.

Again this works fine on Linux. It works fine on OS X if I execute it from a terminal window. It even works with PythonLauncher (but I get a terminal window open which is what I do not what).

However, it does not work if 'opening' it from Finder or executed from an Applescript.

It does work fine if I open the .App up from a terminal using the 'open ' command.

Is there something about OS X when executing an app in 'non' terminal mode that I'm not aware of that I'm hitting?

Thanks.

-Jeremy


Solution

  • Since py2app is new to me and I normally write tools all on Linux, this was a learning experience.

    The error message was really a 'red herring'. There were multiple issues and I could only see them by opening up the console i.e. 'open -a console'. Then I could see several issues. Now what is weird, I don't have any of these issues when running in Linux. Nor did I have these issues when running the Python script in a terminal window in Mac OS.

    So here were the specific issues: Since executing an 'App' in OSX doesn't include all the environment variables that are normal in your terminal shell session, it did not have the VLC_PLUGIN_PATH defined, so my import for vlc module failed. I needed to add the following at the top of my code: os.environ['VLC_PLUGIN_PATH']='/Applications/VLC.app/Contents/MacOS/plugins'

    Next, my file that was my playlist file (just a plain text file) normally would get encoded as unicode at least on the Linux platform I was writing this on. Well, on MAC OS and only when it was packaged with py2app, it complained that the encoding was incorrect. Again, I only saw this in the console log.

    So my work around was to make sure it was encoded explicitly. self.list_box.Append(unicode(str(songlist[0]),'utf-8'))

    The final issue, I needed to remove the try except clause. I don't have any idea why it was hitting the exception. I got lazy and just did a 'with open' and removed the try/except clause - and it works fine.

    Now py2app packages it up and it runs correctly.

    -J