郵便番号検索
DB検索 結果をHTMLで表示
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# DB検索用
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
#############################
###DBファイルへのパス
tell application "Finder"
set aliasPathToMe to (path to me) as alias
set aliasContainerDirPath to (container of aliasPathToMe) as alias
end tell
set strContainerDirPath to (POSIX path of aliasContainerDirPath) as text
set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath)
set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath()
set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true)
set ocidDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("bin/data/postno.db")
set strDbFilePathURL to (ocidDBFilePathURL's |path|()) as text
#############################
### クリップボードの中身取り出し
###初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to ocidPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
###値を格納する
tell application "Finder"
set strReadString to (the clipboard as text) as text
end tell
###Finderでエラーしたら
else
set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
if boolContain = true then
set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
set strReadString to ocidReadString as text
else
log "テキストなし"
set strReadString to "" as text
end if
end if
##############################
###ダイアログ
tell current application
set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder"
activate
end tell
else
tell current application
activate
end tell
end if
set strMes to ("住所で検索 一部分でも可\r神奈川とかで指定すると検索結果が多くなります")
set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
try
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
on error
log "エラーしました"
return
end try
if "OK" is equal to (button returned of recordResult) then
set strReturnedText to (text returned of recordResult) as text
else if (gave up of recordResult) is true then
return "時間切れです"
else
return "キャンセル"
end if
##############################
###戻り値整形
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
##############################
###ひらがなのみの場合はカタカナに
set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ぁ-んー]+$") options:(0) |error|:(reference)
set ocidRegex to (item 1 of listRegex)
set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|()))
log ocidTextRange
set numPach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange)
if (numPach as integer) = 1 then
set ocidTransform to (refMe's NSStringTransformHiraganaToKatakana)
set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:false)
end if
###数字がなければ全角に
set ocidTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:true)
########################################
##カタカナと漢字混在で検索方法が異なる
set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ァ-ヶー]+$") options:(0) |error|:(reference)
set ocidRegex to (item 1 of listRegex)
set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|()))
set numPach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange)
set strSearchText to ocidTextM as text
if (numPach as integer) = 1 then
set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT * FROM postalcode WHERE prefecture_kana LIKE '%" & strSearchText & "%' OR city_kana LIKE '%" & strSearchText & "%' OR town_kana LIKE '%" & strSearchText & "%';\"") as text
log strCommandText
else
set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT * FROM postalcode WHERE prefecture LIKE '%" & strSearchText & "%' OR city LIKE '%" & strSearchText & "%' OR town LIKE '%" & strSearchText & "%';\"") as text
log strCommandText
end if
set strResponse to (do shell script strCommandText) as text
########################################
##
set AppleScript's text item delimiters to "\r"
set listResponse to every text item of strResponse
set AppleScript's text item delimiters to ""
########################################
##HTML 基本構造
###スタイル
set strStylle to "<style>#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;} #bordertable table { width: 80%;margin: 0px;padding: 0px;border: 0px;border-spacing:0px;border-collapse: collapse;} #bordertable caption { font-weight: 900;} #bordertable thead { font-weight: 600;border-spacing:0px;} #bordertable td {border: solid 1px #666666;padding: 5px;margin: 0px;word-wrap: break-word;border-spacing:0px;} #bordertable tr {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;} #bordertable th {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;}</style>"
###ヘッダー部
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>[検索語句]" & strSearchText & "</title>" & strStylle & "</head><body>"
###ボディ
set strBody to ""
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
####
(ocidHTMLString's appendString:strHead)
#########
###テーブルの開始部
set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\">検索結果:" & strReturnedText & "</caption>") as text
set strHTML to (strHTML & "<thead title=\"項目名称\"><tr><th title=\"項目1\" scope=\"row\" > 連番 </th><th title=\"項目2\" scope=\"col\"> 郵便番号 </th><th title=\"項目3\" scope=\"col\"> 住所 </th><th title=\"項目4\" scope=\"col\"> 読み </th><th title=\"項目5\" scope=\"col\">団体コード</th><th title=\"項目6\" scope=\"col\">リンク</th></tr></thead><tbody title=\"検索結果一覧\" >") as text
set numLineNo to 1 as integer
repeat with itemLine in listResponse
set AppleScript's text item delimiters to "\t"
set listLineText to every text item of itemLine
set AppleScript's text item delimiters to ""
set strCityCode to (item 1 of listLineText) as text
set strPostNo to (item 3 of listLineText) as text
set strAddText to ((item 7 of listLineText) & (item 8 of listLineText) & (item 9 of listLineText)) as text
set strKana to ((item 4 of listLineText) & (item 5 of listLineText) & (item 6 of listLineText)) as text
set strLinkURL to ("https://www.post.japanpost.jp/cgi-zip/zipcode.php?zip=" & strPostNo & "")
set strMapURL to ("https://www.google.com/maps/search/郵便番号+" & strPostNo & "")
set strMapAppURL to ("http://maps.apple.com/?q=郵便番号+" & strPostNo & "")
set strLINK to "<a href=\"" & strLinkURL & "\" target=\"_blank\">郵政</a> | <a href=\"" & strMapURL & "\" target=\"_blank\">Google</a> | <a href=\"" & strMapAppURL & "\" target=\"_blank\">Map</a>"
set strHTML to (strHTML & "<tr><th title=\"項番1\" scope=\"row\">" & numLineNo & "</th><td title=\"項目2\"><b>" & strPostNo & "</b></td><td title=\"項目3\">" & strAddText & "</td><td title=\"項目4\"><small>" & strKana & "</small></td><td title=\"項目5\">" & strCityCode & "</td><td title=\"項目6\">" & strLINK & "</td></tr>") as text
set numLineNo to numLineNo + 1 as integer
end repeat
set strHTML to (strHTML & "</tbody><tfoot><tr><th colspan=\"6\" title=\"フッター表の終わり\" scope=\"row\">post.japanpost.jp</th></tr></tfoot></table></div>") as text
####テーブルまでを追加
(ocidHTMLString's appendString:(strHTML))
####終了部を追加
(ocidHTMLString's appendString:(strHtmlEndBody))
###ディレクトリ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###パス
set strFileName to (strSearchText & ".html") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
###ファイルに書き出し
set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
####テキストエディタで開く
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
(*
tell application "TextEdit"
activate
open file aliasFilePath
end tell
*)
tell application "Safari"
activate
open file aliasFilePath
end tell
DB作成
#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
##################################################
###設定項目 CSVのファイル名
STR_CSV_FILE_NAME="utf_all.csv"
### path to me
SCRIPT_PATH="${BASH_SOURCE[0]}"
/bin/echo "このスクリプトのパス $SCRIPT_PATH"
STR_CONTAINER_DIR_PATH=$(/usr/bin/dirname "$SCRIPT_PATH")
/bin/echo "コンテナディレクトリ $STR_CONTAINER_DIR_PATH"
STR_DATA_DIR_PATH="$STR_CONTAINER_DIR_PATH/data"
/bin/mkdir -p "$STR_DATA_DIR_PATH"
/bin/echo "データ格納ディレクトリ $STR_DATA_DIR_PATH"
STR_CSV_FILE_PATH="$STR_DATA_DIR_PATH/$STR_CSV_FILE_NAME"
/bin/echo "CSVのファイルパス $STR_CSV_FILE_PATH"
STR_IMPORT_FILE_PATH="$STR_DATA_DIR_PATH/$STR_CSV_FILE_NAME.import.csv"
/bin/echo "SQLインポート用CSVの書き出しパス $STR_IMPORT_FILE_PATH"
STR_DB_FILE_PATH="$STR_DATA_DIR_PATH/postno.db"
/bin/echo "SQLDBのパス $STR_DB_FILE_PATH"
##################################################
###古いデータは削除する
/usr/bin/touch "$STR_IMPORT_FILE_PATH"
/usr/bin/touch "$STR_DB_FILE_PATH"
/usr/bin/touch "$STR_CSV_FILE_PATH"
/bin/rm "$STR_IMPORT_FILE_PATH"
/bin/rm "$STR_DB_FILE_PATH"
/bin/rm "$STR_CSV_FILE_PATH"
##################################################
STR_URL="https://www.post.japanpost.jp/zipcode/dl/utf/zip/utf_all.zip"
###ダウンロード起動時に削除する項目
USER_TEMP_DIR=$(/usr/bin/mktemp -d)
/bin/echo "起動時に削除されるディレクトリ $USER_TEMP_DIR"
if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/utf_all.zip" "$STR_URL" --connect-timeout 20; then
/bin/echo "ファイルのダウンロードに失敗しました HTTP1.1で再トライします"
if ! /usr/bin/curl -L -o "$USER_TEMP_DIR/utf_all.zip" "$STR_URL" --http1.1 --connect-timeout 20; then
/bin/echo "ファイルのダウンロードに失敗しました"
exit 1
fi
fi
/bin/echo "ダウンロードしたZIPファイル $USER_TEMP_DIR/utf_all.zip"
##################################################
###解凍
/usr/bin/cd "$STR_DATA_DIR_PATH"
/usr/bin/bsdtar -xzvf "$USER_TEMP_DIR/utf_all.zip" -C "$STR_DATA_DIR_PATH"
/bin/echo "ダウンロードデータ解凍OK"
##################################################
###クオテーション除去
STR_TMP_FILE_PATH="$USER_TEMP_DIR/$STR_CSV_FILE_NAME.tmp.csv"
/usr/bin/sed 's/"//g' "$STR_CSV_FILE_PATH" > "$STR_TMP_FILE_PATH"
###改行変更
/usr/bin/sed 's/\r$//' "$STR_TMP_FILE_PATH" > "$STR_IMPORT_FILE_PATH"
/bin/echo "データ整形終了"
##################################################
###1行目挿入 この処理は不要だった
## STR_HEADER_TEXT="code,old_no,p_no,prefecture_kana,city_kana,town_kana,prefecture,city,town,more,aza,cho,contain,update,change"
## /usr/bin/touch "$STR_IMPORT_FILE_PATH"
## /bin/echo "$STR_HEADER_TEXT" > "$STR_IMPORT_FILE_PATH"
## /bin/cat "$STR_TMPB_FILE_PATH" >> "$STR_IMPORT_FILE_PATH"
##################################################
/bin/echo "データインポート開始"
###インポート DB作成
/usr/bin/sqlite3 "$STR_DB_FILE_PATH" <<EOF
CREATE TABLE postalcode (code TEXT,old_no TEXT,p_no TEXT,prefecture_kana TEXT,city_kana TEXT,town_kana TEXT,prefecture TEXT,city TEXT,town TEXT,more INTEGER,azamore INTEGER,chomore INTEGER,containmore INTEGER,updatemore INTEGER,changemore INTEGER);
.mode csv
.import "$STR_IMPORT_FILE_PATH" postalcode
EOF
/bin/echo "データインポート終了"
/bin/echo "処理終了しました"
exit 0
| 固定リンク
「postalcode」カテゴリの記事
- [sqlite3]駅名検索(2023.10.10)
- 郵便番号検索 v2(2023.10.10)
- 郵便番号検索(2023.09.24)
「Text CSV」カテゴリの記事
- [CSV2TSV]カンマ区切りテキストをKeyedArchiveしたPlistに変換する (見直し)(2024.12.23)
- [CSV2TSV]カンマ区切りテキストをPlistに変換する (見直し)(2024.12.23)
- [CSV2TSV]カンマ区切りテキストをタブテキストに変換する (見直し)(2024.12.23)
- CSVクオテーション方式判定(2024.03.06)
- CSV2TSV 修正(2024.03.02)