How to get which file is requested to open using a mac application?

2

I have created a mac application which can be open my file extensions.

But when I tested it, I dont get the path of the file requested to open using the application, instead I got the "psn_0_151589".

I checked it for itunes, textedit, xcode and other applications.

Below is my python app sample main code where i process path of the opened file

import sys
import os.path
print("File opened with this app :: ",sys.argv[1])
if(os.path.exists(sys.argv[1]):
  print("valid file :: { do something...}\n")
else:
  print("Invalid file path received :: { do nothing }\n") 

OUTPUT :

File opened with this app :: psn_0_151589
Invalid file path received :: { do nothing }

Hope someone knows how to get the filepath which was opened using any application.

Any help would be greatly appreciated.

ramsey

Posted 2012-09-27T14:02:32.337

Reputation: 21

If you posted your code it would be easier to understand your question. – adayzdone – 2012-09-27T14:28:15.310

updated above with the sample code and its output – ramsey – 2012-09-27T15:28:19.767

Answers

2

Mac OS X application bundles don't receive the files to be opened as command line arguments. As every application usually only has a single instance, this'd restrict opening further files. Instead, it's a message sent when the application is already running. The argument you receive is the process serial number. More information here.


You could alternatively wrap your Python script in an Automator application's Run Shell Script action (#!/usr/bin/python being the "shell"). When you select to pass input as arguments, they'll be made available in argv, see the sample code:

Screenshot

Daniel Beck

Posted 2012-09-27T14:02:32.337

Reputation: 98 421