Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: REST API

POST method REST API__Slack_usergroups.list

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチーム内のユーザーグループ一覧を取得する(usergroups.list)AppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_usergroups.list
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/usergroups.list"
set aRec to {token:accessToken, include_disabled:"1", include_count:"1", include_users:"1"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>  {​​​​​usergroups:{}, ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_groups.list

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してユーザーがアクセス可能なプライベートなチャンネル一覧を取得する(groups.list)AppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_groups.list
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/groups.info"
set aRec to {token:accessToken, channel:"C11NJCBU3"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>   {​​​​​error:"channel_not_found", ​​​​​ok:0​​​}

–本Scriptはまだチェックが必要と思われる

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_files.list

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチーム内のファイル一覧を取得する(files.list)AppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_files.list.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/files.list"
set aRec to {token:accessToken, user:"U11PVKT6D"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>  {​​​​​files:{}, ​​​​​paging:{​​​​​​​pages:0, ​​​​​​​count:100, ​​​​​​​total:0, ​​​​​​​page:1​​​​​}, ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy Slack | Leave a comment

POST method REST API__Slack_emoji.list

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチーム向けのカスタム絵文字リストを取得する(emoji.list)AppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_emoji.list.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/emoji.list"
set aRec to {token:accessToken}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>  {​​​​​emoji:{​​​​​​​feelsgood:"https://emoji.slack-edge.com/T11N8QR19/feelsgood/7bcbaa15fa.png", ​​​​​​​rage3:"https://emoji.slack-edge.com/T11N8QR19/rage3/8e11678fbf.png", ​​​​​​​squirrel:"https://emoji.slack-edge.com/T11N8QR19/squirrel/465f40c0e0.png", ​​​​​​​piggy:"https://emoji.slack-edge.com/T11N8QR19/piggy/b7762ee8cd.png", ​​​​​​​beryl:"https://emoji.slack-edge.com/T11N8QR19/beryl/efe1ba7e7a.png", ​​​​​​​dusty_stick:"https://emoji.slack-edge.com/T11N8QR19/dusty_stick/6177a62312.png", ​​​​​​​shipit:"alias:squirrel", ​​​​​​​white_square:"alias:white_large_square", ​​​​​​​hurtrealbad:"https://emoji.slack-edge.com/T11N8QR19/hurtrealbad/b9c3d648e6.png", ​​​​​​​rage2:"https://emoji.slack-edge.com/T11N8QR19/rage2/feaf8897c6.png", ​​​​​​​rube:"https://emoji.slack-edge.com/T11N8QR19/rube/74ef8ef199.png", ​​​​​​​black_square:"alias:black_large_square", ​​​​​​​glitch_crab:"https://emoji.slack-edge.com/T11N8QR19/glitch_crab/db049f1f9c.png", ​​​​​​​finnadie:"https://emoji.slack-edge.com/T11N8QR19/finnadie/08e66eb46d.png", ​​​​​​​rage1:"https://emoji.slack-edge.com/T11N8QR19/rage1/0c3685290c.png", ​​​​​​​troll:"alias:trollface", ​​​​​​​slack:"https://emoji.slack-edge.com/T11N8QR19/slack/5ee0c9bea3.png", ​​​​​​​fu:"https://emoji.slack-edge.com/T11N8QR19/fu/2f615de37f.png", ​​​​​​​neckbeard:"https://emoji.slack-edge.com/T11N8QR19/neckbeard/c8ec7bf188.png", ​​​​​​​metal:"https://emoji.slack-edge.com/T11N8QR19/metal/9f936a4278.png", ​​​​​​​cubimal_chick:"https://emoji.slack-edge.com/T11N8QR19/cubimal_chick/85961c43d7.png", ​​​​​​​bowtie:"https://emoji.slack-edge.com/T11N8QR19/bowtie/f3ec6f2bb0.png", ​​​​​​​godmode:"https://emoji.slack-edge.com/T11N8QR19/godmode/1bd6476fbb.png", ​​​​​​​pride:"https://emoji.slack-edge.com/T11N8QR19/pride/56b1bd3388.png", ​​​​​​​simple_smile:"https://a.slack-edge.com/66f9/img/emoji_2015/apple-old/simple_smile.png", ​​​​​​​rage4:"https://emoji.slack-edge.com/T11N8QR19/rage4/a8029a3996.png", ​​​​​​​trollface:"https://emoji.slack-edge.com/T11N8QR19/trollface/8c0ac4ae98.png", ​​​​​​​octocat:"https://emoji.slack-edge.com/T11N8QR19/octocat/627964d7c9.png", ​​​​​​​goberserk:"https://emoji.slack-edge.com/T11N8QR19/goberserk/d8b892d59b.png", ​​​​​​​suspect:"https://emoji.slack-edge.com/T11N8QR19/suspect/ca4ab3c7c7.png"​​​​​}, ​​​​​cache_ts:"1452230549.000000", ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXX8"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy Slack | Leave a comment

POST method REST API__Slack_dnd.teaminfo

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチームのDo Not Disturbステータスを取得する(dnd.teaminfo)AppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_dnd.teaminfo.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/dnd.teamInfo"
set aRec to {token:accessToken, user:"U11PVKT6D"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–> {​​​​​users:{​​​​​​​U11PVKT6D:{​​​​​​​​​next_dnd_start_ts:1461070800, ​​​​​​​​​dnd_enabled:1, ​​​​​​​​​next_dnd_end_ts:1461106800​​​​​​​}​​​​​}, ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_dnd.info

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してユーザーのDo Not Disturbステータスを取得するAppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_dnd.info.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/dnd.info"
set aRec to {token:accessToken, user:"U11PVKT6D"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–> {​​​​​next_dnd_start_ts:1461070800, ​​​​​next_dnd_end_ts:1461106800, ​​​​​snooze_enabled:0, ​​​​​ok:1, ​​​​​dnd_enabled:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_chat.update

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチャット内容のアップデート(chat.update)を実行するAppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_chat.update
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/chat.update"
set aRec to {token:accessToken, include_disabled:"1", include_count:"1", include_users:"1"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>  {​​​​​error:"channel_not_found", ​​​​​ok:0​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy Slack | Leave a comment

POST method REST API__Slack_chat.postMessage

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチャット投稿(chat.postMessage)を実行するAppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_chat.postMessage
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/chat.postMessage"
set aRec to {token:accessToken, channel:"C11NJCBU3", |text|:"こんにちは", parse:"full", link_names:"1", username:"piyomaruBot"}

set aRes to callWebPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–> {​​​​​ts:"1461111976.000006", ​​​​​message:{​​​​​​​username:"bot", ​​​​​​​ts:"1461111976.000006", ​​​​​​​type:"message", ​​​​​​​subtype:"bot_message", ​​​​​​​text:"こんにちは"​​​​​}, ​​​​​channel:"C11NJCBU3", ​​​​​ok:1​​​}

–POST methodのWeb APIを呼ぶ
on callWebPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callWebPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy Slack | Leave a comment

POST method REST API__Slack_channels.list

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチャンネル一覧取得(channels.list)を実行するAppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_channels.list.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/channels.list"
set aRec to {token:accessToken, exclude_archived:"1"}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>   {​​​​​channels:{​​​​​​​{​​​​​​​​​id:"C11NJCBU3", ​​​​​​​​​num_members:1, ​​​​​​​​​members:{​​​​​​​​​​​"U11PVKT6D"​​​​​​​​​}, ​​​​​​​​​is_general:0, ​​​​​​​​​is_archived:0, ​​​​​​​​​topic:{​​​​​​​​​​​value:"", ​​​​​​​​​​​creator:"", ​​​​​​​​​​​last_set:0​​​​​​​​​}, ​​​​​​​​​creator:"U11PVKT6D", ​​​​​​​​​is_member:1, ​​​​​​​​​is_channel:1, ​​​​​​​​​created:1461049266, ​​​​​​​​​purpose:{​​​​​​​​​​​value:"AppleScript Talk", ​​​​​​​​​​​creator:"U11PVKT6D", ​​​​​​​​​​​last_set:1461049267​​​​​​​​​}, ​​​​​​​​​name:"applescript"​​​​​​​}, ​​​​​​​{​​​​​​​​​id:"C11PQC1AT", ​​​​​​​​​num_members:1, ​​​​​​​​​members:{​​​​​​​​​​​"U11PVKT6D"​​​​​​​​​}, ​​​​​​​​​is_general:1, ​​​​​​​​​is_archived:0, ​​​​​​​​​topic:{​​​​​​​​​​​value:"Company-wide announcements and work-based matters", ​​​​​​​​​​​creator:"", ​​​​​​​​​​​last_set:0​​​​​​​​​}, ​​​​​​​​​creator:"U11PVKT6D", ​​​​​​​​​is_member:1, ​​​​​​​​​is_channel:1, ​​​​​​​​​created:1461045242, ​​​​​​​​​purpose:{​​​​​​​​​​​value:"This channel is for team-wide communication and announcements. All team members are in this channel.", ​​​​​​​​​​​creator:"", ​​​​​​​​​​​last_set:0​​​​​​​​​}, ​​​​​​​​​name:"general"​​​​​​​}, ​​​​​​​{​​​​​​​​​id:"C11PQC1C7", ​​​​​​​​​num_members:1, ​​​​​​​​​members:{​​​​​​​​​​​"U11PVKT6D"​​​​​​​​​}, ​​​​​​​​​is_general:0, ​​​​​​​​​is_archived:0, ​​​​​​​​​topic:{​​​​​​​​​​​value:"Non-work banter and water cooler conversation", ​​​​​​​​​​​creator:"", ​​​​​​​​​​​last_set:0​​​​​​​​​}, ​​​​​​​​​creator:"U11PVKT6D", ​​​​​​​​​is_member:1, ​​​​​​​​​is_channel:1, ​​​​​​​​​created:1461045242, ​​​​​​​​​purpose:{​​​​​​​​​​​value:"A place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you’d prefer to keep out of more focused work-related channels.", ​​​​​​​​​​​creator:"", ​​​​​​​​​​​last_set:0​​​​​​​​​}, ​​​​​​​​​name:"random"​​​​​​​}​​​​​}, ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy Slack | Leave a comment

POST method REST API__Slack_channels.info

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してチャンネル情報取得(channels.info)を実行するAppleScriptです。

SLackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_channels.info.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/channels.info"
set aRec to {token:accessToken, channel:"C11NJCBU3"} –#applescript

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–>   {​​​​​error:"channel_not_found", ​​​​​ok:0​​​}
–>  {​​​​​channel:{​​​​​​​id:"C11NJCBU3", ​​​​​​​members:{​​​​​​​​​"U11PVKT6D"​​​​​​​}, ​​​​​​​last_read:"0000000000.000000", ​​​​​​​is_general:0, ​​​​​​​unread_count:0, ​​​​​​​is_archived:0, ​​​​​​​topic:{​​​​​​​​​value:"", ​​​​​​​​​creator:"", ​​​​​​​​​last_set:0​​​​​​​}, ​​​​​​​creator:"U11PVKT6D", ​​​​​​​is_member:1, ​​​​​​​is_channel:1, ​​​​​​​created:1461049266, ​​​​​​​unread_count_display:0, ​​​​​​​purpose:{​​​​​​​​​value:"AppleScript Talk", ​​​​​​​​​creator:"U11PVKT6D", ​​​​​​​​​last_set:1461049267​​​​​​​}, ​​​​​​​name:"applescript", ​​​​​​​latest:{​​​​​​​​​text:"<@U11PVKT6D|maro> set the channel purpose: AppleScript Talk", ​​​​​​​​​purpose:"AppleScript Talk", ​​​​​​​​​ts:"1461049267.000003", ​​​​​​​​​type:"message", ​​​​​​​​​subtype:"channel_purpose", ​​​​​​​​​user:"U11PVKT6D"​​​​​​​}​​​​​}, ​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_auth.test

Posted on 2月 16, 2018 by Takaaki Naganoya
AppleScript名:POST method REST API__Slack_auth.test.scptd
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/auth.test"
set aRec to {token:accessToken}

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, aRec) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–> {​​​​​user:"maro", ​​​​​team_id:"X99X9XX99", ​​​​​user_id:"X99XXXX9X", ​​​​​ok:1, ​​​​​url:"https://piyomarusoftware.slack.com/", ​​​​​team:"Piyomaru Software"​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, aRec)
  
  
load framework
  
  
set aRes to retURLwithParams(aURL, aRec) of me
  
if aRes = false then return false
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aRes)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
–aRequest’s setValue:(token of aRec) forHTTPHeaderField:"token"
  
–aRequest’s setValue:(channel of aRec) forHTTPHeaderField:"channel"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

on retURLwithParams(aBaseURL, aRec)
  
  
set aDic to current application’s 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
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POST method REST API__Slack_api.test

Posted on 2月 16, 2018 by Takaaki Naganoya

SlackのREST APIを呼び出してAPI呼び出しテスト(api.test)を実行するAppleScriptです。

SlackにサインアップしてAPIのアクセストークンを取得し、プログラムリスト中に記入して実行してください。

AppleScript名:POST method REST API__Slack_api.test
— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/api.test"

set aRes to callRestPOSTAPIAndParseResults(reqURLStr, accessToken) of me

set aRESTres to json of aRes
set aRESCode to responseCode of aRes
–>  200

set aRESHeader to responseHeader of aRes
–>  {​​​​​Content-Type:"application/json; charset=utf-8", ​​​​​Access-Control-Allow-Origin:"*", ​​​​​X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", ​​​​​Content-Security-Policy:"referrer no-referrer;", ​​​​​Content-Encoding:"gzip", ​​​​​Server:"Apache", ​​​​​X-XSS-Protection:"0", ​​​​​Transfer-Encoding:"Identity", ​​​​​Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", ​​​​​Date:"Tue, 19 Apr 2016 06:16:37 GMT", ​​​​​Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", ​​​​​Connection:"keep-alive", ​​​​​X-Content-Type-Options:"nosniff", ​​​​​Vary:"Accept-Encoding", ​​​​​X-Cache:"Miss from cloudfront"​​​}

aRESTres as list of string or string
–> {​​​​​ok:1​​​}

–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, anAPIkey)
  
  
load framework
  
  
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
aRequest’s setHTTPMethod:"POST"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
  
aRequest’s setValue:anAPIkey forHTTPHeaderField:"Authorization"
  
–aRequest’s setValue:jsonString forHTTPHeaderField:"Dropbox-API-Arg"
  
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
  
–aRequest’s setHTTPBody:dataJson
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
log resStr
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code
  
set dRes to contents of second item of resList
  
set resCode to ASify from (dRes’s statusCode())
  
  
–Get Response Header
  
set resHeaders to ASify from (dRes’s allHeaderFields())
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
  
end callRestPOSTAPIAndParseResults

on retAccessToken()
  return "xxxx-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXxXxxXXXX"
end retAccessToken

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)OCR Spaceで指定URL上の画像を文字認識する

Posted on 2月 16, 2018 by Takaaki Naganoya

画像文字認識のREST API「OCR.Space」を呼び出して、指定URL上の画像を文字認識するAppleScriptです。

とりあえず試してみるためには、OCR.SpaceにレジストしてAPI Keyを取得し、プログラムリスト中に記入してください。

英数字の文字認識については実用性を感じましたが、日本語文字認識については実用的ではないと感じました。

AppleScript名:(GET)OCR Spaceで指定URL上の画像を文字認識する
— Created 2016-10-27 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aURL to "http://piyocast.com/as/wp-content/uploads/2018/02/604b4edd7a57544c85020973f4d46eca.png"
set aText to recogTextFromImageURL(aURL) of me
(*
"Piyomaru

Software

Takaaki

Machine

Quit

MacBook Pro

MacBook Air

Mac mini

"
*)

on recogTextFromImageURL(aURL)
  set myAPIkey to "XxxxXXxxXXXXXXX" –API Key
  
set reqURLStr to "https://api.ocr.space/parse/ImageUrl"
  
–set aRec to {apikey:myAPIkey, |language|:"jpn", |url|:aURL}–日本語の文字認識はほぼ使い物にならない
  
set aRec to {apikey:myAPIkey, |language|:"eng", |url|:aURL}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
set aRESCode to (responseCode of aRes) as integer
  
set aRESTres to (json of aRes) as record
  
set errorD to ErrorDetails of aRESTres
  
  
if errorD is not equal to missing value then return ""
  
set recogText to ParsedText of (first item of (ParsedResults of aRESTres))
  
  
return recogText
end recogTextFromImageURL

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in Network OCR REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)openBDで複数冊の情報を取得する

Posted on 2月 16, 2018 by Takaaki Naganoya

日本国内の書籍データを提供するAPIサービス「openBD」のREST APIに問い合わせて複数冊の書籍情報を取得するAppleScriptです。

AppleScript名:(GET)openBDで複数冊の情報を取得する
— Created 2016-03-03 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set isbnList to {"978-4-7808-0204-7", " 978-4621300251"}
set isbnStr to retStrFromArrayWithDelimiter(isbnList, ", ") of me

set aBaseURL to "https://api.openbd.jp/v1/get"
set aRec to {isbn:isbnStr, pretty:""}

set reqURLStr to retURLwithParams(aBaseURL, aRec) of me

set aRes to callRestGETAPIAndParseResults(reqURLStr) of me
set aRESCode to (responseCode of aRes) as integer
if aRESCode is not equal to 200 then return false

set aRESHeader to responseHeader of aRes
set aRESTres to (json of aRes) as list

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

–リストを指定デリミタをはさんでテキスト化
on retStrFromArrayWithDelimiter(aList, aDelim)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
set aRes to anArray’s componentsJoinedByString:aDelim
  
return aRes as text
end retStrFromArrayWithDelimiter

★Click Here to Open This Script 

Posted in Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)openBDで1冊分の情報を取得する

Posted on 2月 16, 2018 by Takaaki Naganoya

日本国内の書籍データを提供するAPIサービス「openBD」のREST APIに問い合わせて1冊分の書籍情報を取得するAppleScriptです。

AppleScript名:(GET)openBDで1冊分の情報を取得する
— Created 2016-03-03 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aBaseURL to "https://api.openbd.jp/v1/get"
set aRec to {isbn:"978-4-7808-0204-7", pretty:""}

set reqURLStr to retURLwithParams(aBaseURL, aRec) of me

set aRes to callRestGETAPIAndParseResults(reqURLStr) of me
set aRESCode to (responseCode of aRes) as integer
if aRESCode is not equal to 200 then return false

set aRESHeader to responseHeader of aRes
set aRESTres to (json of aRes) as list

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

Posted in JSON Network REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)Wikipedia APIでキーワード検索を行う

Posted on 2月 10, 2018 by Takaaki Naganoya
AppleScript名:(GET)Wikipedia APIでキーワード検索を行う
— Created 2016-11-04 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–https://www.mediawiki.org/wiki/API:Main_page/ja

set reqURLStr to "https://jp.wikipedia.org/w/api.php" –Japanese Version

set aRec to {action:"query", titles:"アップル (企業)", |prop|:"revisions", rvprop:"content", |format|:"json"}
set aURL to retURLwithParams(reqURLStr, aRec) of me
set aRes to callRestGETAPIAndParseResults(aURL) of me

set aRESTres to (query of json of aRes)
return aRESTres as list of string or string –as anything

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

★Click Here to Open This Script 

Posted in Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

connpassイベントサーチAPIで検索を行うv2

Posted on 2月 10, 2018 by Takaaki Naganoya
AppleScript名:connpassイベントサーチAPIで検索を行うv2
— Created 2016-10-29 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://connpass.com/about/api/
–http://piyocast.com/as/archives/4300

set reqURLStr to "https://connpass.com/api/v1/event/"

set aRec to {keyword:"AppleScript", ym:"201611"} –サーチクエリーと対象月
set aURL to retURLwithParams(reqURLStr, aRec) of me
set aRes to callRestGETAPIAndParseResults(aURL) of me

set aRESTres to (json of aRes) as record
return aRESTres
–>
(*
{results_available:1, results_start:1, |events|:{{place:"マイ・スペース MS&BB 池袋西武横店 1号室", event_url:"http://ashole.connpass.com/event/44103/", accepted:2, title:"AppleScript本フィードバック会", limit:7, event_type:"participation", owner_id:64136, ended_at:"2016-11-26T20:30:00+09:00", updated_at:"2016-11-01T09:22:15+09:00", lon:"139.711383200000", waiting:0, event_id:44103, hash_tag:"AppleScript,Mac,macOS,Mac OS X", owner_nickname:"Piyomaru", lat:"35.726486900000", started_at:"2016-11-26T18:30:00+09:00", owner_display_name:"Piyomaru", catch:"「AppleScript最新リファレンス」「AppleScript最新10大技術」についての解説", series:{|url|:"http://ashole.connpass.com/", |id|:3041, title:"AppleScriptの穴"}, address:"〒171-0022 東京都豊島区南池袋1-16-20(ぬかりやビル2階)", |description|:"<p>macOS標準装備で、GUIアプリケーションを操作できるマクロ言語「AppleScript」、その20年以上の歴史をまとめ、最新情報を盛り込んだ電子書籍「AppleScript最新リファレンス」「AppleScript 最新10大技術」を発行いたしました。</p>\n<p>・電子書籍オンライン販売URL\n<a href=\"https://piyomarusoft.booth.pm\" rel=\"nofollow\">https://piyomarusoft.booth.pm</a></p>\n<p>これらの本について、分からない点やもっと知りたい点について、筆者本人と直接お話できる場を設けました。</p>\n<p>参加資格は、AppleScriptを実際に使っている方、興味を持っている方で、筆者の書籍を実際に購入した、あるいは購入しようと考えている方です。事前に内容を読んであることが望ましいです。</p>\n<p>筆者Blog「AppleScriptの穴」\n<a href=\"http://piyocast.com/as/\" rel=\"nofollow\">http://piyocast.com/as/</a></p>"}}, results_returned:1}
*)

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

★Click Here to Open This Script 

Posted in Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

(GET)Yahoo! 住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の住所ジオコーダAPIを呼び出して、住所情報から緯度、経度を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

GoogleやYahoo!のWeb APIベース、AppleのOS内蔵住所ジオコーダなどさまざまですが、個人的にはYahoo!の住所ジオコーダをよく使っています。ただし、指定の住所がかならずしも一発で緯度・経度情報に変換できるわけでもないので、エラー時には後ろから1文字ずつ削除して再試行しています。

AppleScript名:(GET)Yahoo! 住所ジオコーダAPIを呼び出す
— Created 2016-11-25 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html

set anAddress to "東京都墨田区押上1-1−2"
set aResList to addressGeoCoderByYahoo(anAddress) of me
–>  {​​​​​"35.66042757", ​​​​​"139.72918139"​​​}

on addressGeoCoderByYahoo(addrStr)
  set reqURLStr to "http://geo.search.olp.yahooapis.jp/OpenLocalPlatform/V1/geoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {query:addrStr, output:"json", appid:aKey, datum:"tky"} –日本測地系
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
  
set anAdd1 to (aJSONres’s valueForKeyPath:"Feature")’s firstObject()
  
set aGPSstr to (anAdd1’s valueForKeyPath:"Geometry.Coordinates")
  
set {aLon, aLat} to separateStrByAMark(aGPSstr, ",") of me
  
  
return {aLat, aLon}
end addressGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

–テキストを指定記号を元に分割する
on separateStrByAMark(aStr as string, aMark as string)
  set aMarkLen to length of aMark
  
set aOffset to offset of "," in aStr
  
set aPart to text 1 thru (aOffset – 1) of aStr
  
set bPart to text (aOffset + aMarkLen) thru -1 of aStr
  
return {aPart, bPart}
end separateStrByAMark

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の逆住所ジオコーダAPIを呼び出して、緯度、経度から住所情報を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

AppleScript名:(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す
— Created 2016-11-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/reversegeocoder.html

set aLat to 35.74 as string
set aLon to 139.6 as string
set aResList to reverseGeoCoderByYahoo(aLat, aLon) of me
–>  {​​​​​"東京都", ​​​​​"練馬区"​​​}

on reverseGeoCoderByYahoo(aLat, aLon)
  set reqURLStr to "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {lat:aLat, lon:aLon, output:"json", appid:aKey, datum:"tky"}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
set anAddress to (aJSONres’s valueForKeyPath:"Feature.Property")’s firstObject()
  
set anElement to anAddress’s valueForKeyPath:"AddressElement"
  
  
set aPrefecture to first item of (my filterRecListByLabel1(anElement, "Level == ’prefecture’"))
  
–>  {​​​​​Kana:"とうきょうと", ​​​​​Name:"東京都", ​​​​​Level:"prefecture", ​​​​​Code:"13"​​​}
  
  
set aCity to first item of (my filterRecListByLabel1(anElement, "Level == ’city’"))
  
–>  {​​​​​Kana:"ねりまく", ​​​​​Name:"練馬区", ​​​​​Level:"city", ​​​​​Code:"13120"​​​}
  
  
set aPref to |Name| of aPrefecture
  
set aCt to |Name| of aCity
  
return {aPref, aCt}
end reverseGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s 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 qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • UI Browserがgithub上でソース公開され、オープンソースに
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • iWork 12.2がリリースされた
  • ChatGPTでchatに対する応答文を取得
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 14, Sonoma
  • macOS 13でNSNotFoundバグふたたび
  • Finderの隠し命令openVirtualLocationが発見される
  • あのコン過去ログビューワー(暫定版)
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • Dockアイコンにプログレスバーを追加 v3
  • クリップボードに入った書式つきテキストをプレーン化する「PlainerText」
  • CotEditor v4.4.1+macOS 13の組み合わせで発生している問題
  • 新発売:Mail.app Scripting Book with AppleScript

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (278) 12.0savvy (190) 13.0savvy (69) CotEditor (61) Finder (48) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (21) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC