立即设置熟悉的文件关联

自动设置文件关联,即选择将从资源管理器/Finder 打开文件的程序。 我分享。

问题先来。 默认情况下,具有所需扩展名的文件通常不会被任何程序打开,即使打开,也会被某些 iTunes 打开。 在Windows下,安装(甚至卸载)程序时,有时必要的关联会完全丢​​失:有时卸载GIMP,ico文件会从常用的文件查看器接管到标准照片库。 为什么? 为了什么? 未知...如果我找到了新的编辑器,或者由于各种原因重新安装了怎么办? 如果有多于一台计算机怎么办? 一般来说,在对话中点击鼠标就是一种娱乐。

相反,我在 Dropbox 上保存了两个文件,现在我几乎可以立即让计算机世界恢复正常。 而你这么多年一直在等待什么......接下来是 Windows 和 macOS 的秘诀。

Windows

在 Windows 控制台中 cmd.exe 这分两个阶段完成:

ftype my_file_txt="C:Windowsnotepad.exe" "%1"
assoc .txt=my_file_txt

更改立即生效。 尽管该关联是为特定用户注册的,但由于某种原因,这些命令需要以管理员身份运行。 从 bat 文件运行时,不要忘记加倍百分号 (%%1)。 Windows 7 Ultimate 64 位的神奇世界......

macos

在 MacOS 中,使用实用程序可以方便地设置关联 杜蒂。 它是通过安装的 brew install duti。 使用示例:

duti -s com.apple.TextEdit .txt "editor"

更改立即生效,无需 sudo。 这里的参数“com.apple.TextEdit”就是我们需要的程序的所谓“bundle id”。 “editor”参数是关联类型:“editor”用于编辑,“viewer”用于查看,“all”用于所有内容。

你可以这样找到“bundle id”:如果有第三个版本的“/Applications/Sublime Text.app”,那么它的bundle ID将是“com.sublimetext.3”,或者其他一些:

> osascript -e 'id of app "Sublime Text"'
com.sublimetext.3

在 macOS Sierra 上测试。

Windows 的最终脚本 (.bat)

@echo off

set XNVIEW=C:Program Files (x86)XnViewxnview.exe
set SUBLIME=C:Program FilesSublime Text 3sublime_text.exe
set FOOBAR=C:Program Files (x86)foobar2000foobar2000.exe

call :assoc_ext "%SUBLIME%" txt md js json css java sh yaml
call :assoc_ext "%XNVIEW%" png gif jpg jpeg tiff bmp ico
call :assoc_ext "%FOOBAR%" flac fla ape wav mp3 wma m4a ogg ac3

goto :eof

:assoc_ext
  set EXE=%1
  shift
  :loop
  if "%1" neq "" (
    ftype my_file_%1=%EXE% "%%1"
    assoc .%1=my_file_%1
    shift
    goto :loop
  )
goto :eof

macOS 的最终脚本 (.sh)

#!/bin/bash

# this allows us terminate the whole process from within a function
trap "exit 1" TERM
export TERM_PID=$$

# check `duti` installed
command -v duti >/dev/null 2>&1 || 
  { echo >&2 "duti required: brew install duti"; exit 1; }

get_bundle_id() {
    osascript -e "id of app """ || kill -s TERM $TERM_PID;
}

assoc() {
    bundle_id=$1; shift
    role=$1; shift
    while [ -n "$1" ]; do
        echo "setting file assoc: $bundle_id .$1 $role"
        duti -s "$bundle_id" "." "$role"
        shift
    done
}

SUBLIME=$(get_bundle_id "Sublime Text")
TEXT_EDIT=$(get_bundle_id "TextEdit")
MPLAYERX=$(get_bundle_id "MPlayerX")

assoc "$SUBLIME" "editor" txt md js jse json reg bat ps1 cfg sh bash yaml
assoc "$MPLAYERX" "viewer" mkv mp4 avi mov webm
assoc "$MPLAYERX" "viewer" flac fla ape wav mp3 wma m4a ogg ac3

来源: habr.com

添加评论