Register file extensions / mime types in Linux

后端 未结 4 950
长发绾君心
长发绾君心 2020-11-27 10:54

I\'m developing a Linux application that has its own file format. I want my app to open when you double-click on those files.

How can I register a file extension and

相关标签:
4条回答
  • 2020-11-27 11:21

    1) in linux this is a function of your desktop environment rather than the os itself.
    2) GNOME and KDE have different methods to accomplish this.
    3) There's nothing stopping you from doing it both ways.

    0 讨论(0)
  • 2020-11-27 11:22

    Try this script: needs:

    1. your application icon -> $APP               = FIREFOX.png 
    2. your mimetype icon    -> application-x-$APP = HTML.png
    

    in the current directory:


    #BASH SCRIPT: Register_my_new_app_and_its_extension.sh
    APP="FOO"
    EXT="BAR"
    COMMENT="$APP's data file"
    
    # Create directories if missing
    mkdir -p ~/.local/share/mime/packages
    mkdir -p ~/.local/share/applications
    
    # Create mime xml 
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">
        <mime-type type=\"application/x-$APP\">
            <comment>$COMMENT</comment>
            <icon name=\"application-x-$APP\"/>
            <glob pattern=\"*.$EXT\"/>
        </mime-type>
    </mime-info>" > ~/.local/share/mime/packages/application-x-$APP.xml
    
    # Create application desktop
    echo "[Desktop Entry]
    Name=$APP
    Exec=/usr/bin/$APP %U
    MimeType=application/x-$APP
    Icon=$APP
    Terminal=false
    Type=Application
    Categories=
    Comment=
    "> ~/.local/share/applications/$APP.desktop
    
    # update databases for both application and mime
    update-desktop-database ~/.local/share/applications
    update-mime-database    ~/.local/share/mime
    
    # copy associated icons to pixmaps
    cp $APP.png                ~/.local/share/pixmaps
    cp application-x-$APP.png  ~/.local/share/pixmaps
    

    make sure: FOO binary is there in /usr/bin (or in $PATH)

    0 讨论(0)
  • 2020-11-27 11:26

    Use xdg-utils from freedesktop.org Portland.

    Register the icon for the MIME type:

    xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype
    

    Create a configuration file (freedesktop Shared MIME documentation):

    <?xml version="1.0"?>
     <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
       <mime-type type="application/x-mytype">  
       <comment>A witty comment</comment>
       <comment xml:lang="it">Uno Commento</comment>
       <glob pattern="*.myapp"/>
      </mime-type>
     </mime-info>
    

    Install the configuration file:

    xdg-mime install mytype-mime.xml
    

    This gets your files recognized and associated with an icon. xdg-mime default can be used for associating an application with the MIME type after you get a .desktop file installed.

    0 讨论(0)
  • 2020-11-27 11:44

    There are two parts to this. You need to register a new file type and then create a desktop entry for your application. The desktop entry associates your application with your new mime type.

    I thought that both Gnome and KDE (maybe only 4+?) used the freedesktop shared mime info spec, but I may well be wrong.

    0 讨论(0)
提交回复
热议问题