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

カテゴリー: Network

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

AirServerで出力先名を宣言する

Posted on 2月 13, 2018 by Takaaki Naganoya

AppleScript名:AirServerで出力先名を宣言する
tell application "AirServer"
  start broadcast as "Piyomaru MacBook Pro"
end tell

★Click Here to Open This Script 

Posted in AirPlay Network | Tagged 10.11savvy 10.12savvy 10.13savvy AirServer | Leave a comment

AirServer出力を停止する

Posted on 2月 13, 2018 by Takaaki Naganoya

AppleScript名:AirServer出力を停止する
tell application "AirServer"
  stop broadcast
end tell

★Click Here to Open This Script 

Posted in AirPlay Network | Tagged 10.11savvy 10.12savvy 10.13savvy AirServer | Leave a comment

表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出

Posted on 2月 11, 2018 by Takaaki Naganoya

Safariで表示中のYouTubeのムービーをローカルのMoviesフォルダにダウンロードして音声部分のみ抽出するAppleScriptです。

YouTubeからのダウンロードを行う「YouTubeDLLib」およびShane StanleyのAppleScript Libraries「BridgePlus」を~/Libraries/AppleScript Librariesフォルダに、XAttribute.frameworkを~/Libraries/Frameworksフォルダにインストールして実行してください。

–> YouTubeDLLib

YouTubeDLLib内部には「youtube-dl」を含んでおり、これ単体でアップデートが必要な場合もあります。急に動かなくなったような場合には、新バージョンが出ていないかチェックしてみてください。

Script Menuから実行することを前提としています。

AppleScript名:表示中のYouTubeのムービーをローカルにダウンロードして音声のみ抽出
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"
use framework "XAttribute" –https://github.com/rylio/OTMXAttribute
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use youtubeLib : script "YouTubeDLLib"

set dlFolder to POSIX path of (path to movies folder) –ダウンロード先のフォルダ

set aURL to getPageURLOfFrontmostWindow() of me
set aList to aURL

load framework

–指定URLのYouTubeのムービーをダウンロードする
set aRes to downLoadMovieToFile(aURL, dlFolder) of youtubeLib

–オーディオ部分のみ抽出
set dlFullPath to dlFolder & aRes

–ダウンロードしたMovieからXattrを削除する
set xRes to removeXAttrFromFile(dlFullPath, "com.apple.quarantine")

my convertMovieAt:dlFullPath ToType:"AVAssetExportPresetAppleM4A" withExtension:"m4a" deleteOriginal:true

on getPageURLOfFrontmostWindow()
  tell application "Safari"
    if (count every window) = 0 then return false
    
tell window 1
      set aProp to properties
    end tell
    
set aDoc to document of aProp
    
set aText to URL of aDoc
  end tell
  
return aText
end getPageURLOfFrontmostWindow

on getPageTitleOfFrontmostWindow()
  tell application "Safari"
    if (count every window) = 0 then return false
    
tell window 1
      set aProp to properties
    end tell
    
set aDoc to document of aProp
    
set aText to name of aDoc
  end tell
  
return aText
end getPageTitleOfFrontmostWindow

–指定のムービーを、指定の書き出しプリセットで、指定のファイル形式で、オリジナルファイルを削除するかどうか指定しつつ書き出し
on convertMovieAt:(posixPath as string) ToType:(assetTypeStr as string) withExtension:(aExt as string) deleteOriginal:(deleteFlag as boolean)
  set theURL to current application’s |NSURL|’s fileURLWithPath:posixPath
  
  
set aPreset to current application’s NSString’s stringWithString:assetTypeStr
  
  
— set destination to use same path, different extension
  
set destURL to theURL’s URLByDeletingPathExtension()’s URLByAppendingPathExtension:aExt
  
set theAsset to current application’s AVAsset’s assetWithURL:theURL
  
  
— check asset can be converted
  
set allowedPresets to current application’s AVAssetExportSession’s exportPresetsCompatibleWithAsset:theAsset
  
if (allowedPresets’s containsObject:aPreset) as boolean is false then
    error "Can’t export this file as an ." & aExt & " file."
  end if
  
  
— set up export session
  
set fileTypeUTIstr to retFileFormatUTI(aExt) of me –ファイル拡張子からFile Type UTIを求める
  
set theSession to current application’s AVAssetExportSession’s exportSessionWithAsset:theAsset presetName:aPreset
  
theSession’s setOutputFileType:fileTypeUTIstr
  
theSession’s setOutputURL:destURL
  
  
— begin export and poll for completion
  
theSession’s exportAsynchronouslyWithCompletionHandler:(missing value)
  
repeat
    set theStatus to theSession’s status() as integer
    
if theStatus < 3 then
      delay 0.2
    else
      exit repeat
    end if
  end repeat
  
  
