AppleScript Map

駅名検索 データ更新 HTML表示を少し変更




ダウンロード - ekidata.jp2.zip




AppleScript サンプルコード

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

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にしているだけです
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012
013property refMe : a reference to current application
014property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
015
016
017#############################
018### クリップボードの中身取り出し
019###初期化
020set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
021set ocidPastBoardTypeArray to ocidPasteboard's types
022###テキストがあれば
023set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
024if boolContain = true then
025  ###値を格納する
026  tell application "Finder"
027    set strReadString to (the clipboard as text) as text
028  end tell
029  ###Finderでエラーしたら
030else
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
039end if
040
041
042#############################
043###DBファイルへのパス
044tell application "Finder"
045  set aliasPathToMe to (path to me) as alias
046  set aliasContainerDirPath to (container of aliasPathToMe) as alias
047end tell
048set strContainerDirPath to (POSIX path of aliasContainerDirPath) as text
049set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath)
050set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath()
051set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true)
052set ocidDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/stationfree.db")
053set strDbFilePathURL to (ocidDBFilePathURL's |path|()) as text
054
055set ocidLineDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/linefree.db")
056set strLineDbFilePathURL to (ocidLineDBFilePathURL's |path|()) as text
057
058set ocidPostDBFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/postno.db")
059set strPostDbFilePathURL to (ocidPostDBFilePathURL's |path|()) as text
060
061
062set strMes to ("名称や住所で検索 一部分でも可") as text
063set strQueryText to strReadString as text
064
065##############################
066###ダイアログ
067set strName to (name of current application) as text
068if strName is "osascript" then
069  tell application "Finder" to activate
070else
071  tell current application to activate
072end if
073set aliasIconPath to POSIX file "/System/Applications/Maps.app/Contents/Resources/AppIcon.icns" as alias
074try
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
083on error
084  log "エラーしました"
085  return
086end try
087##############################
088###戻り値整形
089set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
090###タブと改行を除去しておく
091set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
092ocidTextM's appendString:(ocidResponseText)
093##改行除去
094set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
095set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
096##タブ除去
097set ocidTextL to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
098##駅除去
099set ocidTextM to ocidTextL's stringByReplacingOccurrencesOfString:("駅") withString:("")
100##############################
101###ひらがなのみの場合はカタカナに
102set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ぁ-んー]+$") options:(0) |error| :(reference)
103set ocidRegex to (item 1 of listRegex)
104set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|()))
105log ocidTextRange
106set numMach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange)
107if (numMach as integer) = 1 then
108  set ocidTransform to (refMe's NSStringTransformHiraganaToKatakana)
109  set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:false)
110end if
111###数字がなければ全角に
112set ocidTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
113set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidTransform) |reverse|:true)
114##############################
115##カタカナと漢字混在で検索方法が異なる
116set listRegex to refMe's NSRegularExpression's regularExpressionWithPattern:("^[ァ-ヶー]+$") options:(0) |error| :(reference)
117set ocidRegex to (item 1 of listRegex)
118set ocidTextRange to refMe's NSMakeRange(0, (ocidTextM's |length|()))
119set numMach to ocidRegex's numberOfMatchesInString:(ocidTextM) options:0 range:(ocidTextRange)
120set strSearchText to ocidTextM as text
121if (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
124else
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
127end if
128###検索結果の件数
129set numQueryCnt to (do shell script strCommandText) as integer
130##############################
131###件数が100超える場合は中止を促す
132if 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
153else if numQueryCnt = 0 then
154  log "検索結果0件です"
155end if
156####処理継続の場合はそのまま進む
157if (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
160else
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
163end if
164set strResponse to (do shell script strCommandText) as text
165log strResponse
166
167########################################
168##コマンドの戻り値を改行でリストに
169set AppleScript's text item delimiters to "\r"
170set listResponse to every text item of strResponse
171set AppleScript's text item delimiters to ""
172
173########################################
174#headerに渡すエレメント
175set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
176set strSetValue to ("検索語句(" & ocidTextL & ")の検索結果:") as text
177(ocidH3Element's setStringValue:(strSetValue))
178########################################
179#footerに渡すエレメント
180set ocidFotterAElement to refMe's NSXMLElement's elementWithName:("a")
181set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
182(ocidFotterAElement's addAttribute:(ocidAddNode))
183set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
184(ocidFotterAElement's addAttribute:(ocidAddNode))
185set strContents to ("AppleScriptで生成しました") as text
186(ocidFotterAElement's setStringValue:(strContents))
187########################################
188#articleに渡すエレメント
189#テーブル部生成開始
190set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
191#【caption】
192set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
193set strSetValue to ("検索語句: " & strSearchText) as text
194ocidCaptionElement's setStringValue:(strSetValue)
195ocidTableElement's addChild:(ocidCaptionElement)
196#【colgroup】
197set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
198#テーブルのタイトル部
199set listColName to {"項番", "駅名", "路線名", "住所", "リンク"} as list
200#タイトル部の数だけ繰り返し
201repeat 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))
207end repeat
208#テーブルエレメントに追加
209ocidTableElement's addChild:(ocidColgroupElement)
210#【thead】
211set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
212#TR
213set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
214#タイトル部の数だけ繰り返し
215repeat 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))
224end repeat
225#TRをTHEADにセット
226ocidTheadElement's addChild:(ocidTrElement)
227#THEADをテーブルにセット
228ocidTableElement's addChild:(ocidTheadElement)
229
230
231########################################
232#【tbody】
233set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
234###【3-4】:item
235set numLineNO to 1 as integer
236repeat 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
370end repeat
371#TBODYをテーブルにセット
372ocidTableElement's addChild:(ocidTbodyElement)
373#【tfoot】 TRで
374set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot")
375set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
376#項目数を取得して
377set numCntCol to (count of listColName) as integer
378#colspan指定して1行でセット
379set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
380set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
381(ocidThElement's addAttribute:(ocidAddNode))
382set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text))
383(ocidThElement's addAttribute:(ocidAddNode))
384set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
385(ocidThElement's addAttribute:(ocidAddNode))
386#
387set ocidTfotterAElement to refMe's NSXMLElement's elementWithName:("a")
388set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://ekidata.jp"))
389(ocidTfotterAElement's addAttribute:(ocidAddNode))
390set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
391(ocidTfotterAElement's addAttribute:(ocidAddNode))
392set strContents to ("駅データ.jp") as text
393(ocidTfotterAElement's setStringValue:(strContents))
394ocidThElement's addChild:(ocidTfotterAElement)
395#THをTRにセットして
396ocidTrElement's addChild:(ocidThElement)
397#TRをTFOOTにセット
398ocidTfootElement's addChild:(ocidTrElement)
399#TFOOTをテーブルにセット
400ocidTableElement's addChild:(ocidTfootElement)
401
402
403
404##############################
405#HTMLにする
406##############################
407set ocidHTML to doMakeRootElement({ocidH3Element, ocidTableElement, ocidFotterAElement})
408
409##############################
410#保存
411##############################
412#####【5−1】保存先ディレクトリの確保
413set appFileManager to refMe's NSFileManager's defaultManager()
414set ocidTempDirURL to appFileManager's temporaryDirectory()
415#上書きできないようにUUIDで別名フォルダにする
416set ocidUUID to refMe's NSUUID's alloc()'s init()
417set ocidUUIDString to ocidUUID's UUIDString
418set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
419set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
420#アクセス権777=511を指定
421ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
422set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
423#保存ファイル名
424set strFileName to (strSearchText & ".html") as text
425#保存先URL
426set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
427
428
429#####【5−2】保存
430#読み取りやすい表示
431set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
432
433set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)
434
435####【6】ブラウザで開く
436set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
437tell application "Finder"
438  open location aliasFilePath
439end tell
440
441
442
443return
444########################################
445########################################
446##HTML 基本構造
447###スタイル
448set 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###ヘッダー部
450set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>[検索語句]" & strSearchText & "</title>" & strStylle & "</head><body>"
451###ボディ
452set strBody to ""
453###最後
454set strHtmlEndBody to "</body></html>"
455###HTML書き出し用のテキスト初期化
456set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
457####
458(ocidHTMLString's appendString:strHead)
459#########
460###テーブルの開始部
461set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\">検索結果:" & strReturnedText & "</caption>") as text
462set 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))
464set numLineNO to 1 as integer
465repeat 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 & "&nbsp;|&nbsp;" & strLINK2 & "&nbsp;|&nbsp;" & strLINK3 & "&nbsp;|&nbsp;" & strLINK4 & "&nbsp;|&nbsp;" & strLINK5 & "&nbsp;|&nbsp;" & 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
508end repeat
509
510set 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###ディレクトリ
517set appFileManager to refMe's NSFileManager's defaultManager()
518set ocidTempDirURL to appFileManager's temporaryDirectory()
519set ocidUUID to refMe's NSUUID's alloc()'s init()
520set ocidUUIDString to ocidUUID's UUIDString
521set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
522set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
523# 777-->511 755-->493 700-->448 766-->502
524ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
525set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
526###パス
527
528set strFileName to (strSearchText & ".html") as text
529set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
530###ファイルに書き出し
531set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
532####テキストエディタで開く
533set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
534(*
535tell application "TextEdit"
536  activate
537  open file aliasFilePath
538end tell
539*)
540tell application "Safari"
541  activate
542  open file aliasFilePath
543end tell
544
545
546
547
548
549##############################
550# 基本的なHTMLの構造
551(*
552doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
553HTMLのBODY部
554header
555article
556footerにそれぞれAddchildするデータをリストで渡す
557戻り値はRootエレメントにセットされた
558NSXMLDocumentを戻すので 保存すればOK
559*)
560##############################
561to 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
643end doMakeRootElement
AppleScriptで生成しました

|

駅名検索 DB更新

ダウンロード - e9a785e5908de6a49ce7b4a2ekidata.jp.zip

|

[XML]位置情報の取得(ip-api.com)


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
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##############################
015### 【1】JSONのURL
016set ocidComponents to refMe's NSURLComponents's alloc()'s init()
017ocidComponents's setScheme:("https")
018ocidComponents's setHost:("api.ipify.org")
019ocidComponents's setQuery:("format=json")
020set ocidURL to ocidComponents's |URL|
021
022### 【2】URLの内容JSONをNSdataに格納
023set ocidOption to (refMe's NSDataReadingMappedIfSafe)
024set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)
025if (item 2 of listResponse) = (missing value) then
026  log "正常処理"
027  set ocidReadData to (item 1 of listResponse)
028else if (item 2 of listResponse) ≠ (missing value) then
029  log (item 2 of listResponse)'s code() as text
030  log (item 2 of listResponse)'s localizedDescription() as text
031  return "JSON エラーしました"
032end if
033
034### 【3】NSPropertyListSerializationしてレコードに
035set ocidOption to (refMe's NSJSONReadingMutableContainers)
036set listResponse to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidReadData) options:(ocidOption) |error| :(reference))
037if (item 2 of listResponse) = (missing value) then
038  log "正常処理"
039  set ocidJsonDict to (item 1 of listResponse)
040else if (item 2 of listResponse) ≠ (missing value) then
041  log (item 2 of listResponse)'s code() as text
042  log (item 2 of listResponse)'s localizedDescription() as text
043  return "Serialization エラーしました"
044end if
045##
046set strIP to (ocidJsonDict's valueForKey:("ip")) as text
047
048
049##############################
050###【4】Geo
051set ocidComponents to refMe's NSURLComponents's alloc()'s init()
052ocidComponents's setScheme:("http")
053ocidComponents's setHost:("ip-api.com")
054set strSetValue to ("/xml/" & strIP) as text
055ocidComponents's setPath:(strSetValue)
056set ocidURL to ocidComponents's |URL|
057
058### 【5】URLの内容JSONをNSdataに格納
059set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)
060if (item 2 of listResponse) = (missing value) then
061  log "正常処理"
062  set ocidReadData to (item 1 of listResponse)
063else if (item 2 of listResponse) ≠ (missing value) then
064  log (item 2 of listResponse)'s code() as text
065  log (item 2 of listResponse)'s localizedDescription() as text
066  return "XML エラーしました"
067end if
068
069### 【6】XMLDOCにして
070set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML)
071set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error| :(reference)
072if (item 2 of listResponse) = (missing value) then
073  log "正常処理"
074else if (item 2 of listResponse) ≠ (missing value) then
075  log (item 2 of listResponse)'s code() as text
076  log (item 2 of listResponse)'s localizedDescription() as text
077  log "NSXMLDocumentエラー 警告がありました"
078end if
079set ocidXMLDoc to (item 1 of listResponse)
080### 【7】longitude
081set ocidRoot to ocidXMLDoc's rootDocument()
082set listResponse to (ocidRoot's nodesForXPath:("//lon") |error| :(reference))
083if (item 2 of listResponse) = (missing value) then
084  log "正常処理"
085else if (item 2 of listResponse) ≠ (missing value) then
086  log (item 2 of listResponse)'s code() as text
087  log (item 2 of listResponse)'s localizedDescription() as text
088  log "nodesForXPathエラー 警告がありました"
089end if
090set ocidElementValue to (item 1 of listResponse)'s firstObject()
091set strLon to ocidElementValue's stringValue() as text
092### 【8】Latitude
093set listResponse to (ocidRoot's nodesForXPath:("//lat") |error| :(reference))
094if (item 2 of listResponse) = (missing value) then
095  log "正常処理"
096else if (item 2 of listResponse) ≠ (missing value) then
097  log (item 2 of listResponse)'s code() as text
098  log (item 2 of listResponse)'s localizedDescription() as text
099  log "nodesForXPathエラー 警告がありました"
100end if
101set ocidElementValue to (item 1 of listResponse)'s firstObject()
102set strLat to ocidElementValue's stringValue() as text
103
104(*
105###MapURL
106set strMapURL to ("https://maps.apple.com/?ll=" & strLat & "," & strLon & "") as text
107###NabiTime
108set strMapURL to ("https://www.navitime.co.jp/maps/aroundResult?lat=" & strLat & "&lon=" & strLon & "&type=station&radius=2000") as text
109###
110set strMapURL to ("https://map.yahoo.co.jp/place?lat=" & strLat & "&lon=" & strLon & "&zoom=15&maptype=twoTones")
111###
112set strMapURL to ("https://maps.gsi.go.jp/vector/#15/" & strLat & "/" & strLon & "/&ls=vstd&disp=1&d=l")
113###
114set strMapURL to ("https://www.jma.go.jp/bosai/nowc/#lat:" & strLat & "/lon:" & strLon & "/zoom:15/")
115*)
116###GoogleURL
117set strMapURL to ("https://www.google.com/maps/@" & strLat & "," & strLon & ",17z?entry=ttu") as text
118##
119set strDefaultAnswer to (strIP & "\n" & strMapURL & "\n")
120
121##############################
122#####ダイアログ
123##############################
124##前面に出す
125set strName to (name of current application) as text
126if strName is "osascript" then
127  tell application "Finder" to activate
128else
129  tell current application to activate
130end if
131###アイコンパス
132set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
133set strMes to ("戻り値です") as text
134set recordResult to (display dialog strMes with title "選んでください" default answer strDefaultAnswer buttons {"IPアドレスをクリップボードにコピー", "キャンセル", "位置情報を開く"} default button "位置情報を開く" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
135###クリップボードコピー
136if button returned of recordResult is "位置情報を開く" then
137  tell application "Finder"
138    
139    open location strMapURL
140  end tell
141end if
142
143###クリップボードコピー
144if button returned of recordResult is "IPアドレスをクリップボードにコピー" then
145  set strText to (text returned of recordResult) as text
146  try
147    ####ペーストボード宣言
148    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
149    set ocidText to (refMe's NSString's stringWithString:(strIP))
150    appPasteboard's clearContents()
151    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
152  on error
153    tell application "Finder"
154      set the clipboard to strIP as text
155    end tell
156  end try
157end if
158
AppleScriptで生成しました

|

地名検索を修正しました

https://github.com/force4u/AppleScript/blob/main/Script%20Menu/Documents/%E5%9C%B0%E5%90%8D%E6%A4%9C%E7%B4%A2/%E5%9C%B0%E5%90%8D%E6%A4%9C%E7%B4%A2.applescript

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


##通常
set strHostName to ("news.yahoo.co.jp") as text

try
  ##オプリョン -c 1 1回だけ送信 -t 2 タイムアウト2秒
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try

##名前解決できない場合
set strHostName to ("bahoo.co.jp") as text

try
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try

##pingに応答しないケース
set strHostName to ("yahoo.hatena.com") as text

try
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try



|

駅名検索修正

リンクにGoogleMapを追加しました https://github.com/force4u/AppleScript/tree/main/Script%20Menu/Documents/ekidata.jp%E9%A7%85%E5%90%8D%E6%A4%9C%E7%B4%A2

|

[Map]Chromeで開く

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application



tell application "Maps"
    activate
end tell

set strURL to ""
doCopyMap()
set strURL to (the clipboard) as text

################################
######ペーストボードを取得
################################
##set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
#####テキストとして受け取る
##set ocidReadPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
##set strURL to ocidReadPasteboardTypeString as text

################################################
###### URL部分
################################################
set coidURLStr to refMe's NSString's stringWithString:strURL
set coidURL to refMe's NSURL's URLWithString:coidURLStr
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:coidURL resolvingAgainstBaseURL:false



################################################
###### ホスト名取り出し
################################################
set ocidHostName to ocidComponents's |host|()
set strHostName to ocidHostName as text
####処理分岐するなら
if strHostName contains "maps.apple.com" then
    log "AppleMap"
    set ocidGeoLLArray to doGetLLappleMap(coidURL)
    set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
else
    log "他サービス"
    return "緯度経度値を取得できません"
end if

log strLatitude
log strLongitude

set strURL to "https://www.google.com/maps/@" & strLatitude & "," & strLongitude & ",17z/"

################################################
###### ブラウザで開く
################################################

tell application "Google Chrome"
    activate
    set URL of active tab of front window to strURL
end tell



################################################
###### サブルーチン
################################################

###################################################
###AppleMap処理
to doGetLLappleMap(argURL)
    
    ################################################
    ###### URLをコンポーネントに
    ################################################
    set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:argURL resolvingAgainstBaseURL:false
    
    ################################################
    ###### コンポーネントからクエリーを取り出し
    ################################################
    set ocidURLQueryArray to ocidComponents's queryItems()
    
    ################################################
    ###### クエリーをレコードに
    ################################################
    ###格納用の可変ディクショナリ
    set ocidQueryDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
    ###クエリーの数だけ繰り返し
    repeat with itemURLQueryArray in ocidURLQueryArray
        ###KEY
        set ocidQueryKey to itemURLQueryArray's |name|()
        ####値
        set ocidQueryValue to itemURLQueryArray's value()
        ####ディクショナリに格納
        (ocidQueryDict's setObject:ocidQueryValue forKey:ocidQueryKey)
    end repeat
    
    ################################################
    ######  キーで値を取り出す
    ################################################
    ###緯度経度取得
    set ocidGeoLL to (ocidQueryDict's valueForKey:"ll")
    ###カンマで区切ってArrayにして
    set ocidGeoLLArray to ocidGeoLL's componentsSeparatedByString:","
    
    ###戻り値はArrayのまま戻す
    return ocidGeoLLArray
    
    
    (*     ##値を取り出す時は
    set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
    *)
    
end doGetLLappleMap


###################################################
### Mapのコピー処理


to doCopyMap()
    tell application "System Events"
        launch
    end tell
    tell application "Maps"
        activate
    end tell
    try
        tell application "System Events"
            tell process "Maps"
                ##    get every menu bar
                tell menu bar 1
                    ##    get every menu bar item
                    tell menu bar item "編集"
                        ##    get every menu bar item
                        tell menu "編集"
                            ##    get every menu item
                            tell menu item "リンクをコピー"
                                click
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
        tell application "System Events"
            tell process "Maps"
                ##    get every menu bar
                tell menu bar 1
                    ##    get every menu bar item
                    tell menu bar item "編集"
                        ##    get every menu bar item
                        tell menu "編集"
                            ##    get every menu item
                            tell menu item "リンクをコピー"
                                click
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    on error
        try
            tell application "System Events"
                tell process "Maps"
                    get every menu bar
                    tell menu bar 1
                        get every menu bar item
                        tell menu bar item "編集"
                            get every menu bar item
                            tell menu "編集"
                                get every menu item
                                tell menu item "コピー"
                                    click
                                end tell
                            end tell
                        end tell
                    end tell
                end tell
            end tell
            tell application "System Events"
                tell process "Maps"
                    get every menu bar
                    tell menu bar 1
                        get every menu bar item
                        tell menu bar item "編集"
                            get every menu bar item
                            tell menu "編集"
                                get every menu item
                                tell menu item "コピー"
                                    click
                                end tell
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end try
    end try
end doCopyMap

|

[map]URLの分解 緯度経緯の取得

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application



################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
#####テキストとして受け取る
set ocidReadPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)


set strURL to "https://maps.apple.com/?ll=35.658558,139.745504"

set strURL to "https://www.google.com/maps/@35.6585805,139.7454329,17z/"


################################################
###### URL部分
################################################
set coidURLStr to refMe's NSString's stringWithString:strURL
set coidURL to refMe's NSURL's URLWithString:coidURLStr
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:coidURL resolvingAgainstBaseURL:false



################################################
###### ホスト名取り出し
################################################
set ocidHostName to ocidComponents's |host|()
set strHostName to ocidHostName as text
####処理分岐するなら
if strHostName contains "maps.apple.com" then
    log "AppleMap"
    set ocidGeoLLArray to doGetLLappleMap(coidURL)
    set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
else if strHostName contains "google.com" then
    log "GoogleMap"
    set ocidGeoLLArray to doGetLLgooleMap(coidURL)
    set strLatitude to (ocidGeoLLArray's objectAtIndex:1) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:2) as text
else
    log "他サービス"
    return "緯度経度値を取得できません"
end if

log strLatitude
log strLongitude



################################################
###### サブルーチン
################################################

###################################################
###Googlemap処理

to doGetLLgooleMap(argURL)
    
    ################################################
    ###### URLをコンポーネントに
    ################################################
    set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:argURL resolvingAgainstBaseURL:false
    
    ################################################
    ###### パスを取り出し
    ################################################
    set ocidPathComponent to ocidComponents's |path|()
    ###格納用の可変Array
    set ocidPathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
    ###カンマで区切ってArrayにして
    set ocidPathArray to ocidPathComponent's componentsSeparatedByString:"/"
    ###クエリーの数だけ繰り返し
    repeat with itemPathArray in ocidPathArray
        set strItemPathArray to itemPathArray as text
        ###対象のパスアイテムを取得
        if strItemPathArray starts with "@" then
            set strGeoLL to strItemPathArray as text
        end if
    end repeat
    ################################################
    ###### 必要なデータのみ取り出す
    ################################################
    #####区切り文字指定
    set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"@,z"
    ###テキストを戻して
    set ocidGeoLL to refMe's NSString's stringWithString:strGeoLL
    ###区切り文字でリストに
    set ocidGeoLLArray to ocidGeoLL's componentsSeparatedByCharactersInSet:ocidLineEndChrSet
    
    ###戻り値はArrayのまま戻す
    return ocidGeoLLArray
    (* 値を取り出す時は
        ##値を取り出す
    set strLatitude to (ocidGeoLLArray's objectAtIndex:1) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:2) as text
    *)
end doGetLLgooleMap

###################################################
###AppleMap処理
to doGetLLappleMap(argURL)
    
    ################################################
    ###### URLをコンポーネントに
    ################################################
    set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:argURL resolvingAgainstBaseURL:false
    
    ################################################
    ###### コンポーネントからクエリーを取り出し
    ################################################
    set ocidURLQueryArray to ocidComponents's queryItems()
    
    ################################################
    ###### クエリーをレコードに
    ################################################
    ###格納用の可変ディクショナリ
    set ocidQueryDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
    ###クエリーの数だけ繰り返し
    repeat with itemURLQueryArray in ocidURLQueryArray
        ###KEY
        set ocidQueryKey to itemURLQueryArray's |name|()
        ####値
        set ocidQueryValue to itemURLQueryArray's value()
        ####ディクショナリに格納
        (ocidQueryDict's setObject:ocidQueryValue forKey:ocidQueryKey)
    end repeat
    
    ################################################
    ######  キーで値を取り出す
    ################################################
    ###緯度経度取得
    set ocidGeoLL to (ocidQueryDict's valueForKey:"ll")
    ###カンマで区切ってArrayにして
    set ocidGeoLLArray to ocidGeoLL's componentsSeparatedByString:","
    
    ###戻り値はArrayのまま戻す
    return ocidGeoLLArray
    
    
    (*     ##値を取り出す時は
    set strLatitude to (ocidGeoLLArray's objectAtIndex:0) as text
    set strLongitude to (ocidGeoLLArray's objectAtIndex:1) as text
    *)
    
end doGetLLappleMap


|

[Maps]Mapの場所をChromeで開き直す


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009##自分環境がos12なので2.8にしているだけです
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "CoreImage"
013use scripting additions
014
015property objMe : a reference to current application
016property objNSString : a reference to objMe's NSString
017property objNSURL : a reference to objMe's NSURL
018##
019property objNSDictionary : a reference to objMe's NSDictionary
020property objNSMutableDictionary : a reference to objMe's NSMutableDictionary
021property objNSArray : a reference to objMe's NSArray
022property objNSMutableArray : a reference to objMe's NSMutableArray
023
024
025############################
026###コピーのサブへ
027doCopyMap()
028############################
029###クリップボードからURLを取得する
030tell application "Finder"
031set theURL to (the clipboard) as text
032end tell
033set strURL to theURL as text
034############################
035##値がコピー出来なかったときエラーになるので
036##ここはトライ
037try
038####################################
039###URLをNSURLに格納
040set ocidURL to objNSURL's alloc's initWithString:strURL
041log className() of ocidURL as text
042--> NSURL
043####################################
044###クエリー部を取り出し
045set ocidQueryUrl to ocidURL's query
046log className() of ocidQueryUrl as text
047--> __NSCFString
048log ocidQueryUrl as text
049on error
050###エラーしたらコピー取り直し
051tell application "System Events"
052tell process "Maps"
053##get every menu bar
054tell menu bar 1
055##get every menu bar item
056tell menu bar item "編集"
057##get every menu bar item
058tell menu "編集"
059##get every menu item
060tell menu item "リンクをコピー"
061click
062end tell
063end tell
064end tell
065end tell
066end tell
067end tell
068tell application "Finder"
069set theURL to (the clipboard) as text
070end tell
071####################################
072###URLをNSURLに格納
073set ocidURL to objNSURL's alloc's initWithString:strURL
074log className() of ocidURL as text
075--> NSURL
076####################################
077###クエリー部を取り出し
078set ocidQueryUrl to ocidURL's query
079log className() of ocidQueryUrl as text
080--> __NSCFString
081log ocidQueryUrl as text
082end try
083
084####################################
085###取り出したクエリを&を区切り文字でリストに
086set ocidArrayComponent to (ocidQueryUrl's componentsSeparatedByString:"&")
087log className() of ocidArrayComponent as text
088--> __NSArrayM
089log ocidArrayComponent as list
090
091####################################
092####可変レコードを作成
093set ocidRecordQuery to objNSMutableDictionary's alloc()'s init()
094####値が空のレコードを定義
095set recordQuery to {t:"", q:"", address:"", near:"", ll:"", z:"18", spn:"", saddr:"", daddr:"", dirflg:"", sll:"", sspn:"", ug:""} as record
096####↑の定義で値が空の可変レコードを作成
097set ocidRecordQuery to objNSMutableDictionary's dictionaryWithDictionary:recordQuery
098####################################
099###ここからクエリー分繰り返し
100repeat with objArrayComponent in ocidArrayComponent
101#####渡されたクエリーを=を境に分割してArray=リストにする
102set ocidArrayFirstObject to (objArrayComponent's componentsSeparatedByString:"=")
103
104####まずは順番に キー と 値 で格納
105set ocidKey to item 1 of ocidArrayFirstObject
106log ocidKey as text
107
108set ocidValue to item 2 of ocidArrayFirstObject
109log ocidValue as text
110
111#########位置情報 緯度 経度
112if (ocidKey as text) = "ll" then
113set ll of ocidRecordQuery to ocidValue
114####カンマでわけて緯度 経度に
115set ocidArrayLL to (ocidValue's componentsSeparatedByString:",")
116log ocidArrayLL as list
117###最初の項目
118set ocidLatitude to item 1 of ocidArrayLL
119log "Latitude:" & ocidLatitude as text
120###あとの項目
121set ocidLongitude to item 2 of ocidArrayLL
122log "Longitude:" & ocidLongitude as text
123
124#########Address String 住所
125else if (ocidKey as text) = "address" then
126set address of ocidRecordQuery to ocidValue
127set ocidAddEnc to ocidValue's stringByRemovingPercentEncoding
128log "AddressString:" & ocidAddEnc as text
129
130#########The zoom level. 高さ方向
131else if (ocidKey as text) = "z" then
132set z of ocidRecordQuery to ocidValue
133set ocidZoomValue to ocidValue
134log "ZoomValue:" & ocidZoomValue
135
136#########マップビュー
137else if (ocidKey as text) = "t" then
138set t of ocidRecordQuery to ocidValue
139set ocidMapType to ocidValue
140log "MapType:" & ocidMapType
141(*
142m (standard view)
143k (satellite view)
144h (hybrid view)
145r (transit view)
146goole
147map_action=pano
148map_action=map
149basemap=satellite
150terrain
151roadmap
152*)
153####################################
154#########dirflg 移動方法
155else if (ocidKey as text) = "dirflg" then
156set dirflg of ocidRecordQuery to ocidValue
157set ocidDirflgType to ocidValue
158log "DirflgType:" & ocidDirflgType
159(*
160d (by car)
161w (by foot)
162r (by public transit)
163
164goole
165travelmode=driving
166walking
167transit
168*)
169
170#########Dirflg Parameters 出発点
171else if (ocidKey as text) = "saddr" then
172set saddr of ocidRecordQuery to ocidValue
173set strSaddrEnc to ocidValue as text
174set ocidSaddrEnc to ocidValue's stringByRemovingPercentEncoding
175log "StartingPoint:" & ocidSaddrEnc as text
176
177#########Destination 到着店
178else if (ocidKey as text) = "daddr" then
179set daddr of ocidRecordQuery to ocidValue
180set strDaddrEnc to ocidValue as text
181set ocidDaddrEnc to ocidValue's stringByRemovingPercentEncoding
182log "DestinationPoint:" & ocidDaddrEnc as text
183
184#########Search Query 検索語句
185else if (ocidKey as text) = "q" then
186set q of ocidRecordQuery to ocidValue
187set strRecordQuery to ocidValue as text
188set ocidSearchQueryEnc to ocidValue's stringByRemovingPercentEncoding
189log "SearchQuery" & ocidSearchQueryEnc as text
190
191####################################
192#########語句検索時の周辺情報の有無による分岐
193
194else if (ocidKey as text) = "sll" then
195set sll of ocidRecordQuery to ocidValue
196####カンマでわけて緯度 経度に
197set ocidSearchArrayLL to (ocidValue's componentsSeparatedByString:",")
198log ocidSearchArrayLL as list
199####最初の項目
200set ocidNearLatitude to item 1 of ocidSearchArrayLL
201log "NearLatitude:" & ocidNearLatitude as text
202####あとの項目
203set ocidNearLongitude to item 2 of ocidSearchArrayLL
204log "NearNearLongitude:" & ocidNearLongitude as text
205
206else if (ocidKey as text) = "spn" then
207####周囲情報の範囲
208set spn of ocidRecordQuery to ocidValue
209
210
211####################################
212#########during search周辺 位置情報 緯度 経度
213
214else if (ocidKey as text) = "near" then
215set near of ocidRecordQuery to ocidValue
216####カンマでわけて緯度 経度に
217set ocidNearArrayLL to (ocidValue's componentsSeparatedByString:",")
218log ocidNearArrayLL as list
219###最初の項目
220set ocidNearLatitude to item 1 of ocidNearArrayLL
221log "NearLatitude:" & ocidNearLatitude as text
222###あとの項目
223set ocidNearLongitude to item 2 of ocidNearArrayLL
224log "NearNearLongitude:" & ocidNearLongitude as text
225
226####################################
227#########ガイド時のug
228else if (ocidKey as text) = "ug" then
229set ug of ocidRecordQuery to ocidValue
230end if
231end repeat
232
233
234##############################################
235###GoogleMap用の小数点以下の桁揃え
236set theLatitude to (ocidLatitude as text)
237set AppleScript's text item delimiters to "."
238set listLatitude to every text item of theLatitude as list
239set AppleScript's text item delimiters to ""
240set strLatitudeInt to text item 1 of listLatitude as text
241set strLatitudeDecimal to text item 2 of listLatitude as text
242set strLatitudeDecimal to (text 1 through 7 of (strLatitudeDecimal & "000000000")) as text
243set theLatitude to ("" & strLatitudeInt & "." & strLatitudeDecimal & "")
244
245set theLongitude to (ocidLongitude as text)
246set AppleScript's text item delimiters to "."
247set listLongitude to every text item of theLongitude as list
248set AppleScript's text item delimiters to ""
249set strLongitudeInt to text item 1 of listLongitude as text
250set strLongitudeDecimal to text item 2 of listLongitude as text
251set strLongitudeDecimal to (text 1 through 7 of (strLongitudeDecimal & "000000000")) as text
252set theLongitude to ("" & strLongitudeInt & "." & strLongitudeDecimal & "")
253
254set theGooglemapParts to ("@" & theLatitude & "," & theLongitude & "," & (z of ocidRecordQuery as text) & "z")
255
256set theChl to ("https://www.google.com/maps/" & theGooglemapParts & "") as text
257
258
259
260tell application "Google Chrome"
261activate
262make new window
263open location theChl
264end tell
265
266
267return true
268
269
270to doCopyMap()
271tell application "System Events"
272launch
273end tell
274tell application "Maps"
275activate
276end tell
277try
278tell application "System Events"
279tell process "Maps"
280##get every menu bar
281tell menu bar 1
282##get every menu bar item
283tell menu bar item "編集"
284##get every menu bar item
285tell menu "編集"
286##get every menu item
287tell menu item "リンクをコピー"
288click
289end tell
290end tell
291end tell
292end tell
293end tell
294end tell
295on error
296tell application "System Events"
297tell process "Maps"
298get every menu bar
299tell menu bar 1
300get every menu bar item
301tell menu bar item "編集"
302get every menu bar item
303tell menu "編集"
304get every menu item
305tell menu item "コピー"
306click
307end tell
308end tell
309end tell
310end tell
311end tell
312end tell
313end try
314end doCopyMap
AppleScriptで生成しました

|

[Maps]Mapの場所をSafariで開き直す


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009##自分環境がos12なので2.8にしているだけです
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "CoreImage"
013use scripting additions
014
015property objMe : a reference to current application
016property objNSString : a reference to objMe's NSString
017property objNSURL : a reference to objMe's NSURL
018##
019property objNSDictionary : a reference to objMe's NSDictionary
020property objNSMutableDictionary : a reference to objMe's NSMutableDictionary
021property objNSArray : a reference to objMe's NSArray
022property objNSMutableArray : a reference to objMe's NSMutableArray
023
024
025############################
026###コピーのサブへ
027doCopyMap()
028############################
029###クリップボードからURLを取得する
030tell application "Finder"
031set theURL to (the clipboard) as text
032end tell
033set strURL to theURL as text
034############################
035##値がコピー出来なかったときエラーになるので
036##ここはトライ
037try
038####################################
039###URLをNSURLに格納
040set ocidURL to objNSURL's alloc's initWithString:strURL
041log className() of ocidURL as text
042--> NSURL
043####################################
044###クエリー部を取り出し
045set ocidQueryUrl to ocidURL's query
046log className() of ocidQueryUrl as text
047--> __NSCFString
048log ocidQueryUrl as text
049on error
050###エラーしたらコピー取り直し
051tell application "System Events"
052tell process "Maps"
053##get every menu bar
054tell menu bar 1
055##get every menu bar item
056tell menu bar item "編集"
057##get every menu bar item
058tell menu "編集"
059##get every menu item
060tell menu item "リンクをコピー"
061click
062end tell
063end tell
064end tell
065end tell
066end tell
067end tell
068tell application "Finder"
069set theURL to (the clipboard) as text
070end tell
071####################################
072###URLをNSURLに格納
073set ocidURL to objNSURL's alloc's initWithString:strURL
074log className() of ocidURL as text
075--> NSURL
076####################################
077###クエリー部を取り出し
078set ocidQueryUrl to ocidURL's query
079log className() of ocidQueryUrl as text
080--> __NSCFString
081log ocidQueryUrl as text
082end try
083
084####################################
085###取り出したクエリを&を区切り文字でリストに
086set ocidArrayComponent to (ocidQueryUrl's componentsSeparatedByString:"&")
087log className() of ocidArrayComponent as text
088--> __NSArrayM
089log ocidArrayComponent as list
090
091####################################
092####可変レコードを作成
093set ocidRecordQuery to objNSMutableDictionary's alloc()'s init()
094####値が空のレコードを定義
095set recordQuery to {t:"", q:"", address:"", near:"", ll:"", z:"18", spn:"", saddr:"", daddr:"", dirflg:"", sll:"", sspn:"", ug:""} as record
096####↑の定義で値が空の可変レコードを作成
097set ocidRecordQuery to objNSMutableDictionary's dictionaryWithDictionary:recordQuery
098####################################
099###ここからクエリー分繰り返し
100repeat with objArrayComponent in ocidArrayComponent
101#####渡されたクエリーを=を境に分割してArray=リストにする
102set ocidArrayFirstObject to (objArrayComponent's componentsSeparatedByString:"=")
103
104####まずは順番に キー と 値 で格納
105set ocidKey to item 1 of ocidArrayFirstObject
106log ocidKey as text
107
108set ocidValue to item 2 of ocidArrayFirstObject
109log ocidValue as text
110
111#########位置情報 緯度 経度
112if (ocidKey as text) = "ll" then
113set ll of ocidRecordQuery to ocidValue
114####カンマでわけて緯度 経度に
115set ocidArrayLL to (ocidValue's componentsSeparatedByString:",")
116log ocidArrayLL as list
117###最初の項目
118set ocidLatitude to item 1 of ocidArrayLL
119log "Latitude:" & ocidLatitude as text
120###あとの項目
121set ocidLongitude to item 2 of ocidArrayLL
122log "Longitude:" & ocidLongitude as text
123
124#########Address String 住所
125else if (ocidKey as text) = "address" then
126set address of ocidRecordQuery to ocidValue
127set ocidAddEnc to ocidValue's stringByRemovingPercentEncoding
128log "AddressString:" & ocidAddEnc as text
129
130#########The zoom level. 高さ方向
131else if (ocidKey as text) = "z" then
132set z of ocidRecordQuery to ocidValue
133set ocidZoomValue to ocidValue
134log "ZoomValue:" & ocidZoomValue
135
136#########マップビュー
137else if (ocidKey as text) = "t" then
138set t of ocidRecordQuery to ocidValue
139set ocidMapType to ocidValue
140log "MapType:" & ocidMapType
141(*
142m (standard view)
143k (satellite view)
144h (hybrid view)
145r (transit view)
146goole
147map_action=pano
148map_action=map
149basemap=satellite
150terrain
151roadmap
152*)
153####################################
154#########dirflg 移動方法
155else if (ocidKey as text) = "dirflg" then
156set dirflg of ocidRecordQuery to ocidValue
157set ocidDirflgType to ocidValue
158log "DirflgType:" & ocidDirflgType
159(*
160d (by car)
161w (by foot)
162r (by public transit)
163
164goole
165travelmode=driving
166walking
167transit
168*)
169
170#########Dirflg Parameters 出発点
171else if (ocidKey as text) = "saddr" then
172set saddr of ocidRecordQuery to ocidValue
173set strSaddrEnc to ocidValue as text
174set ocidSaddrEnc to ocidValue's stringByRemovingPercentEncoding
175log "StartingPoint:" & ocidSaddrEnc as text
176
177#########Destination 到着店
178else if (ocidKey as text) = "daddr" then
179set daddr of ocidRecordQuery to ocidValue
180set strDaddrEnc to ocidValue as text
181set ocidDaddrEnc to ocidValue's stringByRemovingPercentEncoding
182log "DestinationPoint:" & ocidDaddrEnc as text
183
184#########Search Query 検索語句
185else if (ocidKey as text) = "q" then
186set q of ocidRecordQuery to ocidValue
187set strRecordQuery to ocidValue as text
188set ocidSearchQueryEnc to ocidValue's stringByRemovingPercentEncoding
189log "SearchQuery" & ocidSearchQueryEnc as text
190
191####################################
192#########語句検索時の周辺情報の有無による分岐
193
194else if (ocidKey as text) = "sll" then
195set sll of ocidRecordQuery to ocidValue
196####カンマでわけて緯度 経度に
197set ocidSearchArrayLL to (ocidValue's componentsSeparatedByString:",")
198log ocidSearchArrayLL as list
199####最初の項目
200set ocidNearLatitude to item 1 of ocidSearchArrayLL
201log "NearLatitude:" & ocidNearLatitude as text
202####あとの項目
203set ocidNearLongitude to item 2 of ocidSearchArrayLL
204log "NearNearLongitude:" & ocidNearLongitude as text
205
206else if (ocidKey as text) = "spn" then
207####周囲情報の範囲
208set spn of ocidRecordQuery to ocidValue
209
210
211####################################
212#########during search周辺 位置情報 緯度 経度
213
214else if (ocidKey as text) = "near" then
215set near of ocidRecordQuery to ocidValue
216####カンマでわけて緯度 経度に
217set ocidNearArrayLL to (ocidValue's componentsSeparatedByString:",")
218log ocidNearArrayLL as list
219###最初の項目
220set ocidNearLatitude to item 1 of ocidNearArrayLL
221log "NearLatitude:" & ocidNearLatitude as text
222###あとの項目
223set ocidNearLongitude to item 2 of ocidNearArrayLL
224log "NearNearLongitude:" & ocidNearLongitude as text
225
226####################################
227#########ガイド時のug
228else if (ocidKey as text) = "ug" then
229set ug of ocidRecordQuery to ocidValue
230
231end if
232
233end repeat
234
235
236##############################################
237###GoogleMap用の小数点以下の桁揃え
238set theLatitude to (ocidLatitude as text)
239set AppleScript's text item delimiters to "."
240set listLatitude to every text item of theLatitude as list
241set AppleScript's text item delimiters to ""
242set strLatitudeInt to text item 1 of listLatitude as text
243set strLatitudeDecimal to text item 2 of listLatitude as text
244set strLatitudeDecimal to (text 1 through 7 of (strLatitudeDecimal & "000000000")) as text
245set theLatitude to ("" & strLatitudeInt & "." & strLatitudeDecimal & "")
246
247set theLongitude to (ocidLongitude as text)
248set AppleScript's text item delimiters to "."
249set listLongitude to every text item of theLongitude as list
250set AppleScript's text item delimiters to ""
251set strLongitudeInt to text item 1 of listLongitude as text
252set strLongitudeDecimal to text item 2 of listLongitude as text
253set strLongitudeDecimal to (text 1 through 7 of (strLongitudeDecimal & "000000000")) as text
254set theLongitude to ("" & strLongitudeInt & "." & strLongitudeDecimal & "")
255
256set theGooglemapParts to ("@" & theLatitude & "," & theLongitude & "," & (z of ocidRecordQuery as text) & "z")
257
258set theChl to ("https://www.google.com/maps/" & theGooglemapParts & "") as text
259
260
261
262tell application "Safari"
263activate
264make new document with properties {name:"maps.google.co.jp"}
265tell window 1
266open location theChl
267end tell
268
269end tell
270
271
272return true
273
274
275to doCopyMap()
276tell application "System Events"
277launch
278end tell
279tell application "Maps"
280activate
281end tell
282try
283tell application "System Events"
284tell process "Maps"
285##get every menu bar
286tell menu bar 1
287##get every menu bar item
288tell menu bar item "編集"
289##get every menu bar item
290tell menu "編集"
291##get every menu item
292tell menu item "リンクをコピー"
293click
294end tell
295end tell
296end tell
297end tell
298end tell
299end tell
300on error
301tell application "System Events"
302tell process "Maps"
303get every menu bar
304tell menu bar 1
305get every menu bar item
306tell menu bar item "編集"
307get every menu bar item
308tell menu "編集"
309get every menu item
310tell menu item "コピー"
311click
312end tell
313end tell
314end tell
315end tell
316end tell
317end tell
318end try
319end doCopyMap
AppleScriptで生成しました

|

[Map]位置情報をQRコードにするv2

[Map]位置情報をQRコードにする
https://quicktimer.cocolog-nifty.com/icefloe/2022/04/post-af06ee.html
こちらのアップデートです。
現時点ではここまでにしょうか…と
NSMutableDictionaryを使ってみたものの…
処理は煩雑になるわ、テキスト形式に戻すのに記述が増えるは…で
あまり良い事なかった。
1:便利な機能は使っていく
2:記述にこだわらず、可読性や処理のシンプルさが結局後で効く

こだわらずに、最適と思われる方法にすればよかったなぁ…遠い目
(読みにくいわ…修正しにくいわで、最低な出来だと…トホホ)



AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007#                       com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009##自分環境がos12なので2.8にしているだけです
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "CoreImage"
013use scripting additions
014
015property objMe : a reference to current application
016property objNSString : a reference to objMe's NSString
017property objNSURL : a reference to objMe's NSURL
018##
019property objNSDictionary : a reference to objMe's NSDictionary
020property objNSMutableDictionary : a reference to objMe's NSMutableDictionary
021property objNSArray : a reference to objMe's NSArray
022property objNSMutableArray : a reference to objMe's NSMutableArray
023
024set objFileManager to objMe's NSFileManager's defaultManager()
025
026###リセット
027set theURL to "" as text
028set ocidNSInlineData to "" as text
029set ocidNSBitmapImagePadRep to "" as text
030
031tell application "Finder"
032  set the clipboard to the theURL as string
033end tell
034
035objMe's NSLog("■:osascript:Start Script Geo2Qr4Map")
036
037###コピーのサブへ
038doCopyMap()
039
040log "theURL:" & theURL
041objMe's NSLog("■:osascript:コピー OK" & theURL & "")
042###クリップボードからURLを取得する
043tell application "Finder"
044  set theURL to (the clipboard) as text
045end tell
046delay 0.5
047
048##################################################
049objMe's NSLog("■:osascript: theURL" & theURL & "")
050set strURL to theURL as text
051
052##################################################
053if strURL is "" then
054  ###URLの取得に失敗しているパターン
055  set aliasIconPath to POSIX file "/System/Applications/Maps.app/Contents/Resources/AppIcon.icns" as alias
056  set strDefaultAnswer to "https://maps.apple.com/?ll=35.658558,139.745504" as text
057  try
058    set objResponse to (display dialog "URLの取得に失敗しました" with title "QRテキスト" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
059  on error
060    log "エラーしました"
061    return
062  end try
063  if true is equal to (gave up of objResponse) then
064    return "時間切れですやりなおしてください"
065  end if
066  if "OK" is equal to (button returned of objResponse) then
067    set strURL to (text returned of objResponse) as text
068  else
069    return "キャンセル"
070  end if
071end if
072
073############################
074##値がコピー出来なかったときエラーになるので
075##ここはトライ
076try
077  ####################################
078  ###URLをNSURLに格納
079  set ocidURL to objNSURL's alloc's initWithString:strURL
080  log className() of ocidURL as text
081  --> NSURL
082  ####################################
083  ###クエリー部を取り出し
084  set ocidQueryUrl to ocidURL's query
085  log className() of ocidQueryUrl as text
086  --> __NSCFString
087  log ocidQueryUrl as text
088on error
089###エラーしたらコピー取り直し
090  doCopyMap()
091  tell application "Finder"
092    set theURL to (the clipboard) as text
093  end tell
094  ####################################
095  ###URLをNSURLに格納
096  set ocidURL to objNSURL's alloc's initWithString:strURL
097  log className() of ocidURL as text
098  --> NSURL
099  ####################################
100  ###クエリー部を取り出し
101  set ocidQueryUrl to ocidURL's query
102  log className() of ocidQueryUrl as text
103  --> __NSCFString
104  log ocidQueryUrl as text
105end try
106
107####################################
108###取り出したクエリを&を区切り文字でリストに
109set ocidArrayComponent to (ocidQueryUrl's componentsSeparatedByString:"&")
110log className() of ocidArrayComponent as text
111--> __NSArrayM
112log ocidArrayComponent as list
113
114####################################
115####可変レコードを作成
116set ocidRecordQuery to objNSMutableDictionary's alloc()'s init()
117####値が空のレコードを定義
118set recordQuery to {t:"", q:"", address:"", near:"", ll:"", z:"18", spn:"", saddr:"", daddr:"", dirflg:"", sll:"", sspn:"", ug:""} as record
119####↑の定義で値が空の可変レコードを作成
120set ocidRecordQuery to objNSMutableDictionary's dictionaryWithDictionary:recordQuery
121####################################
122###ここからクエリー分繰り返し
123repeat with objArrayComponent in ocidArrayComponent
124  #####渡されたクエリーを=を境に分割してArray=リストにする
125  set ocidArrayFirstObject to (objArrayComponent's componentsSeparatedByString:"=")
126  
127  ####まずは順番に キー と 値 で格納
128  set ocidKey to item 1 of ocidArrayFirstObject
129  log ocidKey as text
130  
131  set ocidValue to item 2 of ocidArrayFirstObject
132  log ocidValue as text
133  
134  #########位置情報 緯度 経度
135  if (ocidKey as text) = "ll" then
136    set ll of ocidRecordQuery to ocidValue
137    ####カンマでわけて緯度 経度に
138    set ocidArrayLL to (ocidValue's componentsSeparatedByString:",")
139    log ocidArrayLL as list
140    ###最初の項目
141    set ocidLatitude to item 1 of ocidArrayLL
142    log "Latitude:" & ocidLatitude as text
143    ###あとの項目
144    set ocidLongitude to item 2 of ocidArrayLL
145    log "Longitude:" & ocidLongitude as text
146    
147    #########Address String  住所
148  else if (ocidKey as text) = "address" then
149    set address of ocidRecordQuery to ocidValue
150    set ocidAddEnc to ocidValue's stringByRemovingPercentEncoding
151    log "AddressString:" & ocidAddEnc as text
152    
153    #########The zoom level. 高さ方向
154  else if (ocidKey as text) = "z" then
155    set z of ocidRecordQuery to ocidValue
156    set ocidZoomValue to ocidValue
157    log "ZoomValue:" & ocidZoomValue
158    
159    #########マップビュー
160  else if (ocidKey as text) = "t" then
161    set t of ocidRecordQuery to ocidValue
162    set ocidMapType to ocidValue
163    log "MapType:" & ocidMapType
164    (*
165    m (standard view)
166    k (satellite view)
167    h (hybrid view)
168    r (transit view)
169    goole
170    map_action=pano
171    map_action=map
172    basemap=satellite
173    terrain
174    roadmap
175    *)
176    ####################################
177    #########dirflg 移動方法  
178  else if (ocidKey as text) = "dirflg" then
179    set dirflg of ocidRecordQuery to ocidValue
180    set ocidDirflgType to ocidValue
181    log "DirflgType:" & ocidDirflgType
182    (*
183    d (by car)
184    w (by foot)
185    r (by public transit)
186    
187    goole
188    travelmode=driving
189    walking
190    transit
191      *)
192    
193    #########Dirflg Parameters 出発点
194  else if (ocidKey as text) = "saddr" then
195    set saddr of ocidRecordQuery to ocidValue
196    set strSaddrEnc to ocidValue as text
197    set ocidSaddrEnc to ocidValue's stringByRemovingPercentEncoding
198    log "StartingPoint:" & ocidSaddrEnc as text
199    
200    #########Destination  到着店
201  else if (ocidKey as text) = "daddr" then
202    set daddr of ocidRecordQuery to ocidValue