駅名検索 データ更新 HTML表示を少し変更
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #駅データ.jpのCSVを利用します |
004 | # https://ekidata.jp |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | ##自分環境がos12なので2.8にしているだけです |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | |
013 | property refMe : a reference to current application |
014 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
015 | |
016 | |
017 | ############################# |
018 | ### クリップボードの中身取り出し |
019 | ###初期化 |
020 | set ocidPasteboard to refMe's NSPasteboard's generalPasteboard() |
021 | set ocidPastBoardTypeArray to ocidPasteboard's types |
022 | ###テキストがあれば |
023 | set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text" |
024 | if boolContain = true then |
025 | ###値を格納する |
026 | tell application "Finder" |
027 | set strReadString to (the clipboard as text) as text |
028 | end tell |
029 | ###Finderでエラーしたら |
030 | else |
031 | set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType" |
032 | if boolContain = true then |
033 | set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value) |
034 | set strReadString to ocidReadString as text |
035 | else |
036 | log "テキストなし" |
037 | set strReadString to "" as text |
038 | end if |
039 | end if |
040 | |
041 | |
042 | ############################# |
043 | ###DBファイルへのパス |
044 | tell application "Finder" |
045 | set aliasPathToMe to (path to me) as alias |
046 | set aliasContainerDirPath to (container of aliasPathToMe) as alias |
047 | end tell |
048 | set strContainerDirPath to (POSIX path of aliasContainerDirPath) as text |
049 | set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath) |
050 | set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath() |
051 | set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true) |
052 | set ocidDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/stationfree.db") |
053 | set strDbFilePathURL to (ocidDBFilePathURL's |path|()) as text |
054 | |
055 | set ocidLineDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/linefree.db") |
056 | set strLineDbFilePathURL to (ocidLineDBFilePathURL's |path|()) as text |
057 | |
058 | set ocidPostDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/postno.db") |
059 | set strPostDbFilePathURL to (ocidPostDBFilePathURL's |path|()) as text |
060 | |
061 | |
062 | set strMes to ("名称や住所で検索 一部分でも可") as text |
063 | set strQueryText to strReadString as text |
064 | |
065 | ############################## |
066 | ###ダイアログ |
067 | set strName to (name of current application) as text |
068 | if strName is "osascript" then |
069 | tell application "Finder" to activate |
070 | else |
071 | tell current application to activate |
072 | end if |
073 | set aliasIconPath to POSIX file "/System/Applications/Maps.app/Contents/Resources/AppIcon.icns" as alias |
074 | try |
075 | set recordResult to (display dialog strMes with title "駅検索検索" default answer strQueryText buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record |
076 | if "OK" is equal to (button returned of recordResult) then |
077 | set strReturnedText to (text returned of recordResult) as text |
078 | else if (gave up of recordResult) is true then |
079 | return "時間切れです" |
080 | else |
081 | return "キャンセル" |
082 | end if |
083 | on error |
084 | log "エラーしました" |
085 | return |
086 | end try |
087 | ############################## |
088 | ###戻り値整形 |
089 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText)) |
090 | ###タブと改行を除去しておく |
091 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
092 | ocidTextM's appendString:(ocidResponseText) |
093 | ##改行除去 |
094 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("") |
095 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("") |
096 | ##タブ除去 |
097 | set ocidTextL to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("") |
098 | ##駅除去 |
099 | set ocidTextM to ocidTextL's stringByReplacingOccurrencesOfString:("駅") withString:("") |
100 | ############################## |
101 | ###ひらがなのみの場合はカタカナに |
102 | set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ぁ-んー]+$") options:(0) |error| :(reference) |
103 | set ocidRegex to (item 1 of listRegex) |
104 | set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|())) |
105 | log ocidTextRange |
106 | set numMach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange) |
107 | if (numMach as integer) = 1 then |
108 | set ocidTransform to (refMe's NSStringTransformHiraganaToKatakana) |
109 | set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:false) |
110 | end if |
111 | ###数字がなければ全角に |
112 | set ocidTransform to (refMe's NSStringTransformFullwidthToHalfwidth) |
113 | set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:true) |
114 | ############################## |
115 | ##カタカナと漢字混在で検索方法が異なる |
116 | set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ァ-ヶー]+$") options:(0) |error| :(reference) |
117 | set ocidRegex to (item 1 of listRegex) |
118 | set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|())) |
119 | set numMach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange) |
120 | set strSearchText to ocidTextM as text |
121 | if (numMach as integer) = 1 then |
122 | set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT COUNT(*) FROM stationfree WHERE station_name LIKE '%" & strSearchText & "%' OR address LIKE '%" & strSearchText & "%';\"") as text |
123 | log strCommandText |
124 | else |
125 | set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT COUNT(*) FROM stationfree WHERE station_name LIKE '%" & strSearchText & "%' OR address LIKE '%" & strSearchText & "%';\"") as text |
126 | log strCommandText |
127 | end if |
128 | ###検索結果の件数 |
129 | set numQueryCnt to (do shell script strCommandText) as integer |
130 | ############################## |
131 | ###件数が100超える場合は中止を促す |
132 | if numQueryCnt > 100 then |
133 | log "検索結果100件超です" |
134 | ###ダイアログを前面に出す |
135 | set strName to (name of current application) as text |
136 | if strName is "osascript" then |
137 | tell application "Finder" to activate |
138 | else |
139 | tell current application to activate |
140 | end if |
141 | ##1件の処理時間 |
142 | set numMin to (0.01 * numQueryCnt) as integer |
143 | set strAlertMes to "検索結果100件超です(" & numQueryCnt & "件)\r継続すると結果表示まで約:" & numMin & "秒かかります" as text |
144 | try |
145 | set recordResponse to (display alert ("【選んでください】\r" & strAlertMes) buttons {"継続", "終了"} default button "継続" cancel button "終了" as informational giving up after 10) as record |
146 | on error |
147 | log "エラーしました" |
148 | return "キャンセルしました。処理を中止します。再度実行してください" |
149 | end try |
150 | if true is equal to (gave up of recordResponse) then |
151 | return "時間切れです。処理を中止します。再度実行してください" |
152 | end if |
153 | else if numQueryCnt = 0 then |
154 | log "検索結果0件です" |
155 | end if |
156 | ####処理継続の場合はそのまま進む |
157 | if (numMach as integer) = 1 then |
158 | set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT * FROM stationfree WHERE station_name LIKE '%" & strSearchText & "%' OR address LIKE '%" & strSearchText & "%' OR prefecture LIKE '%" & strSearchText & "%' OR city LIKE '%" & strSearchText & "%' OR town LIKE '%" & strSearchText & "%' OR pref_and_city LIKE '%" & strSearchText & "%' OR city_and_town LIKE '%" & strSearchText & "%';\"") as text |
159 | log strCommandText |
160 | else |
161 | set strCommandText to ("/usr/bin/sqlite3 \"" & strDbFilePathURL & "\" -tabs \"SELECT * FROM stationfree WHERE station_name LIKE '%" & strSearchText & "%' OR address LIKE '%" & strSearchText & "%' OR prefecture LIKE '%" & strSearchText & "%' OR city LIKE '%" & strSearchText & "%' OR town LIKE '%" & strSearchText & "%' OR pref_and_city LIKE '%" & strSearchText & "%' OR city_and_town LIKE '%" & strSearchText & "%';\"") as text |
162 | log strCommandText |
163 | end if |
164 | set strResponse to (do shell script strCommandText) as text |
165 | log strResponse |
166 | |
167 | ######################################## |
168 | ##コマンドの戻り値を改行でリストに |
169 | set AppleScript's text item delimiters to "\r" |
170 | set listResponse to every text item of strResponse |
171 | set AppleScript's text item delimiters to "" |
172 | |
173 | ######################################## |
174 | #headerに渡すエレメント |
175 | set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3") |
176 | set strSetValue to ("検索語句(" & ocidTextL & ")の検索結果:") as text |
177 | (ocidH3Element's setStringValue:(strSetValue)) |
178 | ######################################## |
179 | #footerに渡すエレメント |
180 | set ocidFotterAElement to refMe's NSXMLElement's elementWithName:("a") |
181 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/")) |
182 | (ocidFotterAElement's addAttribute:(ocidAddNode)) |
183 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
184 | (ocidFotterAElement's addAttribute:(ocidAddNode)) |
185 | set strContents to ("AppleScriptで生成しました") as text |
186 | (ocidFotterAElement's setStringValue:(strContents)) |
187 | ######################################## |
188 | #articleに渡すエレメント |
189 | #テーブル部生成開始 |
190 | set ocidTableElement to refMe's NSXMLElement's elementWithName:("table") |
191 | #【caption】 |
192 | set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption") |
193 | set strSetValue to ("検索語句: " & strSearchText) as text |
194 | ocidCaptionElement's setStringValue:(strSetValue) |
195 | ocidTableElement's addChild:(ocidCaptionElement) |
196 | #【colgroup】 |
197 | set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup") |
198 | #テーブルのタイトル部 |
199 | set listColName to {"項番", "駅名", "路線名", "住所", "リンク"} as list |
200 | #タイトル部の数だけ繰り返し |
201 | repeat with itemColName in listColName |
202 | #【col】col生成 |
203 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col")) |
204 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName)) |
205 | (ocidAddElement's addAttribute:(ocidAddNode)) |
206 | (ocidColgroupElement's addChild:(ocidAddElement)) |
207 | end repeat |
208 | #テーブルエレメントに追加 |
209 | ocidTableElement's addChild:(ocidColgroupElement) |
210 | #【thead】 |
211 | set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead") |
212 | #TR |
213 | set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr") |
214 | #タイトル部の数だけ繰り返し |
215 | repeat with itemColName in listColName |
216 | #ここはTDではなくてTHを利用 |
217 | set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th")) |
218 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName)) |
219 | (ocidAddElement's addAttribute:(ocidAddNode)) |
220 | #値を入れる |
221 | (ocidAddElement's setStringValue:(itemColName)) |
222 | #TH→TRにセット |
223 | (ocidTrElement's addChild:(ocidAddElement)) |
224 | end repeat |
225 | #TRをTHEADにセット |
226 | ocidTheadElement's addChild:(ocidTrElement) |
227 | #THEADをテーブルにセット |
228 | ocidTableElement's addChild:(ocidTheadElement) |
229 | |
230 | |
231 | ######################################## |
232 | #【tbody】 |
233 | set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody") |
234 | ###【3-4】:item |
235 | set numLineNO to 1 as integer |
236 | repeat with itemLine in listResponse |
237 | |
238 | ###各行タブ区切りなのでタブでリストにする |
239 | set AppleScript's text item delimiters to "\t" |
240 | set listLineText to every text item of itemLine |
241 | set AppleScript's text item delimiters to "" |
242 | ###必要な項目を取得 |
243 | set strLineCode to (item 6 of listLineText) as text |
244 | set strCommandText to ("/usr/bin/sqlite3 \"" & strLineDbFilePathURL & "\" -tabs \"SELECT line_name FROM linefree WHERE line_cd = '" & strLineCode & "';\"") as text |
245 | log strCommandText |
246 | set strLineName to (do shell script strCommandText) as text |
247 | |
248 | set strPostNO to (item 3 of listLineText) as text |
249 | set strAddText to (item 9 of listLineText) as text |
250 | |
251 | set strLong to (item 10 of listLineText) as text |
252 | set strLat to (item 11 of listLineText) as text |
253 | ###リンク生成 |
254 | ###リンク1 |
255 | set strMapURL to ("https://www.navitime.co.jp/maps/aroundResult?lat=" & strLat & "&lon=" & strLong & "") |
256 | set ocidLinkEl01 to (refMe's NSXMLElement's elementWithName:("a")) |
257 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
258 | (ocidLinkEl01's addAttribute:(ocidAddNode)) |
259 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
260 | (ocidLinkEl01's addAttribute:(ocidAddNode)) |
261 | (ocidLinkEl01's setStringValue:("Navitime Map")) |
262 | ###リンク2 |
263 | set strMapURL to ("https://map.yahoo.co.jp/place?lat=" & strLat & "&lon=" & strLong & "&zoom=15&maptype=trainRoutes") |
264 | set ocidLinkEl02 to (refMe's NSXMLElement's elementWithName:("a")) |
265 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
266 | (ocidLinkEl02's addAttribute:(ocidAddNode)) |
267 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
268 | (ocidLinkEl02's addAttribute:(ocidAddNode)) |
269 | (ocidLinkEl02's setStringValue:("Yahoo Map")) |
270 | |
271 | ###リンク3 |
272 | set strMapURL to ("https://maps.gsi.go.jp/vector/#15/" & strLat & "/" & strLong & "/&ls=vstd&disp=1&d=l") |
273 | set ocidLinkEl03 to (refMe's NSXMLElement's elementWithName:("a")) |
274 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
275 | (ocidLinkEl03's addAttribute:(ocidAddNode)) |
276 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
277 | (ocidLinkEl03's addAttribute:(ocidAddNode)) |
278 | (ocidLinkEl03's setStringValue:("Gsi vector Map")) |
279 | |
280 | ###リンク4 |
281 | set strMapURL to ("https://www.jma.go.jp/bosai/nowc/#lat:" & strLat & "/lon:" & strLong & "/zoom:15/") |
282 | set ocidLinkEl04 to (refMe's NSXMLElement's elementWithName:("a")) |
283 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
284 | (ocidLinkEl04's addAttribute:(ocidAddNode)) |
285 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
286 | (ocidLinkEl04's addAttribute:(ocidAddNode)) |
287 | (ocidLinkEl04's setStringValue:("Jma Map")) |
288 | |
289 | ###リンク5 |
290 | set strMapURL to ("http://maps.apple.com/?ll=" & strLat & "," & strLong & "") |
291 | set ocidLinkEl05 to (refMe's NSXMLElement's elementWithName:("a")) |
292 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
293 | (ocidLinkEl05's addAttribute:(ocidAddNode)) |
294 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
295 | (ocidLinkEl05's addAttribute:(ocidAddNode)) |
296 | (ocidLinkEl05's setStringValue:("Apple Map")) |
297 | |
298 | ##リンク6 |
299 | set strMapURL to ("https://www.google.com/maps/@" & strLat & "," & strLong & ",21z") |
300 | set ocidLinkEl06 to (refMe's NSXMLElement's elementWithName:("a")) |
301 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMapURL)) |
302 | (ocidLinkEl06's addAttribute:(ocidAddNode)) |
303 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
304 | (ocidLinkEl06's addAttribute:(ocidAddNode)) |
305 | (ocidLinkEl06's setStringValue:("Google Map")) |
306 | |
307 | # # # # # |
308 | #TRの開始 |
309 | set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr")) |
310 | #【行番号】をTHでセット |
311 | set strZeroSupp to ("00") as text |
312 | set strZeroSupp to ("00" & numLineNO) as text |
313 | set strLineNO to (text -3 through -1 of strZeroSupp) as text |
314 | set ocidThElement to (refMe's NSXMLElement's elementWithName:("th")) |
315 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項番")) |
316 | (ocidThElement's addAttribute:(ocidAddNode)) |
317 | (ocidThElement's setStringValue:(strLineNO)) |
318 | (ocidTrElement's addChild:(ocidThElement)) |
319 | #【ジャンル】をTDでセット |
320 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td")) |
321 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("駅名")) |
322 | (ocidTdElement's addAttribute:(ocidAddNode)) |
323 | (ocidTdElement's setStringValue:(strPostNO)) |
324 | (ocidTrElement's addChild:(ocidTdElement)) |
325 | #【ジャンル】をTDでセット |
326 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td")) |
327 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("路線名")) |
328 | (ocidTdElement's addAttribute:(ocidAddNode)) |
329 | (ocidTdElement's setStringValue:(strLineName)) |
330 | (ocidTrElement's addChild:(ocidTdElement)) |
331 | #【住所】をTDでセット |
332 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td")) |
333 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("住所")) |
334 | (ocidTdElement's addAttribute:(ocidAddNode)) |
335 | (ocidTdElement's setStringValue:(strAddText)) |
336 | (ocidTrElement's addChild:(ocidTdElement)) |
337 | #【リンク】をTDでセット |
338 | set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td")) |
339 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("リンク")) |
340 | (ocidTdElement's addAttribute:(ocidAddNode)) |
341 | set ocidSpacer1 to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind)) |
342 | (ocidSpacer1's setStringValue:(" | ")) |
343 | set ocidSpacer2 to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind)) |
344 | (ocidSpacer2's setStringValue:(" | ")) |
345 | set ocidSpacer3 to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind)) |
346 | (ocidSpacer3's setStringValue:(" | ")) |
347 | set ocidSpacer4 to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind)) |
348 | (ocidSpacer4's setStringValue:(" | ")) |
349 | set ocidSpacer5 to (refMe's NSXMLNode's alloc()'s initWithKind:(refMe's NSXMLTextKind)) |
350 | (ocidSpacer5's setStringValue:(" | ")) |
351 | |
352 | |
353 | (ocidTdElement's addChild:(ocidLinkEl01)) |
354 | (ocidTdElement's addChild:(ocidSpacer1)) |
355 | (ocidTdElement's addChild:(ocidLinkEl02)) |
356 | (ocidTdElement's addChild:(ocidSpacer2)) |
357 | (ocidTdElement's addChild:(ocidLinkEl03)) |
358 | (ocidTdElement's addChild:(ocidSpacer3)) |
359 | (ocidTdElement's addChild:(ocidLinkEl04)) |
360 | (ocidTdElement's addChild:(ocidSpacer4)) |
361 | (ocidTdElement's addChild:(ocidLinkEl05)) |
362 | (ocidTdElement's addChild:(ocidSpacer5)) |
363 | (ocidTdElement's addChild:(ocidLinkEl06)) |
364 | |
365 | #### |
366 | (ocidTrElement's addChild:(ocidTdElement)) |
367 | #出来上がったTRをTBODYにセット |
368 | (ocidTbodyElement's addChild:(ocidTrElement)) |
369 | set numLineNO to numLineNO + 1 as integer |
370 | end repeat |
371 | #TBODYをテーブルにセット |
372 | ocidTableElement's addChild:(ocidTbodyElement) |
373 | #【tfoot】 TRで |
374 | set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot") |
375 | set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr") |
376 | #項目数を取得して |
377 | set numCntCol to (count of listColName) as integer |
378 | #colspan指定して1行でセット |
379 | set ocidThElement to (refMe's NSXMLElement's elementWithName:("th")) |
380 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり")) |
381 | (ocidThElement's addAttribute:(ocidAddNode)) |
382 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text)) |
383 | (ocidThElement's addAttribute:(ocidAddNode)) |
384 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row")) |
385 | (ocidThElement's addAttribute:(ocidAddNode)) |
386 | # |
387 | set ocidTfotterAElement to refMe's NSXMLElement's elementWithName:("a") |
388 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://ekidata.jp")) |
389 | (ocidTfotterAElement's addAttribute:(ocidAddNode)) |
390 | set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank")) |
391 | (ocidTfotterAElement's addAttribute:(ocidAddNode)) |
392 | set strContents to ("駅データ.jp") as text |
393 | (ocidTfotterAElement's setStringValue:(strContents)) |
394 | ocidThElement's addChild:(ocidTfotterAElement) |
395 | #THをTRにセットして |
396 | ocidTrElement's addChild:(ocidThElement) |
397 | #TRをTFOOTにセット |
398 | ocidTfootElement's addChild:(ocidTrElement) |
399 | #TFOOTをテーブルにセット |
400 | ocidTableElement's addChild:(ocidTfootElement) |
401 | |
402 | |
403 | |
404 | ############################## |
405 | #HTMLにする |
406 | ############################## |
407 | set ocidHTML to doMakeRootElement({ocidH3Element, ocidTableElement, ocidFotterAElement}) |
408 | |
409 | ############################## |
410 | #保存 |
411 | ############################## |
412 | #####【5−1】保存先ディレクトリの確保 |
413 | set appFileManager to refMe's NSFileManager's defaultManager() |
414 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
415 | #上書きできないようにUUIDで別名フォルダにする |
416 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
417 | set ocidUUIDString to ocidUUID's UUIDString |
418 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
419 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
420 | #アクセス権777=511を指定 |
421 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
422 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
423 | #保存ファイル名 |
424 | set strFileName to (strSearchText & ".html") as text |
425 | #保存先URL |
426 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false |
427 | |
428 | |
429 | #####【5−2】保存 |
430 | #読み取りやすい表示 |
431 | set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint) |
432 | |
433 | set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference) |
434 | |
435 | ####【6】ブラウザで開く |
436 | set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias |
437 | tell application "Finder" |
438 | open location aliasFilePath |
439 | end tell |
440 | |
441 | |
442 | |
443 | return |
444 | ######################################## |
445 | ######################################## |
446 | ##HTML 基本構造 |
447 | ###スタイル |
448 | 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>" |
449 | ###ヘッダー部 |
450 | set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>[検索語句]" & strSearchText & "</title>" & strStylle & "</head><body>" |
451 | ###ボディ |
452 | set strBody to "" |
453 | ###最後 |
454 | set strHtmlEndBody to "</body></html>" |
455 | ###HTML書き出し用のテキスト初期化 |
456 | set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0 |
457 | #### |
458 | (ocidHTMLString's appendString:strHead) |
459 | ######### |
460 | ###テーブルの開始部 |
461 | set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\">検索結果:" & strReturnedText & "</caption>") as text |
462 | 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></tr></thead><tbody title=\"検索結果一覧\" >") as text |
463 | (ocidHTMLString's appendString:(strHTML)) |
464 | set numLineNO to 1 as integer |
465 | repeat with itemLine in listResponse |
466 | ###各行タブ区切りなのでタブでリストにする |
467 | set AppleScript's text item delimiters to "\t" |
468 | set listLineText to every text item of itemLine |
469 | set AppleScript's text item delimiters to "" |
470 | ###必要な項目を取得 |
471 | set strLineCode to (item 6 of listLineText) as text |
472 | set strCommandText to ("/usr/bin/sqlite3 \"" & strLineDbFilePathURL & "\" -tabs \"SELECT line_name FROM linefree WHERE line_cd = '" & strLineCode & "';\"") as text |
473 | log strCommandText |
474 | set strLineName to (do shell script strCommandText) as text |
475 | |
476 | set strPostNO to (item 3 of listLineText) as text |
477 | set strAddText to (item 9 of listLineText) as text |
478 | |
479 | set strLong to (item 10 of listLineText) as text |
480 | set strLat to (item 11 of listLineText) as text |
481 | ###リンク生成 |
482 | |
483 | ###リンク1 |
484 | set strMapURL to ("https://www.navitime.co.jp/maps/aroundResult?lat=" & strLat & "&lon=" & strLong & "") |
485 | set strLINK1 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Navitime Map</a>" |
486 | ###リンク2 |
487 | set strMapURL to ("https://map.yahoo.co.jp/place?lat=" & strLat & "&lon=" & strLong & "&zoom=15&maptype=trainRoutes") |
488 | set strLINK2 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Yahoo Map</a>" |
489 | ###リンク3 |
490 | set strMapURL to ("https://maps.gsi.go.jp/vector/#15/" & strLat & "/" & strLong & "/&ls=vstd&disp=1&d=l") |
491 | set strLINK3 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Gsi vector Map</a>" |
492 | ###リンク4 |
493 | set strMapURL to ("https://www.jma.go.jp/bosai/nowc/#lat:" & strLat & "/lon:" & strLong & "/zoom:15/") |
494 | set strLINK4 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Jma Map</a>" |
495 | ###リンク5 |
496 | set strMapURL to ("http://maps.apple.com/?ll=" & strLat & "," & strLong & "") |
497 | set strLINK5 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Apple Map</a>" |
498 | ##リンク6 |
499 | set strMapURL to ("https://www.google.com/maps/@" & strLat & "," & strLong & ",21z") |
500 | set strLINK6 to "<a href=\"" & strMapURL & "\" target=\"_blank\">Google Map</a>" |
501 | |
502 | |
503 | set strLINK to "" & strLINK1 & " | " & strLINK2 & " | " & strLINK3 & " | " & strLINK4 & " | " & strLINK5 & " | " & strLINK6 & "" |
504 | ###HTMLにして |
505 | set strHTML to ("<tr><th title=\"項番1\" scope=\"row\">" & numLineNO & "</th><td title=\"項目2\"><b>" & strPostNO & "</b></td><td title=\"項目3\">" & strLineName & "</td><td title=\"項目4\">" & strAddText & "</td><td title=\"項目5\">" & strLINK & "</td></tr>") as text |
506 | (ocidHTMLString's appendString:(strHTML)) |
507 | set numLineNO to numLineNO + 1 as integer |
508 | end repeat |
509 | |
510 | set strHTML to ("</tbody><tfoot><tr><th colspan=\"6\" title=\"フッター表の終わり\" scope=\"row\">ekidata.jp</th></tr></tfoot></table></div>") as text |
511 | ####テーブルまでを追加 |
512 | (ocidHTMLString's appendString:(strHTML)) |
513 | ####終了部を追加 |
514 | (ocidHTMLString's appendString:(strHtmlEndBody)) |
515 | |
516 | ###ディレクトリ |
517 | set appFileManager to refMe's NSFileManager's defaultManager() |
518 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
519 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
520 | set ocidUUIDString to ocidUUID's UUIDString |
521 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
522 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
523 | # 777-->511 755-->493 700-->448 766-->502 |
524 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
525 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
526 | ###パス |
527 | |
528 | set strFileName to (strSearchText & ".html") as text |
529 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false |
530 | ###ファイルに書き出し |
531 | set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
532 | ####テキストエディタで開く |
533 | set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias |
534 | (* |
535 | tell application "TextEdit" |
536 | activate |
537 | open file aliasFilePath |
538 | end tell |
539 | *) |
540 | tell application "Safari" |
541 | activate |
542 | open file aliasFilePath |
543 | end tell |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | ############################## |
550 | # 基本的なHTMLの構造 |
551 | (* |
552 | doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents}) |
553 | HTMLのBODY部 |
554 | header |
555 | article |
556 | footerにそれぞれAddchildするデータをリストで渡す |
557 | 戻り値はRootエレメントにセットされた |
558 | NSXMLDocumentを戻すので 保存すればOK |
559 | *) |
560 | ############################## |
561 | to doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents}) |
562 | #XML初期化 |
563 | set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init() |
564 | ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind) |
565 | # DTD付与 |
566 | set ocidDTD to refMe's NSXMLDTD's alloc()'s init() |
567 | ocidDTD's setName:("html") |
568 | ocidXMLDoc's setDTD:(ocidDTD) |
569 | # |
570 | set ocidRootElement to refMe's NSXMLElement's elementWithName:("html") |
571 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja") |
572 | ocidRootElement's addAttribute:(ocidAddNode) |
573 | # |
574 | set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head") |
575 | # |
576 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("title") |
577 | ocidAddElement's setStringValue:("駅名検索:検索結果") |
578 | ocidHeadElement's addChild:(ocidAddElement) |
579 | # http-equiv |
580 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta") |
581 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type") |
582 | ocidAddElement's addAttribute:(ocidAddNode) |
583 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8") |
584 | ocidAddElement's addAttribute:(ocidAddNode) |
585 | ocidHeadElement's addChild:(ocidAddElement) |
586 | # |
587 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta") |
588 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Style-Type") |
589 | ocidAddElement's addAttribute:(ocidAddNode) |
590 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css") |
591 | ocidAddElement's addAttribute:(ocidAddNode) |
592 | ocidHeadElement's addChild:(ocidAddElement) |
593 | # |
594 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta") |
595 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Script-Type") |
596 | ocidAddElement's addAttribute:(ocidAddNode) |
597 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript") |
598 | ocidAddElement's addAttribute:(ocidAddNode) |
599 | ocidHeadElement's addChild:(ocidAddElement) |
600 | # |
601 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta") |
602 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport") |
603 | ocidAddElement's addAttribute:(ocidAddNode) |
604 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720") |
605 | ocidAddElement's addAttribute:(ocidAddNode) |
606 | ocidHeadElement's addChild:(ocidAddElement) |
607 | # |
608 | set ocidAddElement to refMe's NSXMLElement's elementWithName:("style") |
609 | ocidAddElement's setStringValue:("body { margin: 10px; background-color: #FFFFFF; } table { border-spacing: 0; caption-side: top; font-family: system-ui; } thead th { border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:first-of-type { border-start-start-radius: .5em } &:last-of-type { border-start-end-radius: .5em; border-inline-end-width: 1px } } tbody td { word-wrap: break-word;max-width: 360px;border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:last-of-type { border-inline-end-width: 1px } } tbody th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } tbody tr:nth-of-type(odd) { background: #F2F2F2; } .kind_string { font-size: 0.75em; } .date_string { font-size: 0.5em; } tfoot th { border: solid 1px #666666; padding: .5ch 1ch; &:first-of-type { border-end-start-radius: .5em } &:last-of-type { border-end-end-radius: .5em; border-inline-end-width: 1px } }") |
610 | ocidHeadElement's addChild:(ocidAddElement) |
611 | ocidRootElement's addChild:(ocidHeadElement) |
612 | # |
613 | #ボディエレメント |
614 | set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body") |
615 | #ヘッダー |
616 | set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header") |
617 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header") |
618 | ocidHeaderElement's addAttribute:(ocidAddNode) |
619 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header") |
620 | ocidHeaderElement's addAttribute:(ocidAddNode) |
621 | ocidHeaderElement's addChild:(argHeaderContents) |
622 | ocidBodyElement's addChild:(ocidHeaderElement) |
623 | #アーティクル |
624 | set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article") |
625 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article") |
626 | ocidArticleElement's addAttribute:(ocidAddNode) |
627 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article") |
628 | ocidArticleElement's addAttribute:(ocidAddNode) |
629 | ocidArticleElement's addChild:(argArticleContents) |
630 | ocidBodyElement's addChild:(ocidArticleElement) |
631 | #フッター |
632 | set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer") |
633 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer") |
634 | ocidFooterElement's addAttribute:(ocidAddNode) |
635 | set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer") |
636 | ocidFooterElement's addAttribute:(ocidAddNode) |
637 | ocidFooterElement's addChild:(argFooterContents) |
638 | ocidBodyElement's addChild:(ocidFooterElement) |
639 | |
640 | ocidRootElement's addChild:(ocidBodyElement) |
641 | ocidXMLDoc's setRootElement:(ocidRootElement) |
642 | return ocidXMLDoc |
643 | end doMakeRootElement |
AppleScriptで生成しました |
| 固定リンク