— throw error if it failed
  
if theStatus = (current application’s AVAssetExportSessionStatusFailed) as integer then
    error (theSession’s |error|()’s localizedDescription() as text)
  end if
  
  
— delete original if required
  
if deleteFlag then
    current application’s NSFileManager’s defaultManager()’s removeItemAtURL:theURL |error|:(missing value)
  end if
end convertMovieAt:ToType:withExtension:deleteOriginal:

on retFileFormatUTI(aExt as string)
  return (current application’s SMSForder’s UTIForExtension:aExt)
end retFileFormatUTI

on removeXAttrFromFile(aFile, anXattr)
  –Get Xattr String
  
set anAttribute to (current application’s OTMXAttribute’s stringAttributeAtPath:aFile |name|:anXattr |error|:(missing value))
  
if anAttribute = missing value then return true –There is no use to remove xattr
  
  
–Remove Xattr
  
set xRes to (current application’s OTMXAttribute’s removeAttributeAtPath:aFile |name|:anXattr |error|:(missing value))
  
if xRes = missing value then return false
  
return (xRes as boolean)
end removeXAttrFromFile

★Click Here to Open This Script 

Posted in file Network | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | Leave a comment

表示中のYouTubeのムービーをローカルにダウンロード v2

Posted on 2月 11, 2018 by Takaaki Naganoya

Safariで表示中のYouTubeのムービーをローカルのMoviesフォルダにダウンロードするAppleScriptです。

YouTubeからのダウンロードを行う「YouTubeDLLib」を~/Library/Script Librariesフォルダ(初期状態では存在しないので、ない場合には手動で作成)にインストールして実行してください。

–> Download YouTubeDLLib (To ~/Library/Script Libraries)

YouTubeDLLib内部には「youtube-dl」を含んでおり、これ単体でアップデートが必要な場合もあります(半年に1回ぐらいアップデートしています)。

Script Menuから実行することを前提としています。

AppleScript名:表示中のYouTubeのムービーをローカルにダウンロード v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use youtubeLib : script "YouTubeDLLib"

set dlFolder to POSIX path of (path to movies folder) –ダウンロード先のフォルダ

set aURL to getPageURLOfFrontmostWindow() of me
set aList to {aURL}

set erList to {}
–set aList to paragraphs of a
repeat with i in aList
  set j to contents of i
  
  
–指定URLのYouTubeのムービーをダウンロードする
  
dlMovieSub(aURL, dlFolder) of youtubeLib
end repeat

return erList –ダウンロード結果

on getPageURLOfFrontmostWindow()
  tell application "Safari"
    if (count every window) = 0 then return false
    
tell window 1
      set aProp to properties
    end tell
    
set aDoc to document of aProp
    
set aText to URL of aDoc
  end tell
  
return aText
end getPageURLOfFrontmostWindow

★Click Here to Open This Script 

Posted in file Network | Tagged 10.11savvy 10.12savvy 10.13savvy Safari | 1 Comment

郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:郵便専門ネットでXML-RPC経由で郵便番号を6桁(チェックデジット付き)の全国地方公共団体コード/JISコード/市町村コードに変換
tell application "http://yubin.senmon.net/service/xmlrpc/"
  call xmlrpc {method name:"yubin.postcodeToJiscode6", parameters:{"1760024"}}
end tell

–> "131202"

★Click Here to Open This Script 

Posted in Network XML-RPC | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得

Posted on 2月 11, 2018 by Takaaki Naganoya
AppleScript名:郵便専門ネットでXML-RPC経由で引数に都道府県のJISコード(JISコードの先頭2文字)を渡すと、その都道府県に属しているJISコードを取得
tell application "http://yubin.senmon.net/service/xmlrpc/"
  call xmlrpc {method name:"yubin.jiscodeFromPrefcode", parameters:{"13"}}
end tell

–> {"13101", "13102", "13103", "13104", "13105", "13106", "13107", "13108", "13109", "13110", "13111", "13112", "13113", "13114", "13115", "13116", "13117", "13118", "13119", "13120", "13121", "13122", "13123", "13201", "13202", "13203", "13204", "13205", "13206", "13207", "13208", "13209", "13210", "13211", "13212", "13213", "13214", "13215", "13218", "13219", "13220", "13221", "13222", "13223", "13224", "13225", "13227", "13228", "13229", "13303", "13305", "13307", "13308", "13361", "13362", "13363", "13364", "13381", "13382", "13401", "13402", "13421"}

★Click Here to Open This Script 

Posted in Network XML-RPC | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • Xcode 14.2でAppleScript App Templateを復活させる
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (185) 13.0savvy (55) CotEditor (60) Finder (47) iTunes (19) Keynote (98) 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 (56) Pages (37) Safari (41) Script Editor (20) 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年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