[Json]YahooAPIを使ってテキストにrubyタグを付与
JSONでは『これ』が必要だったんです…汗
もちろん、意図しないルビになる事もありますので(不具合ではない=仕様よね)
直しは必要だけど、ずいぶん時間も手間も減るので助かる…
ダウンロード - rubye382bfe382afe38299e4bb98e4b88e.zip
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# 実行にIDが必要
#https://e.developer.yahoo.co.jp/dashboard/
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use framework "Foundation"
use AppleScript version "2.4"
use scripting additions
property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSJSONSerialization : a reference to objMe's NSJSONSerialization
############################################
#####実行にIDが必要 無料で取得できます
## <https://e.developer.yahoo.co.jp/dashboard/>
set strApiAppId to "ここにID" as text
############################################
##Codeのコピーは失敗する確率高いので
##自力でコピーしておく事にしてここは使わない
(*
tell application id "com.microsoft.VSCode"
activate
end tell
tell application "System Events"
keystroke "c" using command down
end tell
tell application id "com.microsoft.VSCode"
activate
tell application "System Events"
keystroke "C" using command down
end tell
end tell
*)
###テキスト取得
set strText to (the clipboard as text)
###タブ除去
set strText to doReplace(strText, "\t", "") as text
####JSON id用の連番を日付連番に
set strCommandText to ("/bin/date +%s") as text
set numSecNo to (do shell script strCommandText) as text
####このファイルのパス
set aliasPathToMe to path to me as alias
####このファイルへのUNIXパス
set strPathToMe to POSIX path of aliasPathToMe as text
####このファイルのあるディレクトリ
tell application "Finder"
set aliasPathToDir to container of aliasPathToMe as alias
set strPathToDir to POSIX path of aliasPathToDir as text
end tell
####JSON取得用のpyファイル
set strPyPath to ("" & strPathToDir & "bin/kanji2kana4json.py") as text
####コマンド整形
set strCommandText to ("/usr/bin/python3 \"" & strPyPath & "\" \"" & strText & "\" \"" & numSecNo & "\" \"" & strApiAppId & "\"") as text
set strResJson to (do shell script strCommandText) as text
####戻り値をレコードに
set strResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:strResJson)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value)) as record
####データ取得確認
set numErrorCnt to count of |result| of strResJson
if numErrorCnt = 0 then
log "JSONデータの取得に失敗しました"
return
end if
####戻り値が『何』ワードに区切られたか
set numWordCnt to count of |word| of |result| of strResJson
###各種初期化
set numChkWordCnt to 0
set strFuriganaStrings to "" as text
##########ワード数毎繰り返し
repeat numWordCnt times
###カウントアップ
set numChkWordCnt to numChkWordCnt + 1
####ワード×番目のアイテム
set numCntCnkFuri to count of item numChkWordCnt of |word| of |result| of strResJson
####×番目のアイテムの数が1なら『漢字なし』なので
if numCntCnkFuri > 1 then
###漢字ありの場合 ワード内の項目を数えて
set numChkSubwordCnt to count of (item numChkWordCnt of |word| of |result| of strResJson)
####4項目ある場合はサブワードあり
if numChkSubwordCnt > 3 then
######################
###サブワードの数を数えて
set numCntSubword to count of (subword of item numChkWordCnt of |word| of |result| of strResJson)
set numCntSubwordPre to 0
###サブワード分繰り返し
repeat numCntSubword times
set numCntSubwordPre to numCntSubwordPre + 1
###順番にふりがなを取得
set strWordFurigana to furigana of item numCntSubwordPre of subword of item numChkWordCnt of |word| of |result| of strResJson as text
###順番に元の字を取得
set strWordSurface to surface of item numCntSubwordPre of subword of item numChkWordCnt of |word| of |result| of strResJson as text
###ふりがな=元の字の場合はスペースで埋める
if strWordFurigana is strWordSurface then
set strFuriganaStrings to strFuriganaStrings & strWordSurface as text
else
###
set strFruriganaTag to "<ruby>" & strWordSurface & "<rp>(</rp><rt>" & strWordFurigana & "</rt><rp>)</rp></ruby>" as text
###
set strFuriganaStrings to strFuriganaStrings & strFruriganaTag as text
end if
end repeat
######################
else
####オリジナルのテキスト
set strWordSurface to surface of item numChkWordCnt of |word| of |result| of strResJson
set strWordFurigana to furigana of item numChkWordCnt of |word| of |result| of strResJson
set strFruriganaTag to "<ruby>" & strWordSurface & "<rp>(</rp><rt>" & strWordFurigana & "</rt><rp>)</rp></ruby>" as text
###
set strFuriganaStrings to strFuriganaStrings & strFruriganaTag as text
end if
else
set strWordSurface to surface of item numChkWordCnt of |word| of |result| of strResJson
if strWordSurface is "\n" then
set strFruriganaTag to "<br />" as text
###
set strFuriganaStrings to strFuriganaStrings & strFruriganaTag as text
else if strWordSurface is "\r" then
set strFruriganaTag to "<br />" as text
###
set strFuriganaStrings to strFuriganaStrings & strFruriganaTag as text
else
set strFuriganaStrings to strFuriganaStrings & strWordSurface as text
end if
end if
end repeat
set the clipboard to (strFuriganaStrings) as text
tell application id "com.microsoft.VSCode"
activate
end tell
tell application "System Events"
keystroke "v" using command down
end tell
##↑シンプルにこの方式が成功率高い
##-->ここでエラーなら手動でペーストでも大丈夫なんでここは残す
(*
tell application id "com.microsoft.VSCode"
tell application "System Events"
keystroke "v" using command down
end tell
end tell
*)
to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace
| 固定リンク
« [JSON]テキストエディット yahooのAPIを使ってふりがなを付与する | トップページ | [Contacts]選択中のカードの内容でQRコード(アドレス登録用)を作成する(Safari) »
「Microsoft」カテゴリの記事
- [LINK]Teams 2(2024.11.21)
- [LINK]Teams Classic(2024.11.21)
- [bash] Microsoft Teams2 アップデート 途中(2024.08.20)
- Microsoft Teams classic キャッシュクリア(キャッシュをゴミ箱へ)(2024.01.06)
- Microsoft Teams (work or school)バージョンチェッカー(2024.01.04)
「Json」カテゴリの記事
- [JSON] JSON 2 PLIST 少し修正(2025.01.09)
- [Json]ROOTがDict形式のJSONの操作(2024.12.08)
- [Json]ROOTがArray形式のJSONの操作(2024.12.08)
- 市区町村番号取得 API利用版 ダイアログ戻り(2024.10.14)
- 市区町村番号取得 API利用版 HTML出力(2024.10.14)