[英]Open specific file type with Python script?
How can I make a Python script to be a specific file type's (eg, *.foo) default application? 如何使Python脚本成为特定文件类型(例如* .foo)的默认应用程序? As in, when I double click the file in the Finder / Explorer I want the file to open in the Python script.
与之类似,当我在Finder / Explorer中双击文件时,我希望在Python脚本中打开该文件。
Is this possible to do in Win and/or OS X? 在Win和/或OS X中可以这样做吗? The application is a PySide app if that matters.
如果重要的话,该应用程序是PySide应用程序。
On Mac OS X you can use Automator to create an application that calls your python app and passes the input file path as a string argument. 在Mac OS X上,您可以使用Automator创建一个应用程序,该应用程序调用python应用程序并将输入文件路径作为字符串参数传递。 In the application workflow wizard, add action "Run Shell Script", select
Pass input:
as as arguments
, and in the text box add: 在应用程序工作流向导中,添加操作“ Run Shell Script”,选择“将
Pass input:
as arguments
,然后在文本框中添加:
python /path/to/my/app/myapp.py "$@"
The "$@"
passes along whatever arguments were in the input (aka the selected file) as strings. "$@"
将输入(也就是所选文件)中的任何参数作为字符串传递。 As long as your script is set up to deal with the input ( sys.argv
) as a list of strings (the first one being the python app path), then it will work. 只要将脚本设置为以字符串列表的形式处理输入(
sys.argv
)(第一个字符串是python应用程序路径),那么它将起作用。
When you save that Automator workflow, it is treated by OS X like any other app, and you can set that app as the default for files of type "*.foo". 保存该Automator工作流程时,OS X会像对待其他任何应用程序一样对待它,并且可以将该应用程序设置为“ * .foo”类型文件的默认应用程序。 To associate "*.foo" with that app, right click a .foo file,
Get Info
, Open with: Other...
, choose the app you created in Automator, then click the Change All...
button. 要将“ * .foo”与该应用程序相关联,请右键单击.foo文件,
Get Info
, Open with: Other...
,选择您在Automator中创建的应用程序,然后单击Change All...
按钮。
A similar but hopefully less-involved approach might work in Windows. Windows中可能会使用类似但希望涉及较少的方法。 You could probably create a batch file (
.bat
) with the following: 您可能会创建具有以下内容的批处理文件(
.bat
):
python C:\path\to\my\app\myapp.py %*
The %*
expands to all arguments. %*
扩展为所有参数。
As long as you can associate a file extension with that batch file, then you could do that, and that's your solution. 只要您可以将文件扩展名与该批处理文件相关联,就可以做到这一点,这就是您的解决方案。 However, I haven't tried this Windows solution, so take it with a grain of salt.
但是,我还没有尝试过Windows解决方案,因此请带一点盐。 The Mac solution, on the other hand, I have tested.
另一方面,我测试了Mac解决方案。
By example, here's a universal solution I wrote for: 1) opening a Windows desktop link (*.URL) that's been copied to a Linux box. 例如,这是我为之编写的通用解决方案:1)打开一个Windows桌面链接(* .URL),该链接已复制到Linux盒中。 Or 2) opening a Linux .Desktop link that's been copied to a Windows box.
或2)打开已复制到Windows框中的Linux .Desktop链接。
Here's the Python script that handles both cases: 这是处理两种情况的Python脚本:
# UseDesktopLink.py
import sys
import webbrowser
script, filename = sys.argv
file_object = open(filename,'r')
for line in file_object:
if line[0:4]=="URL=":
url=line[4:]
webbrowser.open_new(url)
file_object.close()
On Windows, use Scott H's method (via a bat file) to handle the association. 在Windows上,使用Scott H的方法(通过bat文件)来处理关联。
On Linux, right-click a Windows URL file. 在Linux上,右键单击Windows URL文件。 Choose Properties, and Open With.
选择“属性”,然后选择“打开方式”。 Click Add to add a new application.
单击添加以添加新的应用程序。 Then at the bottom of the "Add Application" window, click "Use a custom command".
然后,在“添加应用程序”窗口的底部,单击“使用自定义命令”。 Then browse to the UseDesktopLink.py file and click Open.
然后浏览到UseDesktopLink.py文件,然后单击“打开”。 But before you click Add, in the textbox below "Use a custom command", put "python " before the filename (without the quotes).
但是,在单击“添加”之前,在“使用自定义命令”下面的文本框中,在文件名前加上“ python”(不带引号)。 Then click Add and Close.
然后单击添加并关闭。
Hope that helps. 希望能有所帮助。
I found this old question while looking for an answer myself, and I thought I would share my solution. 我自己寻找答案时发现了这个老问题,我想我会分享我的解决方案。 I used a simple c program to direct the arguments to a python script, allowing the python script to stay a script instead of needing to compile it to make things work.
我使用了一个简单的c程序将参数定向到python脚本,从而使python脚本可以保留脚本,而不需要对其进行编译以使工作正常。 Here is my c program:
这是我的C程序:
int main(int argc, char *argv[]){
char cmd[0xFF];
// For me, argv[1] is the location of the file that is being opened. I'm not sure if this is different on other OSes
snprintf(cmd,sizeof cmd,"python YOUR_PYTHON_SCRIPT_HERE.py -a %s", argv[1]);
system(cmd);
return 0;
}
I then compiled the c program and set that as the default application for the file extension. 然后,我编译了c程序并将其设置为文件扩展名的默认应用程序。
Then, in the python script YOUR_PYTHON_SCRIPT_HERE.py, I receive the argument like this: 然后,在python脚本YOUR_PYTHON_SCRIPT_HERE.py中,我收到这样的参数:
import sys
assert len(sys.argv) > 2 # Breaks if you call the script without the arguments
theFile = " ".join(sys.argv[2:]) # What the c program gives us
print(theFile) # Print it out to prove that it works
theFile will contain the location of the file that is being opened theFile将包含正在打开的文件的位置
Get the contents of the file by using: 通过使用以下命令获取文件的内容:
with open(theFile,"r") as f:
fileContents = f.read()
foo
foo
文件 Get Info
or Click on the file icon,then click Get info or click on the file and hit Command + I Get Info
或单击文件图标,然后单击获取信息或单击文件,然后按Command + I Open With
pane that shows up, select the path to the python binary Open With
窗格中,选择python二进制文件的路径 change All
button change All
按钮 continue
continue
On Windows: 在Windows上:
.docx
file for this example) .docx
文件) Open with...
Open with...
python
python
Always use the selected program to open this kind of file
. Always use the selected program to open this kind of file
。 Note: this will run the contents of the .docx
file in context of the python shell. 注意:这将在python shell的上下文中运行
.docx
文件的内容。 It will immediately close once it is finished evaluating the contents of the file. 一旦完成文件内容的评估,它将立即关闭。 If you'd like to edit the file in a word processor, perhaps you should download notepad++ , and select that application as the default.
如果您想在文字处理器中编辑文件,也许您应该下载notepad ++并选择该应用程序作为默认应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.