Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yahoo! テキスト解析APIへExtendScriptを利用してPOSTリクエストを送るサンプル(Mac版InDesign専用)
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