20260516

日本語入力用の辞書Windows用のタブ区切りテキストをmacOSの日本語追加辞書用にCSVに変換する

日本語入力用の辞書Windows用のタブ区切りテキストをmacOSの日本語追加辞書用にCSVに変換する

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

04_TSV2CSV.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006macOSのユーザー辞書用TSVをCSVに変換します
007
008辞書用のテキストの変換のみを目的としていますので
009項目内改行等には対応していません
010
011
012v1 初回作成
013
014com.cocolog-nifty.quicktimer.icefloe *)
015----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
016use AppleScript version "2.8"
017use framework "Foundation"
018use framework "AppKit"
019use framework "UniformTypeIdentifiers"
020use scripting additions
021
022property refMe : a reference to current application
023
024
025####################
026#ダイアログ
027set appFileManager to refMe's NSFileManager's defaultManager()
028set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
029set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
030set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
031tell application "Finder"
032   set aliasDefaultLocation to container of (path to me) as alias
033   set aliasDefaultLocation to (path to desktop folder from user domain) as alias
034end tell
035#UTIリスト
036set listUTI to {"public.plain-text", "public.tab-separated-values-text"} as list
037
038####################
039#メッセージマルチリンガル
040set appBundle to refMe's NSBundle's bundleWithIdentifier:("com.apple.osax.standardadditions")
041if appBundle = (missing value) then
042   set strBundlePath to ("/System/Library/ScriptingAdditions/StandardAdditions.osax") as text
043   set ocidBundlePathStr to refMe's NSString's stringWithString:(strBundlePath)
044   set ocidBundlePath to ocidBundlePathStr's stringByStandardizingPath()
045   set ocidBundlePathURL to refMe's NSURL's fileURLWithPath:(ocidBundlePath) isDirectory:(false)
046   set appBundle to refMe's NSBundle's alloc()'s initWithURL:(ocidBundlePathURL)
047end if
048set strChooseAFile to (appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")) as text
049
050
051set strMes to ("辞書用タブ区切りテキストを選択してください " & strChooseAFile & "") as text
052set strPrompt to ("辞書用タブ区切りテキストを選択してください  " & strChooseAFile & "") as text
053try
054   tell application "SystemUIServer"
055      activate
056      set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
057   end tell
058on error strErrMes number numErrNo
059   log strErrMes & numErrNo
060   return false
061end try
062#
063set strFilePath to (POSIX path of aliasFilePath) as text
064set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
065set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
066set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
067#
068set ocidFileName to ocidFilePathURL's lastPathComponent()
069set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping()
070set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
071#
072set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
073set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("csv.txt")
074#
075#NSString
076set ocidEnc to (refMe's NSUTF8StringEncoding)
077set listReadStrings to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(ocidEnc) |error|:(reference)
078if (first item of listReadStrings) is false then
079   set ocidEnc to (refMe's NSShiftJISStringEncoding)
080   set listReadStrings to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(ocidEnc) |error|:(reference)
081   set ocidReadStrings to (first item of listReadStrings)
082else
083   set ocidReadStrings to (first item of listReadStrings)
084end if
085#改行をUNIXに強制
086set ocidReplacedStrings to (ocidReadStrings's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed))
087set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(return) withString:(linefeed))
088set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed))
089set ocidStringArray to ocidReplacedStrings's componentsSeparatedByString:(linefeed)
090#空の項目を削除
091set appPredicate to refMe's NSPredicate's predicateWithFormat_("SELF != nil AND SELF != '' AND SELF != %@", (missing value))
092set ocidFilteredArray to ocidStringArray's filteredArrayUsingPredicate:(appPredicate)
093#出力用のテキスト
094set ocidOutputString to refMe's NSMutableString's alloc()'s init()
095
096repeat with itemLineString in ocidFilteredArray
097   set ocidLineArray to (itemLineString's componentsSeparatedByString:(tab))
098   set ocidShortcut to ocidLineArray's firstObject()
099   set ocidPhrase to (ocidLineArray's objectAtIndex:(1))
100   (ocidOutputString's appendString:(ocidShortcut))
101   (ocidOutputString's appendString:(","))
102   (ocidOutputString's appendString:(ocidPhrase))
103   (ocidOutputString's appendString:(linefeed))
104end repeat
105
106
107set listDone to ocidOutputString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
108if (item 1 of listDone) is true then
109   return "正常終了"
110else if (item 1 of listDone) is false then
111   log (item 2 of listDone)'s localizedDescription() as text
112   return "保存に失敗しました"
113end if
114
115
AppleScriptで生成しました