立即設定熟悉的文件關聯

自動設定檔案關聯,即選擇將從資源管理器/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

來源: www.habr.com

添加評論