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