So I have Foo.app, which is configured to run Foo.app/Contents/MacOS/foo.py when it is launched. In the app’s Info.plist file I also have it set as the application to handle the launching of “.bar” files. When I double-click a .bar file, Foo.app opens as expected.
The problem is that when a file is opened in this way, I can’t figure out how to get the path to the opened file in my foo.py script. I assumed it would be in sys.argv[1], but it’s not. Instead, sys.argv[1] contains a strange string like “-psn_0_2895682”.
Does anyone know how I can get the absolute path to the opened file? I’ve also checked os.environ, but it’s not there either.
You can get your app’s process ID then ask the OS for all open files using
lsof, looking for your process’s ID:The regular files will be of type
'REG'in the fifth column,[4]if you’re indexing in.Files open within the running code can be displayed in a similar way:
from string import *
from os import getpid
from subprocess import check_output, STDOUT
import re
Which results in: