xdg-icon-resource install Windows .ico icon on Linux

Requirements:

  • icoutils – for converting .ico to .png
  • python
  • xdg-icon-resource
  • import os
    import sys
    from os import popen, path
    
    
    def run(cmd):
        print(f'Running {cmd} ...')
        output = popen(cmd).read()
        return output
    
    def pngsize(pngfile):
        return pngfile.split('_')[-1].split('x')[0]
    
    
    if __name__ == '__main__':
        icofile = sys.argv[1]
        iconname = icofile.replace(".ico", "").replace("-", " ")
    
        if path.exists('tmp'):
            run('rm -rf tmp')
    
        os.mkdir('tmp')
    
        run(f'icotool -x  {icofile} -o tmp')
    
        pngfiles = os.listdir('tmp')
    
        for pngfile in pngfiles:
            run(f'xdg-icon-resource install --novendor --size {pngsize(pngfile)} tmp/{pngfile} "{iconname}"')Code language: JavaScript (javascript)

    Usage:

    python xdg_icon_resource_install_ico.py folder-pictures.icoCode language: CSS (css)

patch/git apply – Hunk #hunkNumber FAILED at lineNumber (different line endings).

This error commonly issues with .patch files when the patch and target file use different line ending types (i.e. Windows/DOS/CLRF vs. Unix/LF):

touch .build/x64/status/clone
cd .build/x64/src/sshfs && for f in /cygdrive/c/github.com/sshfs-win/patches/*.patch; do patch --binary -p1 <$f; done
patching file sshfs.c
Hunk #1 FAILED at 365 (different line endings).

The unix2dos / dos2unix and commands can be used to perform conversion. One way to determine the line endings type is to open the files in a text editor like Notepad++ which can indicate the line endings type. But this can also be achieved using the unix2dos command’s -i / --info option:

dos2unix -i 00-passwd.patch
      36       0       0  no_bom    text    00-passwd.patch

The 36 indicates the number of lines that use DOS-style line endings. Here is a wrapper python script for this which prints out the line endings type:

import os
import sys
import re

if __name__ == '__main__':
    filename = sys.argv[1]
    infooutput = os.popen(f'dos2unix -i {filename}').read().strip()
    doslines = int(re.split(r'\s+', infooutput)[0])
    line_ending_type = 'Windows/DOS (CR LF)' if doslines > 0 else 'Unix (LF)'
    print(line_ending_type)Code language: JavaScript (javascript)

convert an .svg icon to .ico that contains all icon sizes

Requirements:

param(
    [Parameter(Mandatory=$true,Position=0)]
    [Alias("s")]
    $SvgFilename,
    [Parameter(Mandatory=$false,Position=1)]
    $IcoFilename = ""
)
if (-not [System.IO.Directory]::Exists("tmp")) {
    New-Item -Type Directory -Path tmp
}
$sizes = (16, 20, 24, 32, 40, 48, 64, 128, 256)
$filename = [System.IO.Path]::GetFileNameWithoutExtension($SvgFilename)

for ($i = 0; $i -lt $sizes.length; $i++) {
    $size = $sizes[$i];
    Write-Host $sizes[$i]
    Write-Host $filename
    inkscape -z -e tmp/${filename}_${size}.png -w $size -h $size -d 300 $SvgFilename
}

if ($IcoFilename.length -eq 0) {
    $IcoFilename = $SvgFilename.Replace(".svg", ".ico")
}
if (-not $IcoFilename.EndsWith(".ico")) {
    $IcoFilename = "${IcoFilename}/${filename}.ico"
}
Write-Host "Writing $IcoFilename"
convert tmp/${filename}_16.png tmp/${filename}_20.png tmp/${filename}_24.png tmp/${filename}_32.png tmp/${filename}_40.png tmp/${filename}_48.png tmp/${filename}_64.png tmp/${filename}_128.png tmp/${filename}_256.png ${IcoFilename}
Remove-Item -Path tmp -Recurse
Write-Host "Successfully wrote $IcoFilename"Code language: PHP (php)

Usage:

svg2ico.ps1 device-harddisk-usb.svg device-harddisk-usb.icoCode language: CSS (css)