Safariのdo JavaScriptコマンドから各種結果を取得してAppleScriptの各種オブジェクトに変換するAppleScriptです。
真剣にSafari上でdo JavaScriptコマンド経由で値の受け渡しをするときに、さまざまな型のデータをJavaScript側からAppleScript側に渡したいことがあります。
ただ、JavaScript側からのデータの受け渡しはそんなにまじめにやっていなかったので、あらためて方法を調べておきました。
AppleScript名:Safariのdo javascriptから結果をJSONで返してASの各種オブジェクトに変換 |
— – Created by: Takaaki Naganoya – Created on: 2019/09/22 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" tell application "Safari" if (count every document) = 0 then return –String List (1D) set jsRes1 to do JavaScript "var tempVar = [’1’,’2’]; JSON.stringify(tempVar);" in front document set aRes to parseJSONAsList(jsRes1) of me –> {"1", "2"} –Number List (1D) set jsRes2 to do JavaScript "var tempVar = [1,2]; JSON.stringify(tempVar);" in front document set bRes to parseJSONAsList(jsRes2) of me –> {1, 2} –Record set jsRes3 to do JavaScript "var tempVar2 = { name:’Steve Jobs’, age:32, tel:’080-1234-5678’ }; JSON.stringify(tempVar2);" in front document set cRes to parseJSONAsRecord(jsRes3) of me –> {|name|:"Steve Jobs", age:32, tel:"080-1234-5678"} –Records in List set jsRes4 to do JavaScript "var tempVar2 = [{ name:’Steve Jobs’, age:32, tel:’080-1234-5678’ },{ name:’Tim Coo’, age:55, tel:’090-1234-5678’ }] ; JSON.stringify(tempVar2);" in front document set dRes to parseJSONAsList(jsRes4) of me –> {{|name|:"Steve Jobs", age:32, tel:"080-1234-5678"}, {|name|:"Tim Coo", age:55, tel:"090-1234-5678"}} –Number List (2D) set jsRes5 to do JavaScript "var tempVar = [[1,2], [3,4], [5,6]]; JSON.stringify(tempVar);" in front document set eRes to parseJSONAsList(jsRes5) of me –> {{1, 2}, {3, 4}, {5, 6}} end tell on parseJSONAsList(jsRes) set jsonString to current application’s NSString’s stringWithString:jsRes set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) return aJsonDict as list end parseJSONAsList on parseJSONAsRecord(jsRes) set jsonString to current application’s NSString’s stringWithString:jsRes set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) return aJsonDict as record end parseJSONAsRecord |
AppleScript名:Safariのdo javascriptから結果をJSONで返してASの各種オブジェクトに変換 2 |
— – Created by: Takaaki Naganoya – Created on: 2019/09/22 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" tell application "Safari" if (count every document) = 0 then return –String set jsRes1 to do JavaScript "var tempVar = ’1’; tempVar;" in front document –> "1" –Number set jsRes2 to do JavaScript "var tempVar = 1; tempVar;" in front document –> 1.0 end tell |
More from my site
(Visited 127 times, 2 visits today)
AppleScript上でJavaScriptを実行する – AppleScriptの穴 says:
[…] また、「Safariのdo JavaScriptから結果をJSONで返してASの各種オブジェクトに変換」の記事において、AppleScriptからJavaScriptを呼び出して、さまざまな型のデータをJavaScript側から受け取る方法 […]