Gender API(https://www.genderapi.io/ja#)のREST APIを呼び出して、氏名から性別を判定するAppleScriptを書いてみました。
本APIは、POST methodで呼び出すものですが、NSURLConnection経由で呼び出すPOST呼び出しのScriptしか書いてありませんでした。NSURLConnectionは同期実行が行えるため、AppleScriptからは呼び出しやすいものですが、Deprecated宣言されてしまっているため、いつ本当になくなるかわかりません(いうても、あちこちで必要なのですぐになくならない気もする)。
# 本Scriptは、間違いなく2026年の意義あるScriptにリストアップされるものです
そこで、NSURLSessionを呼び出すPOST method呼び出しのAppleScriptを書いておいた次第です。ChatGPTに書かせました。
set aPostData to {|name|:”ぴよ まるお”, country:”JP”, askToAI:false, forceToGenderize:false}
のように、氏名を指定して呼び出すと、
{|name|:”ぴよ まるお”, |to|:false, remaining_credits:178, q:”ぴよ まるお”, expires:1.767849716E+9, isHumanName:true, total_names:1, used_credits:1, probability:80, duration:”828ms”, country:”JP”, status:true, gender:”male”}
のように結果が返ってきます。
Gender APIは無料コースが用意されており、1日に200回までの呼び出しの範囲で無料利用できます。本当は、「日本語の氏名から性別を判定できる機械学習モデル」(Classifier)があれば、それを呼び出すだけで済むのですが、ちょっと探したぐらいでは見当たりませんでした。
こういうのは、公共機関が整備して配布すべきもののような気もするのですが……
| AppleScript名:POST method REST API v4.2(NSURLSession)_blog.scptd |
| — – Created by: Takaaki Naganoya – Created on: 2026/01/07 — – Copyright © 2026 Piyomaru Software, All Rights Reserved — use AppleScript version "2.8" use scripting additions use framework "Foundation" use framework "AppKit" property |NSURL| : a reference to current application’s |NSURL| property NSString : a reference to current application’s NSString property NSURLCache : a reference to current application’s NSURLCache property NSURLSession : a reference to current application’s NSURLSession property NSMutableData : a reference to current application’s NSMutableData property NSOperationQueue : a reference to current application’s NSOperationQueue property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSMutableURLRequest : a reference to current application’s NSMutableURLRequest property NSJSONSerialization : a reference to current application’s NSJSONSerialization property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSURLSessionConfiguration : a reference to current application’s NSURLSessionConfiguration property retData : missing value property retCode : 0 property drecF : false property aSession : missing value property aCache : missing value on run –https://www.genderapi.io/ja# にアクセスしてAPI Keyを取得してください set apiKey to "Bearer " & "999zzz99z9999z99999z999z" set reqURLStr to "https://api.genderapi.io/api" set aPostData to {|name|:"長野谷 隆昌", country:"JP", askToAI:false, forceToGenderize:false} set jsonDict to callRestPOSTJSON(reqURLStr, aPostData, 5, apiKey) of me return jsonDict as record –> {status:false, errno:93, errmsg:"query limit reached"}–API使用制限に到達した場合の返答 –> {status:false, errno:94, errmsg:"invalid or missing key"}–API Keyを指定しなかった場合の返答 –> {|name|:"長野谷 隆昌", |to|:false, remaining_credits:178, q:"長野谷 隆昌", expires:1.767849716E+9, isHumanName:true, total_names:1, used_credits:1, probability:80, duration:"828ms", country:"JP", status:true, gender:"male"} end run — POST JSON REST APIを呼び出す on callRestPOSTJSON(reqURLStr as string, aRec as record, timeoutSec as integer, apiKey) set retData to NSMutableData’s alloc()’s init() set retCode to 0 set drecF to false –URL Cache(GETと同じ) set cachePath to (POSIX path of (path to temporary items folder)) & "/Caches/AppleScriptURLCache" set aCache to NSURLCache’s alloc()’s initWithMemoryCapacity:512000 diskCapacity:1024 * 1024 * 5 diskPath:cachePath NSURLCache’s setSharedURLCache:aCache –URL set aURL to |NSURL|’s URLWithString:reqURLStr –POST リクエスト作成 set aRequest to NSMutableURLRequest’s requestWithURL:aURL aRequest’s setHTTPMethod:"POST" aRequest’s setTimeoutInterval:timeoutSec aRequest’s setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type" aRequest’s setValue:apiKey forHTTPHeaderField:"Authorization" aRequest’s setValue:"AppleScript/Cocoa" forHTTPHeaderField:"User-Agent" –JSON化 set jsonData to NSJSONSerialization’s dataWithJSONObject:aRec options:0 |error|:(missing value) aRequest’s setHTTPBody:jsonData –Session set aConfig to NSURLSessionConfiguration’s defaultSessionConfiguration() aConfig’s setURLCache:aCache set aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(NSOperationQueue’s mainQueue()) set aTask to aSession’s dataTaskWithRequest:aRequest aTask’s resume() –delegate終了待ち repeat (1000 * timeoutSec) times if drecF is true then exit repeat delay "0.001" as real end repeat –Session終了 aSession’s finishTasksAndInvalidate() set aSession to missing value –結果パース return my parseSessionResults() end callRestPOSTJSON — delegate: body accumulation on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData retData’s appendData:tmpData end URLSession:dataTask:didReceiveData: — delegate: finished/failed on URLSession:tmpSession task:tmpTask didCompleteWithError:tmpError if tmpError = missing value then set drecF to true else error "POST Failed:" & tmpError end if end URLSession:task:didCompleteWithError: –JSON parse on parseSessionResults() set resStr to NSString’s alloc()’s initWithData:retData encoding:NSUTF8StringEncoding set jsonString to NSString’s stringWithString:resStr set jsonData2 to jsonString’s dataUsingEncoding:NSUTF8StringEncoding set jsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData2 options:0 |error|:(missing value) return jsonDict end parseSessionResults |
