Skip to content

Instantly share code, notes, and snippets.

@UskeS
Last active October 24, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UskeS/ed1e58e42d95d75e1d09e13a79bfda57 to your computer and use it in GitHub Desktop.
Save UskeS/ed1e58e42d95d75e1d09e13a79bfda57 to your computer and use it in GitHub Desktop.
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