Last active
October 24, 2021 10:20
-
-
Save UskeS/ed1e58e42d95d75e1d09e13a79bfda57 to your computer and use it in GitHub Desktop.
Yahoo! テキスト解析APIへExtendScriptを利用してPOSTリクエストを送るサンプル(Mac版InDesign専用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!String.prototype.surroundQuotes) { | |
String.prototype.surroundQuotes = function(q) { | |
return q + this + q; | |
} | |
} | |
var p = { | |
url: "https://jlp.yahooapis.jp/FuriganaService/V2/furigana", | |
header: { | |
contentType: "application/json", | |
userAgent: "Yahoo AppID: <アプリケーションID>", //ここにClientIDを記述 | |
}, | |
body: { | |
id: "1234", | |
jsonrpc: "2.0", | |
method: "jlp.furiganaservice.furigana", | |
params: { | |
q: "彗星が僕の頭上を翔んだ", //実際にはInDesignからテキストを受け取る | |
grade: "1" | |
} | |
}, | |
}; | |
var scpt = "do shell script " + [ | |
"curl", | |
"-H", | |
("content-type: " + p.header.contentType).surroundQuotes("'"), | |
"-A", | |
p.header.userAgent.surroundQuotes("'"), | |
"-X", | |
"POST", | |
"-d", | |
getParamString(p.body).surroundQuotes("'"), | |
p.url | |
].join(" ").surroundQuotes('"'); | |
var result = eval("(" + app.doScript(scpt, ScriptLanguage.applescriptLanguage) + ")"); | |
function getParamString(obj) { | |
var res = []; | |
for (var key in obj) { | |
if (typeof(obj[key]) === "string") { | |
res.push(key.surroundQuotes('\\"') + ": " + obj[key].surroundQuotes('\\"')); | |
} else { | |
res.push(key.surroundQuotes('\\"') + ": " + getParamString(obj[key])); | |
} | |
} | |
return "{" + res.join(",") + "}"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment