指定のURLに対して、recordで指定したパラメータつきURLを生成するAppleScriptです。
REST APIを呼び出すときに使っているため、登場頻度がきわめて高いサブルーチンです。OLD Style AppleScriptの機能の範囲内で書くこともできますが、Cocoaの機能を用いたほうが圧倒的に手軽に書ける好例といえます。
AppleScript名:URLとrecordからパラメータつきURLを生成する v3 |
— Created 2016-04-19 by Takaaki Naganoya — Modified 2016-11-12 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSURLQueryItem : a reference to current application’s NSURLQueryItem property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSURLComponents : a reference to current application’s NSURLComponents set aBaseURL to "https://slack.com/api/auth.test" set aRec to {token:"aaaa", channel:"ぴよまるソフトウェア"} set aRes to retURLwithParams(aBaseURL, aRec) of me –> "https://slack.com/api/auth.test?token=aaaa&channel=%E3%81%B4%E3%82%88%E3%81%BE%E3%82%8B%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2" on retURLwithParams(aBaseURL as string, aRec as record) set aDic to NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set bLen to length of aValList if aLen is not equal to bLen then return false set qList to {} repeat with i from 1 to aLen set aName to (contents of item i of aKeyList) as string set aVal to (contents of item i of aValList) as string set the end of qList to (NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams |
More from my site
(Visited 31 times, 1 visits today)