第一種亂碼類型
在網(wǎng)絡(luò)上查了一圈恩伺,找到三個相關(guān)答案:
- 下載的文件名總是「亂碼」?這里有各平臺的解決方法 :
https://sspai.com/post/44360
Automator 流程:
https://cl.ly/2v1E3n3f1q2M - Mac OS X 下文件名亂碼出現(xiàn)的原因和解決方法:
https://zzi.io/?p=275 - 預(yù)組字符:
https://zh.wikipedia.org/wiki/預(yù)組字符
亂碼一:
FCPX??????????§?????′?è?·?? LOGO? ?????±??¤o????¤′Minimal Logo Stings.dmg
修復(fù)后:
FCPX插件:20個現(xiàn)代字幕條動畫Modern Lower Thirds .dmg
參考一中的代碼是關(guān)鍵:
for f in "$@"
do
fileName=$(basename ${f})
filePath=$(dirname ${f})
# 兩種亂碼類型 GBK两芳、UTF-8
{ fileNewName=$(echo $fileName | iconv -f UTF-8-Mac -t latin1 | iconv -f gbk)
} || { fileNewName=$(echo $fileName | iconv -f UTF-8-Mac -t latin1)
} || { fileNewName=$(echo $fileName) | iconv -f UTF-8-Mac -t GBK
} # 更新 2021-03-12
# 文件名正常或亂碼類型不屬上述兩種時,新文件名為空,則跳過
if [ -n "$fileNewName" ]; then
# 避免文件重復(fù):如果已存在修復(fù)后的文件名官扣,則在新文件名后加上隨機字符串。
if [ -e ${filePath}/$fileNewName ]; then
mv "$f" "${filePath}/${fileNewName}-${RANDOM}"
else
mv "$f" "${filePath}/${fileNewName}"
fi
fi
done
制作“服務(wù)”步驟
-
打開automator
1. 打開automator -
創(chuàng)建 quick action 類型
2. 創(chuàng)建 quick action 類型 -
將run shell script 拖到右邊下面
3. 將run shell script 拖到右邊下面 -
將粘貼代碼到 do/done 之間
4. 將粘貼代碼到 do/done 之間
保存上面的得到的文件产捞,系統(tǒng)會自動加載該文件醇锚,變成“右鍵”的服務(wù)哼御。
此處作廢【更新 2019-10-31】
我們使用參考一的文件坯临,下載后焊唬,雙擊安裝,點擊文件看靠,右擊就可以改名了赶促。
image.png
解釋
utf-8-mac(Decomposed) | ? | latin1(Precomposed) |
---|
我們試圖解讀一下,其中 iconv -f utf-8-mac -t latin1
是將字符先進行 precomposed
處理挟炬,接著 iconv -f gbk
鸥滨,那么為什么要這么處理呢?因為MacOS默認使用 "decomposed" 的字符編碼谤祖。什么是 decomposed
和 precomposed
呢婿滓?
瑞典語 的姓氏 ?str?m 可以有兩種Unicode表示方式,第一種采用預(yù)組字符? (U+00C5)與? (U+00F6), 第二種采用可分解基本字符A (U+0041)與上圓圈 (附加符號) (U+030A) 以及o(U+006F)與分音符 (U+0308).
- ?str?m (U+00C5 U+0073 U+0074 U+0072 U+00F6 U+006D)
- ?str?m (U+0041 U+030A U+0073 U+0074 U+0072 U+006F U+0308 U+006D)
說人話就是:window系統(tǒng)是用 拉丁字母+acute
的 precomposed 方式粥喜,而 mac 系統(tǒng)使用 拉丁字母+acute1+acute2
的方式凸主。
第二種亂碼類型
亂碼二:
%E7%BB%8F%E6%B5%8E%E5%AD%A6%E7%9A%84%E6%80%9D%E7%BB%B4%E6%96%B9%E5%BC%8F++%E4%BF%AE%E8%AE%A2%E7%AC%AC12%E7%89%88.pdf
修復(fù)后:
經(jīng)濟學(xué)的思維方式++修訂第12版.pdf
如果文件名出現(xiàn)了 %
符號是使用了encodeURI
將文件名編碼(參考 這里
),而下載的時候被編碼過的文件名沒有被改過來额湘,就直接下載了卿吐,所以顯示的亂碼。修復(fù)代碼如下:
for file in "$@"
do
# 等號兩邊不能有空格锋华,不然沒法執(zhí)行
fileName=$(basename -- "$file")
filePath=$(dirname -- "$file")
newName=$(echo -e "$fileName"| perl -pe 'y/+/ /;s/\%(\w\w)/chr hex $1/ge')
# 重命名文件
mv "$file" "${filePath}/${newName}"
echo %文件名亂碼修復(fù)成功嗡官!
done
效果如下:
制作步驟同上
- 在
automator
里面新建服務(wù), - 找到
run shell script
——粘貼代碼
- 找到
set value of variable
毯焕,拖到run shell script
下面衍腥,設(shè)置一個變量如:output
- 找到
Display Notification
,拖到set value of variable
下面纳猫,輸入變量名:output
紧阔。 - 保存。在右鍵就可以看到服務(wù)了续担。
第三種亂碼 [更新 2021-03-12]
闂茶瘽ID錛圛nDesign CS5錛夊彜綾嶆帓鐗堝強鐩稿叧闄勪歡
此亂碼來自MAC把GBK編碼以UTF-8-MAC解析得到的亂碼擅耽,可以根據(jù)依據(jù)一下代碼解析:
echo "闂茶瘽ID錛圛nDesign CS5錛夊彜綾嶆帓鐗堝強鐩稿叧闄勪歡" | iconv -t gbk
echo '闂茶瘽ID錛圛nDesign CS5錛夊彜綾嶆帓鐗堝強鐩稿叧闄勪歡' | \
python3 -c 'import sys; str=sys.stdin.readlines();str=str[0].encode("gb18030").decode("UTF_8");print(str)'
參考
-
http://mirror.informatimago.com/next/developer.apple.com/qa/qa2001/qa1235.html
Q: How do I convert a Unicode string to its precomposed form?
A: It is possible to convert a string to precomposed Unicode using APIs introduced in Mac OS X 10.2. The rest of this Q&A explains the difference between precomposed and decomposed Unicode, why you might want to convert to precomposed Unicode, and how to do so.
Precomposed versus Decomposed
Certain Unicode characters can be encoded in more than one way. For example, an á (A acute) can be encoded either precomposed, as U+00C1 (LATIN CAPITAL LETTER A WITH ACUTE), or decomposed, as U+0041 U+0301 (LATIN CAPITAL LETTER A followed by a COMBINING ACUTE ACCENT). Precomposed characters are more common in the Windows world, whereas decomposed characters are more common on the Mac.