将Mono应用程序注册到OSX上的Uri方案中

本文关键字:上的 Uri 方案 OSX Mono 应用程序 注册 | 更新日期: 2023-09-27 18:35:53

你好,我正在为Windows和Mac开发一个注册到uri方案的应用程序

    static void RegisterURI()
    {
        if (Environment.OSVersion.Platform.ToString() == "Unix") //I'll never use it on any linux machine so it should be OK
        {
            //What should I put HERE ??
        }
        else
        {
            //on récupère le chemin de l'assembly
            string MyPos = System.Reflection.Assembly.GetAssembly(typeof(Program)).Location;
            var protocol = Registry.ClassesRoot
                .CreateSubKey("serialcodereader");
            protocol.SetValue("", "URL:Lancer l'écouteur de liseuse de code barre COM");
            protocol.SetValue("URL Protocol", "");
            var icon = protocol.CreateSubKey("DefaultIcon");
            icon.SetValue("", MyPos + ",1");
            var command = protocol.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
            command.SetValue("", '"' + MyPos + '"');
        }
    }

以下是我在Windows中使用的代码,但如何在OSX中使用?

ps:很高兴知道:我一生中从未使用过任何Mac甚至Iphone,所以对那些奇怪的动物保持简单:-(

将Mono应用程序注册到OSX上的Uri方案中

我正在做类似的事情。我确实建议浪费一些时间来检查:NSBundle。MainBundle。InfoDictionary。ToString((

并检查此源代码:https://github.com/mono/monodevelop/blob/master/main/src/addins/MacPlatform/MacPlatform.cs因为他们在运行时使用NSBundle结构来更改它。

一旦我找到一些解决方案,我会尽快与您分享。


我得到了一个功能性的解决方案。

  1. 我从这个博客中挑选了一些信息:https://mhut.ch/journal/2010/01/24/creating_mac_app_bundle_for_gtk_app并在那里修改了脚本
  2. 我使用Mono编写了一个C#/Gtk应用程序,以便在Windows/Linux环境中可重复使用
  3. 在那个应用程序中,我使用了您在Windows环境中关联URL模式时使用的一些方法
  4. 我写了一个.ini文件来描述我的应用程序
  5. 我写了一个bash脚本,它使用编译后的应用程序、.ini文件和图标,并创建了一个Apple Bundle文件
  6. 我测试了从html页面调用单个URL

myProject.ini

[project]
CFBundleExecutable=ExecutableName
CFBundleName="Human Readable Executable Name"
CFBundleVersion="1.0"
REV_DOMAIN="com.sample"
URLScheme="myapp"
# src is where Mono left your executable
src='/Users/myself/Projects/MyApp/MyApp/bin/Release/'
CFBundleURLIconFile='appicons.icns'

用法示例:

$ build-mac-app.sh myProject

这将生成一个扩展名为".app"的文件夹,以及运行它所需的所有内容。首先,记住运行它,一是为了MacOS建立关联,二是注意该方案的使用,因为如果你在其他开发中使用了url方案,它将失败或打开其他应用程序。

HTML测试文件

<a href=">myapp://coisa-a-ser-assiada">阿西斯纳多集团<a>

生成器脚本

我最初是用ptbr写的,因为它最初是今天为内部使用而开发的

#!/bin/bash
echo ""
echo "EDortta/build-mac-app.sh"
echo "Construtor de aplicativos para MacOSx"
echo "Este script usa a saída gerada por MonoDevelop C#/Gtk"
echo "e produz um Mac App Bundle pronto para ser distribuido"
echo "------------------------------------------------------"
if [ -f "$1" ]; then
  config="$1"
  CFBundleExecutable=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/CFBundleExecutable/{print $2}' | tr -d '"' | tr -d "'"`
  CFBundleName=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/CFBundleName/{print $2}' | tr -d '"' | tr -d "'"`
  CFBundleVersion=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/CFBundleVersion/{print $2}' | tr -d '"' | tr -d "'"`
  REV_DOMAIN=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/REV_DOMAIN/{print $2}' | tr -d '"' | tr -d "'"`
  CFBundleURLIconFile=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/CFBundleURLIconFile/{print $2}' | tr -d '"' | tr -d "'"`
  URLScheme=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/URLScheme/{print $2}' | tr -d '"' | tr -d "'"`
  src=`cat $config | sed -n '/'[project']/,/'[.*']/p' | awk -F= '/src/{print $2}' | tr -d '"' | tr -d "'"`
  echo "src=$src"
  if [ ! -d "$CFBundleExecutable.app" ]; then
    mkdir "$CFBundleExecutable.app"
    rsync -rvt mac-skel/* "$CFBundleExecutable.app/"
    sed -i -e "s/%CFBundleExecutable%/$CFBundleExecutable/g" "$CFBundleExecutable.app/Contents/Info.plist"
    sed -i -e "s/%CFBundleName%/$CFBundleName/g" "$CFBundleExecutable.app/Contents/Info.plist"
    sed -i -e "s/%CFBundleVersion%/$CFBundleVersion/g" "$CFBundleExecutable.app/Contents/Info.plist"
    sed -i -e "s/%REV_DOMAIN%/$REV_DOMAIN/g" "$CFBundleExecutable.app/Contents/Info.plist"
    sed -i -e "s/%URLScheme%/$URLScheme/g" "$CFBundleExecutable.app/Contents/Info.plist"
    sed -i -e "s/%CFBundleURLIconFile%/$CFBundleURLIconFile/g" "$CFBundleExecutable.app/Contents/Info.plist"
    if [ -f "$CFBundleExecutable.app/Contents/Info.plist-e" ]; then
        rm -f "$CFBundleExecutable.app/Contents/Info.plist-e"
    fi
    if [ -f "$CFBundleURLIconFile" ]; then
        cp "$CFBundleURLIconFile" "$CFBundleExecutable.app/Contents/Resources/$CFBundleExecutable.icns"
    fi
    mv "$CFBundleExecutable.app/Contents/MacOS/appname.sh" "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh"
    cp -r "$src/" "$CFBundleExecutable.app/Contents/MacOS/"
    sed -i -e "s/%CFBundleExecutable%/$CFBundleExecutable/g" "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh"
    sed -i -e "s/%CFBundleName%/$CFBundleName/g" "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh"
    if [ -f "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh-e" ]; then
        rm -f "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh-e"
    fi
    chmod +x "$CFBundleExecutable.app/Contents/MacOS/$CFBundleExecutable.sh"
  else
    echo "$CFBundleExecutable.app já existe"
    echo "Por favor, considere remover a pasta"
  fi
else
    echo "Indique o seu projeto C#/GTK"
    echo "Caso precise de um exemplo, use project-sample.ini"
fi

幕后结构

在"buildmacapp.sh"和应用程序图标(从项目或其他来源移动或复制(的同一杠杆上,有一个mac-skel文件夹,其中包含以下文件:

mac-skel/
└── Contents
    ├── Info.plist
    ├── MacOS
    │   └── appname.sh
    └── Resources

mac skel/Contents/Info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>pt-br</string>
    <key>CFBundleExecutable</key>
    <string>MacOS/%CFBundleExecutable%.sh</string>
    <key>CFBundleIconFile</key>
    <string>../Resources/%CFBundleExecutable%.icns</string>
    <key>CFBundleIdentifier</key>
    <string>%REV_DOMAIN%.%CFBundleExecutable%</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>%CFBundleName%</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>%CFBundleVersion%</string>
    <key>CFBundleSignature</key>
    <string>xmmd</string>
    <key>CFBundleVersion</key>
    <string>%CFBundleVersion%</string>
    <key>NSAppleScriptEnabled</key>
    <string>NO</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLIconFile</key>
            <string>%CFBundleURLIconFile%</string>
            <key>CFBundleURLName</key>
            <string>%REV_DOMAIN%.%CFBundleExecutable%</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>%URLScheme%</string>
            </array>
            <key>CFBundleURLTypes</key>
            <string>Editor</string>
        </dict>
    </array>
</dict>
</plist>

mac skel/Contents/MacOS/appname.sh这是一个从博客显示之前

#!/bin/sh
#get the bundle's MacOS directory full path
DIR=$(cd "$(dirname "$0")"; pwd)
#change these values to match your app
EXE_PATH="$DIR'%CFBundleExecutable%.exe"
PROCESS_NAME="%CFBundleExecutable%"
BUNDLENAME="%CFBundleName%"
#set up environment
MONO_FRAMEWORK_PATH=/Library/Frameworks/Mono.framework/Versions/Current
export DYLD_FALLBACK_LIBRARY_PATH="$DIR:$MONO_FRAMEWORK_PATH/lib:/lib:/usr/lib"
export PATH="$MONO_FRAMEWORK_PATH/bin:$PATH"
#mono version check
REQUIRED_MAJOR=2
REQUIRED_MINOR=4
VERSION_TITLE="$BUNDLENAME Não pode ser aberta"
VERSION_MSG="$BUNDLENAME requer o Framework Mono versão $REQUIRED_MAJOR.$REQUIRED_MINOR ou  posterior."
DOWNLOAD_URL="http://www.go-mono.com/mono-downloads/download.html"
MONO_VERSION="$(mono --version | grep 'Mono JIT compiler version ' |  cut -f5 -d' )"
MONO_VERSION_MAJOR="$(echo $MONO_VERSION | cut -f1 -d.)"
MONO_VERSION_MINOR="$(echo $MONO_VERSION | cut -f2 -d.)"
if [ -z "$MONO_VERSION" ] '
    || [ $MONO_VERSION_MAJOR -lt $REQUIRED_MAJOR ] '
    || [ $MONO_VERSION_MAJOR -eq $REQUIRED_MAJOR -a $MONO_VERSION_MINOR -lt $REQUIRED_MINOR ] 
then
    osascript '
    -e "set question to display dialog '"$VERSION_MSG'" with title '"$VERSION_TITLE'" buttons {'"Cancelar'", '"Baixar...'"} default button 2" '
    -e "if button returned of question is equal to '"Baixar...'" then open location '"$DOWNLOAD_URL'""
    echo "$VERSION_TITLE"
    echo "$VERSION_MSG"
    exit 1
fi
#get an exec command that will work on the current OS version
OSX_VERSION=$(uname -r | cut -f1 -d.)
if [ $OSX_VERSION -lt 9 ]; then  # If OSX version is 10.4
    MONO_EXEC="exec mono"
else
    MONO_EXEC="exec -a '"$PROCESS_NAME'" mono"
fi
#create log file directory if it doesn't exist
LOG_FILE="$HOME/Library/Logs/$BUNDLENAME/$BUNDLENAME.log"
mkdir -p "`dirname '"$LOG_FILE'"`"
#run app using mono
$MONO_EXEC $MONO_OPTIONS "$EXE_PATH" $* 2>&1 1> "$LOG_FILE"