使用Url Schemes打开本地程序

本文介绍了如何通过创建自定义Url Schemes在不同操作系统(Windows、Mac和Linux)上启动本地程序。在Windows上,这涉及到编辑注册表;在Mac上,只需要更新应用程序的Info.plist文件;而在Linux下,方法有所不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试:


将下列内容保存为test.html,用浏览器打开,单击“Test Url Schemes”超链接,即可看到效果(会启动相应的程序)

<HTML>
	<head>
	</head>
	<body>
		<a href="TestUrlSchemes://">Test Url Schemes</a>
	</body>
</HTML>



实现:


windows下:

只要向注册表的HKEY_CLASSES_ROOT中写入指定格式的信息,即可

用一个bat来快速说明:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\TestUrlSchemes]
@="Test URL Schemes"
"URL Protocol"="c:\\myapp.exe"

[HKEY_CLASSES_ROOT\TestUrlSchemes\DefaultIcon]
@="c:\\myapp.exe,1"

[HKEY_CLASSES_ROOT\TestUrlSchemes\shell]

[HKEY_CLASSES_ROOT\TestUrlSchemes\shell\open]

[HKEY_CLASSES_ROOT\TestUrlSchemes\shell\open\command]
@="\"c:\\myapp.exe\" \"%1\""

其中“TestUrlSchemes”这个可以改成你要取的自定义协议名称(貌似不能有特殊字符,且windows下会忽略大小写),

“c:\\myapp.exe”则可改成安装的程序的绝对路径,就是这么简单



mac下:

更简单,只需修改Contents下的Info.plist文件

比如,修改前:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>CFBundleIconFile</key>
        <string></string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleGetInfoString</key>
        <string>Created by Qt/QMake</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleExecutable</key>
        <string>myapp</string>
        <key>CFBundleIdentifier</key>
        <string>com.yourcompany.myapp</string>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>NOTE</key>
        <string>This file was generated by Qt/QMake.</string>
</dict>
</plist>

修改后:(增加“CFBundleURLTypes”等项)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>CFBundleIconFile</key>
        <string></string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleGetInfoString</key>
        <string>Created by Qt/QMake</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleExecutable</key>
        <string>myapp</string>
        <key>CFBundleIdentifier</key>
        <string>com.yourcompany.myapp</string>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleURLTypes</key>
        <array>
                <dict>
                        <key>CFBundleURLSchemes</key>
                        <array>
                                <string>TestUrlSchemes</string>
                        </array>
                        <key>CFBundleURLName</key>
                        <string>com.yourcompany.myapp</string>
                </dict>
        </array>
        <key>NOTE</key>
        <string>This file was generated by Qt/QMake.</string>
</dict>
</plist>


注意:第一次,需要手动打开程序(可能是因为程序不是从AppStore下载的,需要右键打开程序),之后,就可以直接用自定义协议的方式打开了



linux下:

在软件对应的.desktop文件增加字段“MimeType=x-scheme-handler/testurlschemes”

注意:
1、testurlschemes必须纯英文的小写
2、ubuntu14.04已经支持,centos6.8也支持(centos6.5经测试不支持)
3、.desktop文件放在/usr/share/applications/目录下(如果是手动改这个文件,执行update-desktop-database立即生效)

test.desktop文件内容大致如下:
[Desktop Entry]
GenericName=web transmit adapter
Name=web_transmit_adapter
Version=1.0.0
Exec=bash /usr/bin/web_transmit_adapter/web_transmit_adapter.sh
Comment=web transmit adapter on Linux x64
Icon=web_transmit_adapter.png
Type=Application
Terminal=false
StartupNotify=false
Encoding=UTF-8
Categories=Utility
MimeType=x-scheme-handler/testurlschemes






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值