Encode Decode

文字列:濁音の分割


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#アートボードをアートホ ゙ート ゙に分割します
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011
012property refMe : a reference to current application
013
014set strMes to ("濁音等の文字を文字と濁音に分割します") as text
015
016########################
017## クリップボードの中身取り出し
018########################
019###初期化
020set appPasteboard to refMe's NSPasteboard's generalPasteboard()
021##格納されているタイプをリストにして
022set ocidPastBoardTypeArray to appPasteboard's types
023###テキストがあれば
024set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text")
025if (boolContain as boolean) is true then
026  ###値を格納する
027  tell application "Finder"
028    set strReadString to (the clipboard as text) as text
029  end tell
030else
031  ###UTF8が無いなら
032  ##テキスト形式があるか?確認して
033  set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
034  ##テキスト形式があるなら
035  if (boolContain as boolean) is true then
036    set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
037    ocidTypeClassArray's addObject:(refMe's NSString)
038    set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)
039    set strReadString to ocidReadString as text
040  else
041    log "テキストなし"
042    set strReadString to strMes as text
043  end if
044end if
045##############################
046#####ダイアログ テキスト入力
047##############################
048set strName to (name of current application) as text
049if strName is "osascript" then
050  tell application "Finder" to activate
051else
052  tell current application to activate
053end if
054set aliasIconPath to POSIX file "/System/Applications/Font Book.app/Contents/Resources/AppIcon.icns" as alias
055try
056  set strMes to ("ダンプする文字列を入力してください") as text
057  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
058on error
059  log "エラーしました"
060  return
061end try
062if "OK" is equal to (button returned of recordResult) then
063  set strReturnedText to (text returned of recordResult) as text
064else if (gave up of recordResult) is true then
065  return "時間切れです"
066else
067  return "キャンセル"
068end if
069
070##############################
071#####戻り値整形
072##############################
073set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
074###タブと改行を除去しておく
075set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
076ocidTextM's appendString:(ocidResponseText)
077###除去する文字列リスト
078set listRemoveChar to {"\n", "\r", "\t"} as list
079##置換
080repeat with itemChar in listRemoveChar
081  set strPattern to itemChar as text
082  set strTemplate to ("") as text
083  set ocidOption to (refMe's NSRegularExpressionCaseInsensitive)
084  set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference))
085  if (item 2 of listResponse) ≠ (missing value) then
086    log (item 2 of listResponse)'s localizedDescription() as text
087    return "正規表現パターンに誤りがあります"
088  else
089    set ocidRegex to (item 1 of listResponse)
090  end if
091  set numLength to ocidResponseText's |length|()
092  set ocidRange to refMe's NSRange's NSMakeRange(0, numLength)
093  set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate))
094end repeat
095set strInput to ocidResponseText as string
096
097##############################
098#####分割
099##############################
100
101set strCommandText to ("function doGetCharUni() {let strText = '" & strInput & "'; let strOutput = strText.normalize('NFD');let charArray = Array.from(strOutput); return charArray; } doGetCharUni();") as text
102# log "\n" & strCommandText & "\n"
103set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text
104# log "\n" & strExecCommand & "\n"
105try
106  set strResponse to (do shell script strExecCommand) as string
107on error
108  log "osascript でエラーしました"
109  return false
110end try
111
112
113##############################
114#####戻す
115##############################
116set strDelim to AppleScript's text item delimiters
117set AppleScript's text item delimiters to ","
118set listResponse to every text item of strResponse
119set AppleScript's text item delimiters to strDelim
120set strOutPut to ("") as text
121repeat with itemArray in listResponse
122  set strOutPut to (strOutPut & itemArray) as text
123end repeat
124
125set strDelim to AppleScript's text item delimiters
126set AppleScript's text item delimiters to " "
127set listOutPut to every text item of strOutPut
128set AppleScript's text item delimiters to strDelim
129set strReturn to listOutPut as text
130
131
132##############################
133#####ダイアログ
134##############################
135tell current application
136  set strName to name as text
137end tell
138####スクリプトメニューから実行したら
139if strName is "osascript" then
140  tell application "Finder"
141    activate
142  end tell
143else
144  tell current application
145    activate
146  end tell
147end if
148try
149  set recordResult to (display dialog "分割しました" with title "戻り値です" default answer strReturn buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
150on error
151  return "エラーしました"
152end try
153if (gave up of recordResult) is true then
154  return "時間切れです"
155end if
156##############################
157#####自分自身を再実行
158##############################
159if button returned of recordResult is "再実行" then
160  tell application "Finder"
161    set aliasPathToMe to (path to me) as alias
162  end tell
163  run script aliasPathToMe with parameters "再実行"
164end if
165##############################
166#####値のコピー
167##############################
168if button returned of recordResult is "クリップボードにコピー" then
169  try
170    set strText to text returned of recordResult as text
171    ####ペーストボード宣言
172    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
173    set ocidText to (refMe's NSString's stringWithString:(strText))
174    appPasteboard's clearContents()
175    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
176  on error
177    tell application "Finder"
178      set the clipboard to strText as text
179    end tell
180  end try
181end if
182
183
184return 0
185
AppleScriptで生成しました

|

[JavaScript]文字列を \u\U形式のユニコードエスケープにエンコード(BMP外の文字に対応)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# 文字列を \u\U形式のユニコードエスケープにエンコードします
005# Unicodeエスケープ
006#com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012
013property refMe : a reference to current application
014
015################################
016##デフォルトクリップボードからテキスト取得
017################################
018set appPasteboard to refMe's NSPasteboard's generalPasteboard()
019set ocidTypeArray to appPasteboard's types()
020set boolContain to ocidTypeArray's containsObject:("public.utf8-plain-text")
021if boolContain = true then
022  try
023    set ocidPasteboardArray to appPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
024    set ocidPasteboardStrings to ocidPasteboardArray's firstObject()
025  on error
026    set ocidStringData to appPasteboard's stringForType:("public.utf8-plain-text")
027    set ocidPasteboardStrings to (refMe's NSString's stringWithString:(ocidStringData))
028  end try
029else
030  set ocidPasteboardStrings to (refMe's NSString's stringWithString:(""))
031end if
032set strDefaultAnswer to ocidPasteboardStrings as text
033
034################################
035######ダイアログ
036################################
037#####ダイアログを前面に
038tell current application
039  set strName to name as text
040end tell
041####スクリプトメニューから実行したら
042if strName is "osascript" then
043  tell application "Finder" to activate
044else
045  tell current application to activate
046end if
047set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns") as alias
048try
049  set strMes to ("読める文字をユニコードエスケープします") as text
050  
051  set recordResponse to (display dialog strMes with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
052on error
053  log "エラーしました"
054  return "エラーしました"
055end try
056if true is equal to (gave up of recordResponse) then
057  return "時間切れですやりなおしてください"
058end if
059if "OK" is equal to (button returned of recordResponse) then
060  set strResponse to (text returned of recordResponse) as text
061else
062  log "キャンセルしました"
063  return "キャンセルしました"
064end if
065
066##通常テキストの場合
067set strText to strResponse as text
068###改行 タブの除去
069set ocidText to refMe's NSString's stringWithString:(strText)
070set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
071ocidTextM's appendString:(ocidText)
072##行末の改行
073set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
074set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
075##タブ除去
076set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
077
078################################
079######本処理
080################################
081set ocidTextM to (ocidTextM's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\"))
082set ocidTextM to (ocidTextM's stringByReplacingOccurrencesOfString:("'") withString:("\\'"))
083set ocidTextM to (ocidTextM's stringByReplacingOccurrencesOfString:("\"") withString:("\\\""))
084#コマンド整形 まずは4桁で生成して
085set strSetValue to ocidTextM as Unicode text
086set strCommandText to ("function doGetCharUni() {let strText = '" & strSetValue & "';let listOutputArray = [];let listCharArray = Array.from(strText);for (let itemChar of listCharArray) {let numCodePoint = itemChar.codePointAt(0);let strUnicodeNo = numCodePoint.toString(16).toUpperCase();let strFormattedUnicodeNo = strUnicodeNo.padStart(4, '0'); strUnicodeNo = '\\\\\\u' + strFormattedUnicodeNo; listOutputArray.push(strUnicodeNo);}let strOutput = listOutputArray.join('');return strOutput;} doGetCharUni();") as text
087# log "\n" & strCommandText & "\n"
088#コマンド実行
089set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text
090try
091  set strResponse to (do shell script strExecCommand) as text
092on error
093  log "osascript でエラーしました"
094  return false
095end try
096####BMP内判定
097set strDelim to AppleScript's text item delimiters
098set AppleScript's text item delimiters to "\\u"
099set listResponse to every text item of strResponse
100set AppleScript's text item delimiters to strDelim
101set boolBMP to true as boolean
102repeat with itemText in listResponse
103  set strText to itemText as text
104  if strText is not "" then
105    set numCntItemText to (count of every text item of strText) as integer
106    if numCntItemText > 4 then
107      set boolBMP to false as boolean
108      exit repeat
109    end if
110  end if
111end repeat
112####処理分岐
113if boolBMP is true then
114  #BMP内文字で構成されていれば\u表記のまま戻す
115  set strDecodedText to strResponse as text
116else if boolBMP is false then
117  #BMp外の文字がある場合は8桁で作成しなおし
118  set strCommandText to ("function doGetCharUni() {let strText = '" & strSetValue & "';let listOutputArray = [];let listCharArray = Array.from(strText);for (let itemChar of listCharArray) {let numCodePoint = itemChar.codePointAt(0);let strUnicodeNo = numCodePoint.toString(16).toUpperCase();let strFormattedUnicodeNo = strUnicodeNo.padStart(8, '0'); strUnicodeNo = '\\\\\\U' + strFormattedUnicodeNo; listOutputArray.push(strUnicodeNo);}let strOutput = listOutputArray.join('');return strOutput;} doGetCharUni();") as text
119  # log "\n" & strCommandText & "\n"
120  #コマンド実行
121  set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text
122  try
123    set strResponse to (do shell script strExecCommand) as text
124  on error
125    log "osascript でエラーしました"
126    return false
127  end try
128  set strDecodedText to strResponse as text
129end if
130
131################################
132######ダイアログ
133################################
134#####ダイアログを前面に
135tell current application
136  set strName to name as text
137end tell
138####スクリプトメニューから実行したら
139if strName is "osascript" then
140  tell application "Finder" to activate
141else
142  tell current application to activate
143end if
144set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
145set strMes to ("戻り値です\r" & strDecodedText) as text
146
147set recordResult to (display dialog strMes with title "%デコード" default answer strDecodedText buttons {"クリップボードにコピー", "終了", "再実行"} default button "終了" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer)
148
149if button returned of recordResult is "クリップボードにコピー" then
150  try
151    set strText to text returned of recordResult as text
152    ####ペーストボード宣言
153    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
154    set ocidText to (refMe's NSString's stringWithString:(strText))
155    appPasteboard's clearContents()
156    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
157  on error
158    tell application "Finder"
159      set the clipboard to strText as text
160    end tell
161  end try
162end if
163
164##############################
165#####自分自身を再実行
166##############################
167if button returned of recordResult is "再実行" then
168  tell application "Finder"
169    set aliasPathToMe to (path to me) as alias
170  end tell
171  run script aliasPathToMe with parameters "再実行"
172end if
AppleScriptで生成しました

|

[U+XXXXX]ユニコードのコードポイントを取得する(修正)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006# com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
014
015
016set strText to ("文字列を入力") as «class utf8»
017
018########################
019## クリップボードの中身取り出し
020########################
021###初期化
022set appPasteboard to refMe's NSPasteboard's generalPasteboard()
023##格納されているタイプをリストにして
024set ocidPastBoardTypeArray to appPasteboard's types
025###テキストがあれば
026set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text")
027if (boolContain as boolean) is true then
028  ###値を格納する
029  tell application "Finder"
030    set strReadString to (the clipboard as text) as «class utf8»
031  end tell
032else
033  ###UTF8が無いなら
034  ##テキスト形式があるか?確認して
035  set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
036  ##テキスト形式があるなら
037  if (boolContain as boolean) is true then
038    set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
039    ocidTypeClassArray's addObject:(refMe's NSString)
040    set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)
041    set strReadString to ocidReadString as «class utf8»
042  else
043    log "テキストなし"
044    set strReadString to strMes as «class utf8»
045  end if
046end if
047##############################
048#####ダイアログ
049##############################
050###ダイアログを前面に出す
051set strName to (name of current application) as text
052if strName is "osascript" then
053  tell application "Finder" to activate
054else
055  tell current application to activate
056end if
057set strMes to "変換するテキストを入力してください"
058set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
059try
060  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
061on error
062  log "エラーしました"
063  return
064end try
065if "OK" is equal to (button returned of recordResult) then
066  set strReturnedText to (text returned of recordResult) as «class utf8»
067else if (gave up of recordResult) is true then
068  return "時間切れです"
069else
070  return "キャンセル"
071end if
072##############################
073#####戻り値整形
074##############################
075set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
076###タブと改行を除去しておく
077set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
078ocidTextM's appendString:(ocidResponseText)
079###除去する文字列リスト
080set listRemoveChar to {"\n", "\r", "\t"} as list
081##置換
082repeat with itemChar in listRemoveChar
083  set strPattern to itemChar as text
084  set strTemplate to ("") as text
085  set ocidOption to (refMe's NSRegularExpressionCaseInsensitive)
086  set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference))
087  if (item 2 of listResponse) ≠ (missing value) then
088    log (item 2 of listResponse)'s localizedDescription() as text
089    return "正規表現パターンに誤りがあります"
090  else
091    set ocidRegex to (item 1 of listResponse)
092  end if
093  set numLength to ocidResponseText's |length|()
094  set ocidRange to refMe's NSRange's NSMakeRange(0, numLength)
095  set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate))
096end repeat
097##############################
098#####本処理
099##############################
100###最低限のエスケープ処理
101set ocidReplacedStrings to (ocidResponseText's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\"))
102set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:("'") withString:("\\'"))
103set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:("\"") withString:("\\\""))
104set strText to ocidReplacedStrings as «class utf8»
105
106###戻り値用テキスト
107set strOutPutText to (strText & "\n") as text
108
109#コマンド整形
110set strCommandText to ("function doGetCharUni() {let strText = '" & strText & "';let listOutputArray = [];let listCharArray = Array.from(strText);for (let itemChar of listCharArray) {let numCodePoint = itemChar.codePointAt(0);let strUnicodeNo = numCodePoint.toString(16).toUpperCase();let strFormattedUnicodeNo = strUnicodeNo.padStart(4, '0'); strUnicodeNo = 'U+' + strFormattedUnicodeNo; listOutputArray.push(strUnicodeNo);}let strOutput = listOutputArray.join('');return strOutput;} doGetCharUni();") as text
111#log "\n" & strCommandText & "\n"
112#コマンド実行
113set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text
114try
115  set strResponse to (do shell script strExecCommand) as string
116on error
117  log "osascript でエラーしました"
118  return false
119end try
120
121set strOutPutText to (strOutPutText & strResponse & "\n\n") as text
122
123##文字単位 一文字づつ
124set numCntChar to (count of character of strText) as integer
125
126repeat with itemCharNo from 1 to (numCntChar) by 1
127  set strSetStr to (character itemCharNo of strText) as «class utf8»
128  
129  #コマンド整形
130  set strCommandText to ("function doGetCharUni() {let strText = '" & strSetStr & "';let listOutputArray = [];let listCharArray = Array.from(strText);for (let itemChar of listCharArray) {let numCodePoint = itemChar.codePointAt(0);let strUnicodeNo = numCodePoint.toString(16).toUpperCase();let strFormattedUnicodeNo = strUnicodeNo.padStart(4, '0'); strUnicodeNo = 'U+' + strFormattedUnicodeNo; listOutputArray.push(strUnicodeNo);}let strOutput = listOutputArray.join('');return strOutput;} doGetCharUni();") as text
131  #log "\n" & strCommandText & "\n"
132  #コマンド実行
133  set strExecCommand to ("/usr/bin/osascript -l JavaScript -e \"" & strCommandText & "\"") as text
134  try
135    set strResponse to (do shell script strExecCommand) as string
136  on error
137    log "osascript でエラーしました"
138    return false
139  end try
140  
141  
142  set strOutPutText to (strOutPutText & strSetStr & " = " & strResponse & "\n") as text
143  
144end repeat
145log strOutPutText
146set strOutPutText to (strOutPutText & "\n\n") as text
147
148
149
150
151##############################
152#####ダイアログ
153##############################
154tell current application
155  set strName to name as text
156end tell
157####スクリプトメニューから実行したら
158if strName is "osascript" then
159  tell application "Finder"
160    activate
161  end tell
162else
163  tell current application
164    activate
165  end tell
166end if
167set strMes to "変換しましたコピーして使ってください"
168try
169  set recordResult to (display dialog strMes with title "戻り値です" default answer strOutPutText buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
170on error
171  return "エラーしました"
172end try
173if (gave up of recordResult) is true then
174  return "時間切れです"
175end if
176##############################
177#####自分自身を再実行
178##############################
179if button returned of recordResult is "再実行" then
180  tell application "Finder"
181    set aliasPathToMe to (path to me) as alias
182  end tell
183  run script aliasPathToMe with parameters "再実行"
184end if
185##############################
186#####値のコピー
187##############################
188if button returned of recordResult is "クリップボードにコピー" then
189  try
190    set strText to text returned of recordResult as text
191    ####ペーストボード宣言
192    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
193    set ocidText to (refMe's NSString's stringWithString:(strText))
194    appPasteboard's clearContents()
195    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
196  on error
197    tell application "Finder"
198      set the clipboard to strText as text
199    end tell
200  end try
201end if
202
203
204return 0
AppleScriptで生成しました

|

サロゲートペア・バリエーション付きの文字の判定


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# サロゲート判定
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use scripting additions
009property refMe : a reference to current application
010
011#入力文字列
012set strUniText to ("ABCDEFGあ😀☯️🙇‍♂️🇯🇵") as «class utf8»
013#文字数数えて
014set numCntTextItem to (count of every text item of strUniText) as integer
015#順番に処理
016repeat with itemNo from 1 to numCntTextItem by 1
017  #順番に文字列を取り出して
018  set strTextItem to (text item itemNo of strUniText) as «class utf8»
019  log strTextItem as «class utf8»
020  #NSStringにして
021  set strInputString to (refMe's NSString's stringWithString:(strTextItem))
022  #lengthを取得
023  set numCntLength to strInputString's |length| as integer
024  log numCntLength as integer
025  #1より大きい数字はエラーで停止
026  if numCntLength > 1 then
027    display alert "サロゲートペアやバリエーションの文字は処理できません"
028    return "サロゲートペアかバリエーションの文字は処理できません"
029  end if
030  
031end repeat
032
033
034
035
036
037log numCntLength as integer
AppleScriptで生成しました

|

UNICODEの名前(例:AならLATIN CAPITAL LETTER A)を取得するためのレコードを作成する



ダウンロード - getunicodename.zip




AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012
013######
014#読み込むデータのパス
015set aliasPathToMe to (path to me) as alias
016set strPathToMe to (POSIX path of aliasPathToMe) as text
017set ocidFilePathStr to refMe's NSString's stringWithString:(strPathToMe)
018set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
019set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
020set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
021set ocidSaveDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("Data") isDirectory:(true)
022set ocidReadFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("UnicodeData.txt") isDirectory:(false)
023
024######
025#NSdataに読み込み
026set ocidOption to (refMe's NSDataReadingMappedIfSafe)
027set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidReadFilePathURL) options:(ocidOption) |error| :(reference)
028if (item 2 of listResponse) = (missing value) then
029  set ocidReadData to (item 1 of listResponse)
030else if (item 2 of listResponse) ≠ (missing value) then
031  set strErrorNO to (item 2 of listResponse)'s code() as text
032  set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
033  refMe's NSLog("■:" & strErrorNO & strErrorMes)
034  return "エラーしました" & strErrorNO & strErrorMes
035end if
036
037######
038#テキストに変換
039set ocidReadString to refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding)
040
041######
042#改行でリストに
043set ocidStringArray to ocidReadString's componentsSeparatedByString:("\n")
044
045######
046#出力用のDICT
047set ocidUnicodeNameDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
048
049######
050#行毎処理
051repeat with itemLineString in ocidStringArray
052  set ocidLineArray to (itemLineString's componentsSeparatedByString:(";"))
053  #最後の改行のみの行まできたら終了
054  if (ocidLineArray's |count|()) = 1 then
055    exit repeat
056  end if
057  #行の最初を取得
058  set ocidUniID to ocidLineArray's firstObject()
059  #2番目を取得して
060  set ocidUniName to (ocidLineArray's objectAtIndex:(1))
061  #コントロール文字だったら
062  if (ocidUniName as text) is "<control>" then
063    #11番目を取得
064    set ocidUniName to (ocidLineArray's objectAtIndex:(10))
065  end if
066  #エラー確認用(ダウンロードが正しくできれば不要)
067  if ocidUniName = ("") then
068    return ocidUniID as text
069  end if
070  #DICTに値を格納
071  (ocidUnicodeNameDict's setValue:(ocidUniName) forKey:(ocidUniID))
072  
073end repeat
074
075######
076#保存先パス
077set ocidBaseSaveFilePathURL to ocidReadFilePathURL's URLByDeletingPathExtension()
078set ocidSaveFilePathURL to ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("plist")
079
080######
081#PLISTに変換
082set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0)
083set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidUnicodeNameDict) format:(ocidFormat) options:0  |error| :(reference)
084if (item 2 of listResponse) = (missing value) then
085  set ocidPlistData to (item 1 of listResponse)
086else if (item 2 of listResponse) ≠ (missing value) then
087  set strErrorNO to (item 2 of listResponse)'s code() as text
088  set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
089  refMe's NSLog("■:" & strErrorNO & strErrorMes)
090  return "エラーしました" & strErrorNO & strErrorMes
091end if
092
093
094######
095#保存
096set listDone to ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)
097if (item 1 of listDone) is true then
098  return "正常終了"
099else if (item 1 of listDone) is false then
100  log (item 2 of listDone)'s localizedDescription() as text
101  return "保存に失敗しました"
102end if
103
AppleScriptで生成しました

|

文字コード判定して開く (失敗作)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004(*
005無駄な労力であるのは理解している…笑
006com.apple.TextEncodingの値で開く前に
007NSencodeに置換できる物は置換して開く
008
009
010*)
011# com.cocolog-nifty.quicktimer.icefloe
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
013use AppleScript version "2.8"
014use framework "Foundation"
015use framework "CoreFoundation"
016use framework "AppKit"
017use scripting additions
018property refMe : a reference to current application
019
020
021set aliasFilePath to (choose file) as alias
022set strFilePath to (POSIX path of aliasFilePath) as text
023set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
024set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
025set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
026
027################
028#文字コード判定 xattr
029set strCommandText to ("/usr/bin/xattr -p com.apple.TextEncoding \"" & strFilePath & "\"") as text
030log "\n" & strCommandText & "\n"
031set strExecCommand to ("/bin/zsh -c '" & strCommandText & "'") as text
032try
033  set strResponse to (do shell script strExecCommand) as string
034  ####################
035  # サブルーチンへ渡す
036  set oicdRefCfEnc to doGetEnc(strResponse)
037  log oicdRefCfEnc
038  set ocidNsEnc to doCfEnc2NsEnc(oicdRefCfEnc)
039  log oicdRefCfEnc
040  if ocidNsEnc = (missing value) then
041    set ocidSetEnc to refMe's CFString's CFStringConvertEncodingToNSStringEncoding(oicdRefCfEnc)
042  else
043    set ocidSetEnc to ocidNsEnc
044  end if
045on error
046  #文字コード判定 file
047  set strCommandText to ("/usr/bin/file -I  \"" & strFilePath & "\"") as text
048  log "\n" & strCommandText & "\n"
049  set strExecCommand to ("/bin/zsh -c '" & strCommandText & "'") as text
050  try
051    set strResponse to (do shell script strExecCommand) as string
052    ####
053    if strResponse contains "iso-8859" then
054      #Latin1と見切り
055      set ocidSetEnc to refMe's NSISOLatin1StringEncoding
056      
057    else if strResponse contains "utf-8" then
058      set ocidSetEnc to refMe's NSUTF8StringEncoding
059      
060    else if strResponse contains "utf-16" then
061      set ocidSetEnc to refMe's NSUTF16StringEncoding
062      
063    else if strResponse contains "utf-32" then
064      set ocidSetEnc to refMe's NSUTF32BigEndianStringEncoding
065      
066    else if strResponse contains "ascii" then
067      set ocidSetEnc to refMe's NSASCIIStringEncoding
068      
069    else if strResponse contains "8bit" then
070      #S-JISと見切り
071      set ocidSetEnc to refMe's NSShiftJISStringEncoding
072      
073    else if strResponse contains "binary" then
074      #UTF16BEと見切り
075      set ocidSetEnc to refMe's NSUTF16BigEndianStringEncoding
076    else
077      return "文字コードの判定ができませんでした"
078    end if
079    
080  on error
081    return "file でエラーしました"
082  end try
083  
084end try
085################
086#
087set listResponse to refMe's NSString's stringWithContentsOfURL:(ocidFilePathURL) encoding:(ocidSetEnc) |error| :(reference)
088
089if (item 2 of listResponse) = (missing value) then
090  log "正常処理"
091  set ocidReadString to (item 1 of listResponse)
092else if (item 2 of listResponse) ≠ (missing value) then
093  set strErrorNO to (item 2 of listResponse)'s code() as text
094  set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
095  refMe's NSLog("■:" & strErrorNO & strErrorMes)
096  return "エラーしました" & strErrorNO & strErrorMes
097end if
098
099log ocidReadString as string
100
101
102################
103# Cf encode to Ns
104to doCfEnc2NsEnc(argText)
105  set strCfNo to argText as text
106  set recordNsEncode to {|1536|:current application's NSASCIIStringEncoding, |2818|:current application's NSNEXTSTEPStringEncoding, |2336|:current application's NSJapaneseEUCStringEncoding, |134217984|:current application's NSUTF8StringEncoding, |528|:current application's NSISOLatin1StringEncoding, |33|:current application's NSSymbolStringEncoding, |3071|:current application's NSNonLossyASCIIStringEncoding, |8|:current application's NSShiftJISStringEncoding, |1576|:current application's NSShiftJISStringEncoding, |1577|:current application's NSShiftJISStringEncoding, |514|:current application's NSISOLatin2StringEncoding, |1282|:current application's NSWindowsCP1251StringEncoding, |1280|:current application's NSWindowsCP1252StringEncoding, |1283|:current application's NSWindowsCP1253StringEncoding, |1284|:current application's NSWindowsCP1254StringEncoding, |1281|:current application's NSWindowsCP1250StringEncoding, |2080|:current application's NSISO2022JPStringEncoding, |2081|:current application's NSISO2022JPStringEncoding, |2082|:current application's NSISO2022JPStringEncoding, |2083|:current application's NSISO2022JPStringEncoding, |0|:current application's NSMacOSRomanStringEncoding, |256|:current application's NSUTF16StringEncoding, |268435712|:current application's NSUTF16BigEndianStringEncoding, |335544576|:current application's NSUTF16LittleEndianStringEncoding, |201326848|:current application's NSUTF32StringEncoding, |402653440|:current application's NSUTF32BigEndianStringEncoding, |469762304|:current application's NSUTF32LittleEndianStringEncoding, |65536|:current application's NSProprietaryStringEncoding} as record
107  
108  ##
109  set ocidNsEncDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
110  ocidNsEncDict's setDictionary:(recordNsEncode)
111  ##
112  set ocidRefNsEncode to ocidNsEncDict's objectForKey:(strCfNo)
113  
114  return ocidRefNsEncode
115  
116end doCfEnc2NsEnc
117
118################
119# Cf encode
120to doGetEnc(argText)
121  
122  set recordCfEncode to {|134217984|:current application's kCFStringEncodingUTF8, |256|:current application's kCFStringEncodingUTF16, |268435712|:current application's kCFStringEncodingUTF16BE, |335544576|:current application's kCFStringEncodingUTF16LE, |201326848|:current application's kCFStringEncodingUTF32, |402653440|:current application's kCFStringEncodingUTF32BE, |469762304|:current application's kCFStringEncodingUTF32LE, |1280|:current application's kCFStringEncodingWindowsLatin1, |513|:current application's kCFStringEncodingISOLatin1, |2817|:current application's kCFStringEncodingNextStepLatin, |1536|:current application's kCFStringEncodingASCII, |3071|:current application's kCFStringEncodingNonLossyASCII, |0|:current application's kCFStringEncodingMacRoman, |1|:current application's kCFStringEncodingMacJapanese, |2|:current application's kCFStringEncodingMacChineseTrad, |3|:current application's kCFStringEncodingMacKorean, |4|:current application's kCFStringEncodingMacArabic, |5|:current application's kCFStringEncodingMacHebrew, |6|:current application's kCFStringEncodingMacGreek, |7|:current application's kCFStringEncodingMacCyrillic, |9|:current application's kCFStringEncodingMacDevanagari, |10|:current application's kCFStringEncodingMacGurmukhi, |11|:current application's kCFStringEncodingMacGujarati, |12|:current application's kCFStringEncodingMacOriya, |13|:current application's kCFStringEncodingMacBengali, |14|:current application's kCFStringEncodingMacTamil, |15|:current application's kCFStringEncodingMacTelugu, |16|:current application's kCFStringEncodingMacKannada, |17|:current application's kCFStringEncodingMacMalayalam, |18|:current application's kCFStringEncodingMacSinhalese, |19|:current application's kCFStringEncodingMacBurmese, |20|:current application's kCFStringEncodingMacKhmer, |21|:current application's kCFStringEncodingMacThai, |22|:current application's kCFStringEncodingMacLaotian, |23|:current application's kCFStringEncodingMacGeorgian, |24|:current application's kCFStringEncodingMacArmenian, |25|:current application's kCFStringEncodingMacChineseSimp, |26|:current application's kCFStringEncodingMacTibetan, |27|:current application's kCFStringEncodingMacMongolian, |28|:current application's kCFStringEncodingMacEthiopic, |29|:current application's kCFStringEncodingMacCentralEurRoman, |30|:current application's kCFStringEncodingMacVietnamese, |31|:current application's kCFStringEncodingMacExtArabic, |33|:current application's kCFStringEncodingMacSymbol, |34|:current application's kCFStringEncodingMacDingbats, |35|:current application's kCFStringEncodingMacTurkish, |36|:current application's kCFStringEncodingMacCroatian, |37|:current application's kCFStringEncodingMacIcelandic, |38|:current application's kCFStringEncodingMacRomanian, |39|:current application's kCFStringEncodingMacCeltic, |40|:current application's kCFStringEncodingMacGaelic, |140|:current application's kCFStringEncodingMacFarsi, |152|:current application's kCFStringEncodingMacUkrainian, |236|:current application's kCFStringEncodingMacInuit, |252|:current application's kCFStringEncodingMacVT100, |255|:current application's kCFStringEncodingMacHFS, |514|:current application's kCFStringEncodingISOLatin2, |515|:current application's kCFStringEncodingISOLatin3, |516|:current application's kCFStringEncodingISOLatin4, |517|:current application's kCFStringEncodingISOLatinCyrillic, |518|:current application's kCFStringEncodingISOLatinArabic, |519|:current application's kCFStringEncodingISOLatinGreek, |520|:current application's kCFStringEncodingISOLatinHebrew, |521|:current application's kCFStringEncodingISOLatin5, |522|:current application's kCFStringEncodingISOLatin6, |523|:current application's kCFStringEncodingISOLatinThai, |525|:current application's kCFStringEncodingISOLatin7, |526|:current application's kCFStringEncodingISOLatin8, |527|:current application's kCFStringEncodingISOLatin9, |528|:current application's kCFStringEncodingISOLatin10, |1024|:current application's kCFStringEncodingDOSLatinUS, |1029|:current application's kCFStringEncodingDOSGreek, |1030|:current application's kCFStringEncodingDOSBalticRim, |1040|:current application's kCFStringEncodingDOSLatin1, |1041|:current application's kCFStringEncodingDOSGreek1, |1042|:current application's kCFStringEncodingDOSLatin2, |1043|:current application's kCFStringEncodingDOSCyrillic, |1044|:current application's kCFStringEncodingDOSTurkish, |1045|:current application's kCFStringEncodingDOSPortuguese, |1046|:current application's kCFStringEncodingDOSIcelandic, |1047|:current application's kCFStringEncodingDOSHebrew, |1048|:current application's kCFStringEncodingDOSCanadianFrench, |1049|:current application's kCFStringEncodingDOSArabic, |1050|:current application's kCFStringEncodingDOSNordic, |1051|:current application's kCFStringEncodingDOSRussian, |1052|:current application's kCFStringEncodingDOSGreek2, |1053|:current application's kCFStringEncodingDOSThai, |1056|:current application's kCFStringEncodingDOSJapanese, |1057|:current application's kCFStringEncodingDOSChineseSimplif, |1058|:current application's kCFStringEncodingDOSKorean, |1059|:current application's kCFStringEncodingDOSChineseTrad, |1281|:current application's kCFStringEncodingWindowsLatin2, |1282|:current application's kCFStringEncodingWindowsCyrillic, |1283|:current application's kCFStringEncodingWindowsGreek, |1284|:current application's kCFStringEncodingWindowsLatin5, |1285|:current application's kCFStringEncodingWindowsHebrew, |1286|:current application's kCFStringEncodingWindowsArabic, |1287|:current application's kCFStringEncodingWindowsBalticRim, |1288|:current application's kCFStringEncodingWindowsVietnamese, |1296|:current application's kCFStringEncodingWindowsKoreanJohab, |1537|:current application's kCFStringEncodingANSEL, |1568|:current application's kCFStringEncodingJIS_X0201_76, |1569|:current application's kCFStringEncodingJIS_X0208_83, |1570|:current application's kCFStringEncodingJIS_X0208_90, |1571|:current application's kCFStringEncodingJIS_X0212_90, |1572|:current application's kCFStringEncodingJIS_C6226_78, |1576|:current application's kCFStringEncodingShiftJIS_X0213, |1577|:current application's kCFStringEncodingShiftJIS_X0213_MenKuTen, |1584|:current application's kCFStringEncodingGB_2312_80, |1585|:current application's kCFStringEncodingGBK_95, |1586|:current application's kCFStringEncodingGB_18030_2000, |1600|:current application's kCFStringEncodingKSC_5601_87, |1601|:current application's kCFStringEncodingKSC_5601_92_Johab, |1617|:current application's kCFStringEncodingCNS_11643_92_P1, |1618|:current application's kCFStringEncodingCNS_11643_92_P2, |1619|:current application's kCFStringEncodingCNS_11643_92_P3, |2080|:current application's kCFStringEncodingISO_2022_JP, |2081|:current application's kCFStringEncodingISO_2022_JP_2, |2082|:current application's kCFStringEncodingISO_2022_JP_1, |2083|:current application's kCFStringEncodingISO_2022_JP_3, |2096|:current application's kCFStringEncodingISO_2022_CN, |2097|:current application's kCFStringEncodingISO_2022_CN_EXT, |2112|:current application's kCFStringEncodingISO_2022_KR, |2336|:current application's kCFStringEncodingEUC_JP, |2352|:current application's kCFStringEncodingEUC_CN, |2353|:current application's kCFStringEncodingEUC_TW, |2368|:current application's kCFStringEncodingEUC_KR, |2561|:current application's kCFStringEncodingShiftJIS, |2562|:current application's kCFStringEncodingKOI8_R, |2563|:current application's kCFStringEncodingBig5, |2564|:current application's kCFStringEncodingMacRomanLatin1, |2565|:current application's kCFStringEncodingHZ_GB_2312, |2566|:current application's kCFStringEncodingBig5_HKSCS_1999, |2567|:current application's kCFStringEncodingVISCII, |2568|:current application's kCFStringEncodingKOI8_U, |2569|:current application's kCFStringEncodingBig5_E, |2818|:current application's kCFStringEncodingNextStepJapanese, |3073|:current application's kCFStringEncodingEBCDIC_US, |3074|:current application's kCFStringEncodingEBCDIC_CP037, |67109120|:current application's kCFStringEncodingUTF7, |2576|:current application's kCFStringEncodingUTF7_IMAP} as record
123  
124  ##
125  set ocidCfEncDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
126  ocidCfEncDict's setDictionary:(recordCfEncode)
127  
128  ##
129  set strAttrResponse to argText as text
130  set ocidAttrResponse to refMe's NSString's stringWithString:(strAttrResponse)
131  set ocidAttrResArray to ocidAttrResponse's componentsSeparatedByString:(";")
132  set strCfNo to ocidAttrResArray's lastObject()
133  
134  set ocidRefCfEncode to ocidCfEncDict's objectForKey:(strCfNo)
135  
136  return ocidRefCfEncode
137end doGetEnc
AppleScriptで生成しました

|

[com.apple.TextEncoding] External String Encodings CFStringのリスト

エンコード番号のリスト
134217984kCFStringEncodingUTF8
256kCFStringEncodingUTF16
268435712kCFStringEncodingUTF16BE
335544576kCFStringEncodingUTF16LE
201326848kCFStringEncodingUTF32
402653440kCFStringEncodingUTF32BE
469762304kCFStringEncodingUTF32LE
1280kCFStringEncodingWindowsLatin1
513kCFStringEncodingISOLatin1
2817kCFStringEncodingNextStepLatin
1536kCFStringEncodingASCII
256kCFStringEncodingUnicode
3071kCFStringEncodingNonLossyASCII
0kCFStringEncodingMacRoman
1kCFStringEncodingMacJapanese
2kCFStringEncodingMacChineseTrad
3kCFStringEncodingMacKorean
4kCFStringEncodingMacArabic
5kCFStringEncodingMacHebrew
6kCFStringEncodingMacGreek
7kCFStringEncodingMacCyrillic
9kCFStringEncodingMacDevanagari
10kCFStringEncodingMacGurmukhi
11kCFStringEncodingMacGujarati
12kCFStringEncodingMacOriya
13kCFStringEncodingMacBengali
14kCFStringEncodingMacTamil
15kCFStringEncodingMacTelugu
16kCFStringEncodingMacKannada
17kCFStringEncodingMacMalayalam
18kCFStringEncodingMacSinhalese
19kCFStringEncodingMacBurmese
20kCFStringEncodingMacKhmer
21kCFStringEncodingMacThai
22kCFStringEncodingMacLaotian
23kCFStringEncodingMacGeorgian
24kCFStringEncodingMacArmenian
25kCFStringEncodingMacChineseSimp
26kCFStringEncodingMacTibetan
27kCFStringEncodingMacMongolian
28kCFStringEncodingMacEthiopic
29kCFStringEncodingMacCentralEurRoman
30kCFStringEncodingMacVietnamese
31kCFStringEncodingMacExtArabic
33kCFStringEncodingMacSymbol
34kCFStringEncodingMacDingbats
35kCFStringEncodingMacTurkish
36kCFStringEncodingMacCroatian
37kCFStringEncodingMacIcelandic
38kCFStringEncodingMacRomanian
39kCFStringEncodingMacCeltic
40kCFStringEncodingMacGaelic
140kCFStringEncodingMacFarsi
152kCFStringEncodingMacUkrainian
236kCFStringEncodingMacInuit
252kCFStringEncodingMacVT100
255kCFStringEncodingMacHFS
514kCFStringEncodingISOLatin2
515kCFStringEncodingISOLatin3
516kCFStringEncodingISOLatin4
517kCFStringEncodingISOLatinCyrillic
518kCFStringEncodingISOLatinArabic
519kCFStringEncodingISOLatinGreek
520kCFStringEncodingISOLatinHebrew
521kCFStringEncodingISOLatin5
522kCFStringEncodingISOLatin6
523kCFStringEncodingISOLatinThai
525kCFStringEncodingISOLatin7
526kCFStringEncodingISOLatin8
527kCFStringEncodingISOLatin9
528kCFStringEncodingISOLatin10
1024kCFStringEncodingDOSLatinUS
1029kCFStringEncodingDOSGreek
1030kCFStringEncodingDOSBalticRim
1040kCFStringEncodingDOSLatin1
1041kCFStringEncodingDOSGreek1
1042kCFStringEncodingDOSLatin2
1043kCFStringEncodingDOSCyrillic
1044kCFStringEncodingDOSTurkish
1045kCFStringEncodingDOSPortuguese
1046kCFStringEncodingDOSIcelandic
1047kCFStringEncodingDOSHebrew
1048kCFStringEncodingDOSCanadianFrench
1049kCFStringEncodingDOSArabic
1050kCFStringEncodingDOSNordic
1051kCFStringEncodingDOSRussian
1052kCFStringEncodingDOSGreek2
1053kCFStringEncodingDOSThai
1056kCFStringEncodingDOSJapanese
1057kCFStringEncodingDOSChineseSimplif
1058kCFStringEncodingDOSKorean
1059kCFStringEncodingDOSChineseTrad
1281kCFStringEncodingWindowsLatin2
1282kCFStringEncodingWindowsCyrillic
1283kCFStringEncodingWindowsGreek
1284kCFStringEncodingWindowsLatin5
1285kCFStringEncodingWindowsHebrew
1286kCFStringEncodingWindowsArabic
1287kCFStringEncodingWindowsBalticRim
1288kCFStringEncodingWindowsVietnamese
1296kCFStringEncodingWindowsKoreanJohab
1537kCFStringEncodingANSEL
1568kCFStringEncodingJIS_X0201_76
1569kCFStringEncodingJIS_X0208_83
1570kCFStringEncodingJIS_X0208_90
1571kCFStringEncodingJIS_X0212_90
1572kCFStringEncodingJIS_C6226_78
1576kCFStringEncodingShiftJIS_X0213
1577kCFStringEncodingShiftJIS_X0213_MenKuTen
1584kCFStringEncodingGB_2312_80
1585kCFStringEncodingGBK_95
1586kCFStringEncodingGB_18030_2000
1600kCFStringEncodingKSC_5601_87
1601kCFStringEncodingKSC_5601_92_Johab
1617kCFStringEncodingCNS_11643_92_P1
1618kCFStringEncodingCNS_11643_92_P2
1619kCFStringEncodingCNS_11643_92_P3
2080kCFStringEncodingISO_2022_JP
2081kCFStringEncodingISO_2022_JP_2
2082kCFStringEncodingISO_2022_JP_1
2083kCFStringEncodingISO_2022_JP_3
2096kCFStringEncodingISO_2022_CN
2097kCFStringEncodingISO_2022_CN_EXT
2112kCFStringEncodingISO_2022_KR
2336kCFStringEncodingEUC_JP
2352kCFStringEncodingEUC_CN
2353kCFStringEncodingEUC_TW
2368kCFStringEncodingEUC_KR
2561kCFStringEncodingShiftJIS
2562kCFStringEncodingKOI8_R
2563kCFStringEncodingBig5
2564kCFStringEncodingMacRomanLatin1
2565kCFStringEncodingHZ_GB_2312
2566kCFStringEncodingBig5_HKSCS_1999
2567kCFStringEncodingVISCII
2568kCFStringEncodingKOI8_U
2569kCFStringEncodingBig5_E
2818kCFStringEncodingNextStepJapanese
3073kCFStringEncodingEBCDIC_US
3074kCFStringEncodingEBCDIC_CP037
67109120kCFStringEncodingUTF7
2576kCFStringEncodingUTF7_IMAP
1576kCFStringEncodingShiftJIS_X0213_00
  
External String Encodings

|

nkfインストール(macOS15.0.1)

1:Xcodeを利用可能にしておく
2:テキストエディタを用意
3:zipダウンロード
4:zip解凍
5:Makefileを編集
6:ターミナルからmake

あっさりエラーもなるmake出来た

1:Xcodeを利用可能にしておく
AppStoreLINK
デベロッパーサイトLINK
又は
CommandLineToolsをインストールしてもよい

サンプルコード

サンプルソース(参考)
行番号ソース
001/usr/bin/xcode-select --install
AppleScriptで生成しました

2:テキストエディタを用意
Os標準のテキストエディタでも良いが
CotEditor LINK
Visual Studio Code LINK

あると良いでしょう


3:zipダウンロード
https://github.com/nurse/nkf LINK 20241023032818_1368x892

4:zip解凍
普通にWクリックで解凍でOK
20241023032830_740x286
20241023032835_660x268


5:Makefileを編集
解凍したフォルダの中にあるMakefileを
お使いのテキストエディタ開いて
20241023032924_1344x464

PYTHON2をコメントアウトしたら上書き保存します
20241023032938_868x544


6:ターミナルからmake
20241023032854_2880x1800_2024110121130120241023032857_2880x18003 20241023033007_1288x9902 この後 ターミナルからsudo make insetallしても良いが 私は/usr/localを使わない派なので…ここまで

|

非デザイナー原稿チェック用 原稿文字にmini外(当用漢字・人名漢字外)の漢字があるか?チェック



ダウンロード - chkminchraset.zip




AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004(*
005人名漢字
006常用漢字
007JIS0212 補助漢字は適応していない
008JIS0208 非漢字部分
009JIS0201 欧文
010選択したテキストにminiの範囲に無い文字をチェックする
011*)
012#com.cocolog-nifty.quicktimer.icefloe
013----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
014use AppleScript version "2.8"
015use framework "Foundation"
016use framework "UniformTypeIdentifiers"
017use framework "AppKit"
018use scripting additions
019property refMe : a reference to current application
020set appFileManager to refMe's NSFileManager's defaultManager()
021
022##############################
023#####Unicode番号リスト
024##############################
025set aliasPathToMe to (path to me) as alias
026set strPathToMe to (POSIX path of aliasPathToMe) as text
027set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe)
028set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath()
029set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(true)
030set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent()
031#
032set ocidDataFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("Data/MiniData.txt") isDirectory:(false)
033##NSDATAに読み込む
034set ocidOption to (refMe's NSDataReadingMappedIfSafe)
035set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidDataFilePathURL) options:(ocidOption) |error| :(reference)
036set ocidReadData to (item 1 of listResponse)
037#NSSTRING
038set ocidDataString to refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding)
039#NSArray
040set ocidDataArray to ocidDataString's componentsSeparatedByString:("\n")
041
042##############################
043###ダイアログ
044set strName to (name of current application) as text
045if strName is "osascript" then
046  tell application "Finder" to activate
047else
048  tell current application to activate
049end if
050set appFileManager to refMe's NSFileManager's defaultManager()
051set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
052set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
053set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
054set listUTI to {"public.text"}
055set strMes to ("ファイルを選んでください") as text
056set strPrompt to ("ファイルを選んでください") as text
057try
058  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
059on error
060  log "エラーしました"
061  return "エラーしました"
062end try
063#
064set strFilePath to (POSIX path of aliasFilePath) as text
065set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
066set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
067set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(strFilePath) isDirectory:false)
068##NSDATAに読み込む
069set ocidOption to (refMe's NSDataReadingMappedIfSafe)
070set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)
071set ocidStringData to (item 1 of listResponse)
072#NSSTRING
073set ocidReadString to refMe's NSString's alloc()'s initWithData:(ocidStringData) encoding:(refMe's NSUTF8StringEncoding)
074#改行をUNIXに強制
075set ocidReadString to (ocidReadString's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
076set ocidReadString to (ocidReadString's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n"))
077#NSarrayに
078set ocidLineStringArray to ocidReadString's componentsSeparatedByString:("\n")
079
080##############################
081#処理部
082##############################
083#出力用のテキスト
084set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
085#行順に処理
086repeat with itemLineString in ocidLineStringArray
087  #テキストにして
088  set strLineString to itemLineString as text
089  #文字数をカウント
090  set listLineString to (every character of strLineString) as list
091  repeat with itemLineText in listLineString
092    set strLineText to itemLineText as text
093    set ocidItemStr to (refMe's NSString's stringWithString:(strLineText))
094    #文字数数えて
095    set numCntString to (ocidItemStr's |length|()) as integer
096    #2文字ある場合はサロゲートペア
097    if numCntString = 2 then
098      #サロゲート文字の場合はゲタで囲む
099      (ocidOutPutstring's appendString:("〓"))
100      (ocidOutPutstring's appendString:(ocidItemStr))
101      (ocidOutPutstring's appendString:("〓"))
102    else
103      #問題ない場合はそのままの文字のみ
104      set ocidUniNo to doConvertUniNO(ocidItemStr)
105      log ocidUniNo as text
106      set boolContain to (ocidDataArray's containsObject:(ocidUniNo as text))
107      if (boolContain as boolean) is true then
108        (ocidOutPutstring's appendString:(ocidItemStr))
109      else
110        #リストに無い文字はさんかくで囲む
111        (ocidOutPutstring's appendString:("▶︎"))
112        (ocidOutPutstring's appendString:(ocidItemStr))
113        (ocidOutPutstring's appendString:("◀︎"))
114      end if
115      
116    end if
117    
118  end repeat
119  #行毎の改行
120  (ocidOutPutstring's appendString:("\n"))
121end repeat
122
123##############################
124#保存先パス
125set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
126set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("チェック済.txt")
127#保存
128set listDone to ocidOutPutstring's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
129return 0
130
131##############################
132#####変換のサブルーチン
133##############################
134to doConvertUniNO(argCharText)
135  #テキストで確定させて
136  set strCharText to argCharText as text
137  #NSStringに
138  set ocidReadText to refMe's NSString's stringWithString:(strCharText)
139  (* HTML用
140  set ocidEncodedText to (ocidReadText's stringByApplyingTransform:(refMe's NSStringTransformToXMLHex) |reverse|:false)
141  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:(";") withString:(""))
142  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:("&#x") withString:("U+"))
143  *)
144  #ユニコードエスケープ形式で出力
145  set ocidEncodedText to (ocidReadText's stringByApplyingTransform:("Any-Hex/Java") |reverse|:false)
146  #不要な文字を置換して
147  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:("\\u") withString:("U+"))
148  #戻す
149  return ocidEncodedText
150end doConvertUniNO
AppleScriptで生成しました

|

テキストのユニコード番号の取得 (サロゲートペア対応)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004(*
005NSStringPboardTypeが非推奨になったので
006NSPasteboardTypeStringに変更
007*)
008#com.cocolog-nifty.quicktimer.icefloe
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "UniformTypeIdentifiers"
013use framework "AppKit"
014use scripting additions
015property refMe : a reference to current application
016property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
017
018
019set strMes to ("文字列を入力") as text
020
021########################
022## クリップボードの中身取り出し
023########################
024###初期化
025set appPasteboard to refMe's NSPasteboard's generalPasteboard()
026##格納されているタイプをリストにして
027set ocidPastBoardTypeArray to appPasteboard's types
028###テキストがあれば
029set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text")
030if (boolContain as boolean) is true then
031  ###値を格納する
032  tell application "Finder"
033    set strReadString to (the clipboard as text) as text
034  end tell
035else
036  ###UTF8が無いなら
037  ##テキスト形式があるか?確認して
038  set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
039  ##テキスト形式があるなら
040  if (boolContain as boolean) is true then
041    set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
042    ocidTypeClassArray's addObject:(refMe's NSString)
043    set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)
044    set strReadString to ocidReadString as text
045  else
046    log "テキストなし"
047    set strReadString to strMes as text
048  end if
049end if
050##############################
051#####ダイアログ
052##############################
053###ダイアログを前面に出す
054set strName to (name of current application) as text
055if strName is "osascript" then
056  tell application "Finder" to activate
057else
058  tell current application to activate
059end if
060set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
061try
062  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
063on error
064  log "エラーしました"
065  return
066end try
067if "OK" is equal to (button returned of recordResult) then
068  set strReturnedText to (text returned of recordResult) as text
069else if (gave up of recordResult) is true then
070  return "時間切れです"
071else
072  return "キャンセル"
073end if
074##############################
075#####戻り値整形
076##############################
077set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
078###タブと改行を除去しておく
079set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
080ocidTextM's appendString:(ocidResponseText)
081###除去する文字列リスト
082set listRemoveChar to {"\n", "\r", "\t"} as list
083##置換
084repeat with itemChar in listRemoveChar
085  set strPattern to itemChar as text
086  set strTemplate to ("") as text
087  set ocidOption to (refMe's NSRegularExpressionCaseInsensitive)
088  set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference))
089  if (item 2 of listResponse) ≠ (missing value) then
090    log (item 2 of listResponse)'s localizedDescription() as text
091    return "正規表現パターンに誤りがあります"
092  else
093    set ocidRegex to (item 1 of listResponse)
094  end if
095  set numLength to ocidResponseText's |length|()
096  set ocidRange to refMe's NSRange's NSMakeRange(0, numLength)
097  set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate))
098end repeat
099
100##############################
101#####計算部
102##############################
103set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
104set ocidEncStringArray to ocidResponseText's componentsSeparatedByString:("")
105#
106set strResponseText to ocidResponseText as text
107set listResponseChar to (every character of strResponseText) as list
108repeat with itemText in listResponseChar
109  set ocidItemStr to (refMe's NSString's stringWithString:(itemText))
110  #文字数数えて
111  set numCntString to ocidItemStr's |length|()
112  if numCntString = 2 then
113    #"文字列にサロゲートペア文字が含まれています"
114    repeat with itemIntNo from 0 to (numCntString - 1) by 1
115      set ocidGetRange to refMe's NSRange's NSMakeRange(itemIntNo, 1)
116      set ocidChkChar to (ocidItemStr's substringWithRange:(ocidGetRange))
117      set ocidUniNo to doConvertUniNO(ocidChkChar)
118      (ocidOutPutstring's appendString:("〓"))
119      (ocidOutPutstring's appendString:(ocidUniNo))
120      
121    end repeat
122    (ocidOutPutstring's appendString:("〓"))
123    (ocidOutPutstring's appendString:(" "))
124    
125  else
126    set ocidUniNo to doConvertUniNO(ocidItemStr)
127    (ocidOutPutstring's appendString:(ocidUniNo))
128    (ocidOutPutstring's appendString:(" "))
129    
130  end if
131  
132end repeat
133
134
135##############################
136#####ダイアログ
137##############################
138tell current application
139  set strName to name as text
140end tell
141####スクリプトメニューから実行したら
142if strName is "osascript" then
143  tell application "Finder"
144    activate
145  end tell
146else
147  tell current application
148    activate
149  end tell
150end if
151set strMes to "ユニコード番号の戻り値です\n〓を含む文字はサロゲートペアの文字です"
152set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Sync.icns") as alias
153try
154  set recordResult to (display dialog strMes with title "戻り値です" default answer (ocidOutPutstring as text) buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
155on error
156  return "エラーしました"
157end try
158if (gave up of recordResult) is true then
159  return "時間切れです"
160end if
161##############################
162#####自分自身を再実行
163##############################
164if button returned of recordResult is "再実行" then
165  tell application "Finder"
166    set aliasPathToMe to (path to me) as alias
167  end tell
168  run script aliasPathToMe with parameters "再実行"
169end if
170##############################
171#####値のコピー
172##############################
173if button returned of recordResult is "クリップボードにコピー" then
174  try
175    set strText to text returned of recordResult as text
176    ####ペーストボード宣言
177    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
178    set ocidText to (refMe's NSString's stringWithString:(strText))
179    appPasteboard's clearContents()
180    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
181  on error
182    tell application "Finder"
183      set the clipboard to strText as text
184    end tell
185  end try
186end if
187
188
189return 0
190
191##############################
192#####変換のサブルーチン
193##############################
194to doConvertUniNO(argCharText)
195  set strCharText to argCharText as text
196  set ocidReadText to refMe's NSString's stringWithString:(strCharText)
197  (* HTML用
198  set ocidEncodedText to (ocidReadText's stringByApplyingTransform:(refMe's NSStringTransformToXMLHex) |reverse|:false)
199  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:(";") withString:(""))
200  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:("&#x") withString:("U+"))
201  *)
202  set ocidEncodedText to (ocidReadText's stringByApplyingTransform:("Any-Hex/Java") |reverse|:false)
203  set ocidEncodedText to (ocidEncodedText's stringByReplacingOccurrencesOfString:("\\u") withString:("U+"))
204  return ocidEncodedText
205end doConvertUniNO
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom