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

月: 2020年4月

指定アプリケーションを指定言語環境で再起動

Posted on 4月 30, 2020 by Takaaki Naganoya

指定アプリケーションを、現在のユーザーアカウントで指定可能な言語環境を指定して再起動するAppleScriptです。

Xcodeにこのような機能があり、そのような処理を行える可能性について思い至っていました。shell commandについてはEdama2さんから教えていただきました。あー、やっぱりこういう感じなんですね。

次に、言語コードを求める方法ですが、調べても全言語の言語コードを求める方法が見当たらなかったので(これは、探し方が足りないだけ)、現在のユーザーアカウントで指定可能な言語コードを求める方法に落ち着きました。

本Scriptを実行すると、指定アプリケーションを起動する言語コードを選択。

処理するアプリケーションを選択。choose applicationコマンドが活躍するのはとても珍しいケースです。

英語(English)を指定した場合。

フランス語(French)を指定した場合。

日本語(Japanese)を指定した場合。

個人的には、英語環境でアプリケーションを動かしつつ、執筆用のアプリケーション(Keynote)だけ日本語環境で動かすことができて、些細なコマンドの呼称を確認する必要がなくていい感じです。あれ? 日本語環境でターゲットのアプリケーションだけ英語環境を指定して起動すればいいんじゃ????

AppleScript名:指定アプリケーションを指定言語環境で再起動
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/30
–  Special Thanks to : Edama2
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
–  
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aLocList to (current application’s NSLocale’s preferredLanguages()) as list

set targLanguage to choose from list aLocList
set anApp to choose application

set apFile to POSIX path of (path to anApp)
tell anApp to quit

set sText to "open -n -a " & quoted form of apFile & " –args -AppleLanguages ’(\"" & targLanguage & "\")’"
do shell script sText

★Click Here to Open This Script 

(Visited 60 times, 4 visits today)
Posted in Language Locale shell script | Tagged 10.13savvy 10.14savvy 10.15savvy | 3 Comments

アラートダイアログ上にWkWebViewでGoogle Chartsを表示 v2

Posted on 4月 29, 2020 by Takaaki Naganoya

アラートダイアログ上にWkWebViewを配置し、Google Chartsを用いてPie Chartを表示するAppleScriptです。

自分の開発環境(MacBook Pro Retina 2012, Core i7 2.6GHz)で10日間のアクセス履歴を処理して1.38秒程度でダイアログ表示してグラフ描画に入ります。3秒強ぐらいで描画が終了します。

WkWebViewに対して、文字列で組み立てたHTMLを読み込んでグラフ表示しています。Pie Chart上にマウスカーソルを乗せると反応します。一応表示できているぐらいで、デザイン的には余計な余白ができたままで、いまひとつな感じです。せめて、WkWebViewの背景を透明化できると大味な表示をいくらか緩和できるかと思っているのですが、WkWebViewの透明背景にはNSColorではなく(AppleScriptから作成できない)CGColorでclearColor(透明色)を指定する必要があるようで、、、、

サンプル表示データは、Safariの10日間のアクセス履歴からドメイン単位で計算したヒストグラムを、その場で計算したものを使っています。

本Scriptは、①NSAlertのダイアログ表示の経験 ②WkWebView上へのコンテンツ表示の経験 ③グラフ表示用FrameworkやSVGベースのグラフ作成の経験 を経て、2・3年越しで完成したものです。とくに、WkWebViewについてはサンプル数がそれほど多くなく、用途の方向性がObjective-CやSwiftと若干異なるために、参考にできるサンプルの数が多くない状況。必然的に、いろいろ試しては失敗を重ねてきました。急に出来上がってきたものではありません。

Safariの履歴集計ScriptをScriptオブジェクトの中に放り込んだら、scripting additionsの用語をusing terms from句で囲っておく必要がありました。何回か経験していますが、知らないと対処に困る現象です。

現状(macOS 10.14以降)では、Scripting AdditionはmacOS標準搭載のStandard Additionsしか存在していないし認識も行われないため、エラー内容がいまひとつわかりにくく、不正確であるように思えます。Apple純正のStandard Additionsしか存在していないんだから自分で判断しろやコラ、と言いたくなります。

AppleScript名:アラートダイアログ上にWebViewでGoogle Chartを表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/02
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"
use scripting additions

property |NSURL| : a reference to current application’s |NSURL|
property NSAlert : a reference to current application’s NSAlert
property NSString : a reference to current application’s NSString
property NSButton : a reference to current application’s NSButton
property WKWebView : a reference to current application’s WKWebView
property WKUserScript : a reference to current application’s WKUserScript
property NSURLRequest : a reference to current application’s NSURLRequest
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property WKUserContentController : a reference to current application’s WKUserContentController
property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration
property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd

property returnCode : 0

–Calc Safari access domain from history
set aRes to calcSafariHistoryBest10Domain(10) of safariHistoryLib

set aList to {{"Domain", "Access"}}
repeat with i in aRes
  set aDom to theName of i
  
set accessNum to numberOfTimes of i
  
set the end of aList to {aDom, accessNum}
end repeat

–set aList to {{"Task", "Hours per Day"}, {"Work", 8}, {"Eat", 2}, {"TV", 4}, {"Gym", 2}, {"Sleep", 8}}

set aJsonArrayStr to array2DToJSONArray(aList) of me

–Pie Chart Template HTML
set myStr to "<!DOCTYPE html>
<html lang=\"UTF-8\">
<body>
<div id=\"piechart\"></div>

<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>

<script type=\"text/javascript\">
// Load google charts
google.charts.load(’current’, {’packages’:[’corechart’]});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable(%@);

// Optional; add a title and set the width and height of the chart
var options = {’title’:’’, ’width’:600, ’height’:400};

// Display the chart inside the <div> element with id=\"piechart\"
var chart = new google.visualization.PieChart(document.getElementById(’piechart’));
chart.draw(data, options);
}
</script>

</body>
</html>"

set aString to current application’s NSString’s stringWithFormat_(myStr, aJsonArrayStr) as string

set paramObj to {myMessage:"Pie Chart Test", mySubMessage:"This is a simple pie chart using google charts", htmlStr:aString}
–my browseStrWebContents:paramObj–for debug
my performSelectorOnMainThread:"browseStrWebContents:" withObject:(paramObj) waitUntilDone:true

on browseStrWebContents:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set htmlString to (htmlStr of paramObj)
  
  
set aWidth to 600
  
set aHeight to 450
  
  
–WebViewをつくる
  
set aConf to WKWebViewConfiguration’s alloc()’s init()
  
  
–指定HTML内のJavaScriptをFetch
  
set jsSource to pickUpFromToStr(htmlString, "<script type=\"text/javascript\">", "</script>") of me
  
  
set userScript to WKUserScript’s alloc()’s initWithSource:jsSource injectionTime:(WKUserScriptInjectionTimeAtDocumentEnd) forMainFrameOnly:true
  
set userContentController to WKUserContentController’s alloc()’s init()
  
userContentController’s addUserScript:(userScript)
  
aConf’s setUserContentController:userContentController
  
  
set aWebView to WKWebView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight – 100)) configuration:aConf
  
aWebView’s setNavigationDelegate:me
  
aWebView’s setUIDelegate:me
  
aWebView’s setTranslatesAutoresizingMaskIntoConstraints:true
  
  
set bURL to |NSURL|’s fileURLWithPath:(POSIX path of (path to me))
  
aWebView’s loadHTMLString:htmlString baseURL:(bURL)
  
  
— set up alert  
  
set theAlert to NSAlert’s alloc()’s init()
  
tell theAlert
    its setMessageText:aMainMes
    
its setInformativeText:aSubMes
    
its addButtonWithTitle:"OK"
    
–its addButtonWithTitle:"Cancel"
    
its setAccessoryView:aWebView
    
    
set myWindow to its |window|
  end tell
  
  
— show alert in modal loop
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
  
  
–Stop Web View Action
  
set bURL to |NSURL|’s URLWithString:"about:blank"
  
set bReq to NSURLRequest’s requestWithURL:bURL
  
aWebView’s loadRequest:bReq
  
  
if (my returnCode as number) = 1001 then error number -128
end browseStrWebContents:

on doModal:aParam
  set (my returnCode) to (aParam’s runModal()) as number
end doModal:

on viewDidLoad:aNotification
  return true
end viewDidLoad:

on fetchJSSourceString(aURL)
  set jsURL to |NSURL|’s URLWithString:aURL
  
set jsSourceString to NSString’s stringWithContentsOfURL:jsURL encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
return jsSourceString
end fetchJSSourceString

on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
  set a1Offset to offset of s1Str in aStr
  
if a1Offset = 0 then return false
  
set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr
  
set a2Offset to offset of s2Str in bStr
  
if a2Offset = 0 then return false
  
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
  
return cStr as string
end pickUpFromToStr

–リストを任意のデリミタ付きでテキストに
on retArrowText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

on array2DToJSONArray(aList)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set jsonData to current application’s NSJSONSerialization’s dataWithJSONObject:anArray options:(0 as integer) |error|:(missing value) –0 is
  
set resString to current application’s NSString’s alloc()’s initWithData:jsonData encoding:(current application’s NSUTF8StringEncoding)
  
return resString
end array2DToJSONArray

–Safariのn日前からの履歴をドメイン別に集計してソート
script safariHistoryLib
  use scripting additions
  
use framework "Foundation"
  
property parent : AppleScript
  
  
property |NSURL| : a reference to current application’s |NSURL|
  
  
script spd
    property sList : {}
    
property nList : {}
    
property sRes : {}
  end script
  
  
  
on calcSafariHistoryBest10Domain(daysBefore)
    –現在日時のn日前を求める
    
using terms from scripting additions
      set origDate to (current date) – (daysBefore * days)
    end using terms from
    
set dStr to convDateObjToStrWithFormat(origDate, "yyyy-MM-dd hh:mm:ss") of me
    
    
set aDBpath to "~/Library/Safari/History.db"
    
set pathString to current application’s NSString’s stringWithString:aDBpath
    
set newPath to pathString’s stringByExpandingTildeInPath()
    
    
set aText to "sqlite3 " & newPath & " ’SELECT datetime(history_visits.visit_time+978307200, \"unixepoch\", \"localtime\"), history_visits.title || \" @ \" || substr(history_items.URL,1,max(length(history_items.URL)*(instr(history_items.URL,\" & \")=0),instr(history_items.URL,\" & \"))) as Info FROM history_visits INNER JOIN history_items ON history_items.id = history_visits.history_item where history_visits.visit_time>(julianday(\"" & dStr & "\")*86400-211845068000) ORDER BY visit_time ASC LIMIT 999999;’"
    
    
using terms from scripting additions
      set (sRes of spd) to do shell script aText
    end using terms from
    
    
set (sList of spd) to (paragraphs of (sRes of spd))
    
    
set (nList of spd) to {}
    
    
repeat with i in (sList of spd)
      set j to contents of i
      
      
–Parse each field
      
set j2 to parseByDelim(j, {"|", "@ "}) of me
      
      
–Calculate URL’s domain only
      
set j3 to contents of last item of j2
      
set aURL to (|NSURL|’s URLWithString:j3)
      
      
if aURL = missing value then
        set j4 to ""
      else
        set j4 to (aURL’s |host|()) as string
      end if
      
      
set the end of (nList of spd) to j4
    end repeat
    
    
–登場頻度でURLを集計
    
set aList to countItemsByItsAppearance((nList of spd)) of me
    
    
    
–Best 10の計算のために10位以下をまとめる
    
set best9 to items 1 thru 9 of aList
    
set best10 to items 10 thru -1 of aList
    
    
set otherTimes to 0
    
repeat with i in best10
      set otherTimes to otherTimes + (numberOfTimes of i)
    end repeat
    
    
set the end of best9 to {theName:"Other", numberOfTimes:otherTimes}
    
return best9
  end calcSafariHistoryBest10Domain
  
  
  
  
on parseByDelim(aData, aDelim)
    set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to aDelim
    
set dList to text items of aData
    
set AppleScript’s text item delimiters to curDelim
    
return dList
  end parseByDelim
  
  
  
–出現回数で集計
  
on countItemsByItsAppearance(aList)
    set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList
    
set bArray to current application’s NSMutableArray’s array()
    
set theEnumerator to aSet’s objectEnumerator()
    
    
repeat
      set aValue to theEnumerator’s nextObject()
      
if aValue is missing value then exit repeat
      
bArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"theName", "numberOfTimes"})
    end repeat
    
    
–出現回数(numberOfTimes)で降順ソート
    
set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"numberOfTimes" ascending:false
    
bArray’s sortUsingDescriptors:{theDesc}
    
    
return bArray as list
  end countItemsByItsAppearance
  
  
  
on convDateObjToStrWithFormat(aDateO as date, aFormatStr as string)
    set aDF to current application’s NSDateFormatter’s alloc()’s init()
    
    
set aLoc to current application’s NSLocale’s currentLocale()
    
set aLocStr to (aLoc’s localeIdentifier()) as string
    
    
aDF’s setLocale:(current application’s NSLocale’s alloc()’s initWithLocaleIdentifier:aLocStr)
    
aDF’s setDateFormat:aFormatStr
    
set dRes to (aDF’s stringFromDate:aDateO) as string
    
return dRes
  end convDateObjToStrWithFormat
  
end script

★Click Here to Open This Script 

(Visited 50 times, 1 visits today)
Posted in dialog Web Contents Control | Tagged 10.13savvy 10.14savvy 10.15savvy NSAlert NSButton NSRunningApplication NSString NSURL NSURLRequest NSUTF8StringEncoding Safari WKUserContentController WKUserScript WKUserScriptInjectionTimeAtDocumentEnd WKWebView WKWebViewConfiguration | 1 Comment

Safariの履歴を読み込んでBest 10を求める。「その他」計算機能つき

Posted on 4月 29, 2020 by Takaaki Naganoya

Safariの閲覧履歴のdatabaseにアクセスして、domain単位でベスト10を求めるAppleScriptです。

自分の開発環境(MacBook Pro Retina 2012, Core i7 2.6GHz)で10日間のアクセス履歴を処理して0.8秒程度です。

以前のSafariではplistでヒストリを管理していましたが、気づいたらSqliteのDBで管理するように変わっていたので(Safari 10あたりで?)、よろしく抽出して加工してみました。おそらく、最低限の情報のみ抽出することで現状の半分ぐらいの処理時間で処理できるようになるとは思うのですが、汎用性を持たせるために現状のレベルにまとめています。

計算結果が当たり前すぎて予想よりはるかに面白くなかったので、そのことに逆に驚きました。

AppleScript名:Safariの履歴を読み込んでBest 10を求める。「その他」計算機能つき.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/29
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — (10.13) or later
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|

script spd
  property sList : {}
  
property nList : {}
  
property sRes : {}
end script

set aRes to calcSafariHistoryBest10Domain(10) of me
–> {{theName:"piyocast.com", numberOfTimes:1100}, {theName:"www.youtube.com", numberOfTimes:710}, {theName:"twitter.com", numberOfTimes:354}, {theName:"www.google.com", numberOfTimes:331}, {theName:"syosetu.com", numberOfTimes:199}, {theName:"github.com", numberOfTimes:140}, {theName:"developer.apple.com", numberOfTimes:139}, {theName:"appstoreconnect.apple.com", numberOfTimes:121}, {theName:"xxxxxxxxxxxxx", numberOfTimes:106}, {theName:"Other", numberOfTimes:847}}

on calcSafariHistoryBest10Domain(daysBefore)
  –現在日時のn日前を求める
  
set origDate to (current date) – (daysBefore * days)
  
set dStr to convDateObjToStrWithFormat(origDate, "yyyy-MM-dd hh:mm:ss") of me
  
  
set aDBpath to "~/Library/Safari/History.db"
  
set pathString to current application’s NSString’s stringWithString:aDBpath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
  
set aText to "sqlite3 " & newPath & " ’SELECT datetime(history_visits.visit_time+978307200, \"unixepoch\", \"localtime\"), history_visits.title || \" @ \" || substr(history_items.URL,1,max(length(history_items.URL)*(instr(history_items.URL,\" & \")=0),instr(history_items.URL,\" & \"))) as Info FROM history_visits INNER JOIN history_items ON history_items.id = history_visits.history_item where history_visits.visit_time>(julianday(\"" & dStr & "\")*86400-211845068000) ORDER BY visit_time ASC LIMIT 999999;’"
  
set (sRes of spd) to do shell script aText
  
  
set (sList of spd) to (paragraphs of (sRes of spd))
  
  
set (nList of spd) to {}
  
  
repeat with i in (sList of spd)
    set j to contents of i
    
    
–Parse each field
    
set j2 to parseByDelim(j, {"|", "@ "}) of me
    
    
–Calculate URL’s domain only
    
set j3 to contents of last item of j2
    
set aURL to (|NSURL|’s URLWithString:j3)
    
    
if aURL = missing value then
      set j4 to ""
    else
      set j4 to (aURL’s |host|()) as string
    end if
    
    
set the end of (nList of spd) to j4
  end repeat
  
  
–登場頻度でURLを集計
  
set aList to countItemsByItsAppearance((nList of spd)) of me
  
  
  
–Best 10の計算のために10位以下をまとめる
  
set best9 to items 1 thru 9 of aList
  
set best10 to items 10 thru -1 of aList
  
  
set otherTimes to 0
  
repeat with i in best10
    set otherTimes to otherTimes + (numberOfTimes of i)
  end repeat
  
  
set the end of best9 to {theName:"Other", numberOfTimes:otherTimes}
  
return best9
end calcSafariHistoryBest10Domain

on parseByDelim(aData, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set dList to text items of aData
  
set AppleScript’s text item delimiters to curDelim
  
return dList
end parseByDelim

–出現回数で集計
on countItemsByItsAppearance(aList)
  set aSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList
  
set bArray to current application’s NSMutableArray’s array()
  
set theEnumerator to aSet’s objectEnumerator()
  
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
bArray’s addObject:(current application’s NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"theName", "numberOfTimes"})
  end repeat
  
  
–出現回数(numberOfTimes)で降順ソート
  
set theDesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"numberOfTimes" ascending:false
  
bArray’s sortUsingDescriptors:{theDesc}
  
  
return bArray as list
end countItemsByItsAppearance

on convDateObjToStrWithFormat(aDateO as date, aFormatStr as string)
  set aDF to current application’s NSDateFormatter’s alloc()’s init()
  
  
set aLoc to current application’s NSLocale’s currentLocale()
  
set aLocStr to (aLoc’s localeIdentifier()) as string
  
  
aDF’s setLocale:(current application’s NSLocale’s alloc()’s initWithLocaleIdentifier:aLocStr)
  
aDF’s setDateFormat:aFormatStr
  
set dRes to (aDF’s stringFromDate:aDateO) as string
  
return dRes
end convDateObjToStrWithFormat

★Click Here to Open This Script 

(Visited 32 times, 1 visits today)
Posted in shell script | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

Previewで現在表示中のPDFのページ番号を抽出する

Posted on 4月 29, 2020 by Takaaki Naganoya

macOS標準搭載の画像/PDFビューワーの「Preview.app」で表示中のPDFの、現在表示中のページの番号を取得するAppleScriptです。

本来、Preview.appのような超低機能アプリケーションから強引に情報を(GUI Scriptingまで使って)取得する意味はありません。PDFビューワーとしてまっとうな機能を持っているSkimを使って表示中のページ番号を取得するのが筋です。

ただ、どうしてもPreviewでないといけないケースで、仕方なく作ったものですが、英語環境でも日本語環境でも同様に動くために作ってみたらこんな感じに。指定アプリケーション単体で言語環境を指定して起動できると、各言語環境における動作確認が手軽に行えてよいと思うものの、手段がありそうで見つかりません(Xcode上でそういう起動ができるので、不可能ではないと思うのですが)。


▲英語環境で実行したところ(macOS 10.14.6)


▲日本語環境で実行したところ(macOS 10.15.4)

仕方なくGUI Scripting経由でウィンドウのタイトルを取得して、ファイル名とページ情報を文字列処理で分離しています。このあたり、英語環境と日本語環境でセパレータ(括弧)が異なるので、分離したページ情報から数字判定を行なって取得しています。Preview.appのアプリケーションバンドル内にこうしたフォーマットのテキストが存在していれば、そちらを使うべきです(見つかっていないので現状こんな感じで)。

GUI Scripting内でプロセス名を指定する箇所で、ローカライズされたプロセス名をCocoaの機能を用いて取得しています。これで、英語環境と日本語環境については問題なく共通Scriptでカバーできています。

AppleScript名:Previewで現在表示中のPDFのページ番号を抽出する.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/29
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set pNum to retPreviewDispPageNumber() of me

on retPreviewDispPageNumber()
  set aLocName to getLocalizedName("com.apple.Preview") of me
  
  
tell application "Preview"
    if (count every document) = 0 then return false
  end tell
  
  
tell application "System Events"
    tell process aLocName
      tell window 1
        set aTitle to title
      end tell
    end tell
  end tell
  
  
set aPageInfo to pickUpFromToStr(aTitle, "(", ")") of me –English env
  
if aPageInfo = false then
    set aPageInfo to pickUpFromToStr(aTitle, "(", ")") of me –double witdh parenthesis (Japanese env)
  end if
  
set pList to words of aPageInfo
  
set hitF to false
  
repeat with i in pList
    set nRes to chkNumeric(i) of me
    
if nRes = true then
      set hitF to true
      
exit repeat
    end if
  end repeat
  
  
if hitF = false then return
  
return i as integer
end retPreviewDispPageNumber

on getLocalizedName(aBundleID as string)
  set pRes to getProcessByBUndleID(aBundleID) of me
  
if pRes = false then return ""
  
set pName to pRes’s localizedName()
  
return pName as string
end getLocalizedName

–指定プロセスを取得する
on getProcessByBUndleID(aBundleID)
  set appArray to current application’s NSRunningApplication’s runningApplicationsWithBundleIdentifier:aBundleID
  
if appArray’s |count|() > 0 then
    set appItem to appArray’s objectAtIndex:0
    
return appItem
  else
    return false
  end if
end getProcessByBUndleID

on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string)
  set a1Offset to offset of s1Str in aStr
  
if a1Offset = 0 then return false
  
set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr
  
  
set a2Offset to offset of s2Str in bStr
  
if a2Offset = 0 then return false
  
  
set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr
  
  
return cStr as string
end pickUpFromToStr

–数字のみかチェック
on chkNumeric(checkString)
  set digitCharSet to current application’s NSCharacterSet’s characterSetWithCharactersInString:"0123456789"
  
set ret to my chkCompareString:checkString baseString:digitCharSet
  
return ret as boolean
end chkNumeric

on chkCompareString:checkString baseString:baseString
  set aScanner to current application’s NSScanner’s localizedScannerWithString:checkString
  
aScanner’s setCharactersToBeSkipped:(missing value)
  
aScanner’s scanCharactersFromSet:baseString intoString:(missing value)
  
return (aScanner’s isAtEnd()) as boolean
end chkCompareString:baseString:

★Click Here to Open This Script 

(Visited 183 times, 1 visits today)
Posted in GUI Scripting PDF Text | Tagged 10.13savvy 10.14savvy 10.15savvy NSCharacterSet NSRunningApplication NSScanner Preview | Leave a comment

システム環境設定の「ディスプレイ」>「ディスプレイ」を表示

Posted on 4月 28, 2020 by Takaaki Naganoya

システム環境設定の「ディスプレイ」paneの「ディスプレイ」を表示するAppleScriptです。

やりかたは山のようにあるものの、Cocoa系の機能を使うとこんな感じ。

AppleScript名:システム環境設定の「ディスプレイ」>「ディスプレイ」を表示_asobjc.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/28
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

current application’s NSWorkspace’s sharedWorkspace()’s openFile:"/System/Library/PreferencePanes/Displays.prefPane"

★Click Here to Open This Script 

まんま、同じ内容をshell呼び出しで書くとこんな感じ。

AppleScript名:システム環境設定の「ディスプレイ」>「ディスプレイ」を表示_shell.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/28
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

do shell script "open /System/Library/PreferencePanes/Displays.prefPane"

★Click Here to Open This Script 

System Events経由でファイルオープンするとこんな感じ。POSIX pathをfileとして指定できて驚きました。aliasにcastしないとopenできないかと思っていました。

AppleScript名:システム環境設定の「ディスプレイ」>「ディスプレイ」を表示_System Events.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/28
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "System Events"
  open file "/System/Library/PreferencePanes/Displays.prefPane"
end tell

★Click Here to Open This Script 

AppleScript的にいろいろルーチンを整備して汎用性を持たせつつ、十分な機能を用意するとこんな感じでしょうか。

AppleScript名:システム環境設定の「ディスプレイ」>「ディスプレイ」を表示
— Created 2017-10-10 by Takaaki Naganoya
— Created 2020-04-28 by Takaaki Naganoya
— 2020 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Get every pane ID
set aList to getEveryPaneID() of me

–Get every anchor in a pane
set pList to getEveryAnchorName("com.apple.preference.displays") of me
–> {"displaysArrangementTab", "displaysNightShiftTab", "displaysColorTab", "displaysGeometryTab", "displaysDisplayTab"}

set aRes to dispSystemPreferences("com.apple.preference.displays", "displaysDisplayTab") of me
–> true

on dispSystemPreferences(paneID, anchorID)
  tell application "System Preferences"
    activate
    
set pList to id of every pane
    
if paneID is not in pList then return false
    
    
set current pane to pane paneID
    
tell current pane
      set nList to name of every anchor
      
if anchorID is not in nList then return false
      
reveal anchor anchorID
    end tell
  end tell
  
  
return true
end dispSystemPreferences

on getEveryPaneID()
  tell application "System Preferences"
    return id of every pane
  end tell
end getEveryPaneID

on getEveryAnchorName(paneID)
  tell application "System Preferences"
    set pList to id of every pane
    
if paneID is not in pList then return false
    
set current pane to pane paneID
    
tell current pane
      return name of every anchor
    end tell
  end tell
end getEveryAnchorName

★Click Here to Open This Script 

(Visited 82 times, 1 visits today)
Posted in System | Tagged 10.13savvy 10.14savvy 10.15savvy NSWorkspace System Preferences | Leave a comment

2D Listの各要素を逆順に

Posted on 4月 26, 2020 by Takaaki Naganoya

2D Listの各要素を逆順に入れ替えるAppleScriptです。

そのままreverse ofで逆順にすると、

set aList to {{1}, {2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8, 9}}
set bList to reverse of aList
--> {{3, 4, 5, 6, 7, 8, 9}, {2, 3, 4, 5, 6, 7}, {1}}

のようになりますが、これでは目的にかないませんでした。

ちょうど、Kamenokoのセル回転テーブルを反対方向に回転させたかったので、

--> {{1}, {7, 6, 5, 4, 3, 2}, {9, 8, 7, 6, 5, 4, 3}}

のような結果が欲しかったのでした。本ルーチンを実際に組み込んで、逆方向へのデータ回転を実現させました。

ただ、データ上で回転できているだけで視覚効果については別問題です。プレビュー画像を作成しにいくとメモリ上にゴミが残ったまま消えないとか、あんまり視覚効果を利用しにくい雰囲気がしていますけれども。

AppleScript名:2D Listの各要素を逆順に.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/26
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aList to {{1}, {2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8, 9}}
set bList to reverse2DListEachItems(aList) of me
–> {{1}, {7, 6, 5, 4, 3, 2}, {9, 8, 7, 6, 5, 4, 3}}

on reverse2DListEachItems(aList)
  set newList to {}
  
repeat with i in aList
    set j to contents of i
    
set tmpJ to reverse of j
    
set the end of newList to tmpJ
  end repeat
  
  
return newList
end reverse2DListEachItems

★Click Here to Open This Script 

(Visited 64 times, 2 visits today)
Posted in list | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

Now I’m writing “Switch Control” ebook

Posted on 4月 25, 2020 by Takaaki Naganoya

Now, I’m writing Switch Control ebook. It desribe how easy to make GUI for AppleScript with Switch Control.

# Book images are working version. Not finished yet.

This book will be sold at booth.pm.

(Visited 24 times, 1 visits today)
Posted in PRODUCTS | Tagged Switch Control | Leave a comment

Double PDF v2.1準備中

Posted on 4月 24, 2020 by Takaaki Naganoya

Double PDF v2.1をMac App Storeに出すべく準備中です。このバージョンは、あくまでアプリケーションの署名周りの問題を解決するだけのリリースであり、機能的な追加を行うものではありません。

(Visited 28 times, 1 visits today)
Posted in PRODUCTS | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

ディスプレイのミラーリング表示(sandbox対応)

Posted on 4月 23, 2020 by Takaaki Naganoya

Mac用のオープンソースのディスプレイミラーリング・ツール「mirror-displays」を改変して、Xcode上でコードサインしやすく、かつAppleScriptアプリケーション(Xcode上で記述)から呼び出しやすくしたものです。

ディスプレイのミラーリング表示のOn/Offを行うmirror-displaysを、そのままMac App Storeに申請するアプリケーションの中にバイナリで入れようとしたら、Xcode上のValidate(Mac App Storeにアップロードする前段階の各種妥当性チェック)でひっかかってしまいました。

アプリケーションバンドル中のResourcesフォルダに入れてdo shell scriptで呼ぶという「お気楽」な呼び方が(Code Signの問題で)できなかったわけです。

# コマンドライン・ツールとしてビルドするときにCode Signすればよかったんじゃないか、という話もありますが、いずれ最終的にこの形式にする必要があったので、これでいいんじゃないかと

そこで、コマンドラインから呼び出す形式ではなく、Objective-Cのプログラム「らしい」形式に変更して(ヘッダファイルをゼロから書き起こしました)、AppleScriptから呼び出しやすく変更してみました。配布条件がGPLだったので、ここにmirror-displayまわりのソースと最低限の呼び出し側のAppleScriptアプリケーションのプロジェクトを掲載した次第です。

–> Download Xcode Project’s zip-archive

AppleScript名:AppDelegate.applescript
—
— AppDelegate.applescript
— mirrorTest
—
— Created by Takaaki Naganoya on 2020/04/15.
— Copyright © 2020 Takaaki Naganoya. All rights reserved.
—

script AppDelegate
  property parent : class "NSObject"
  
  
— IBOutlets
  
property theWindow : missing value
  
  
on applicationWillFinishLaunching:aNotification
    — Insert code here to initialize your application before any files are opened
  end applicationWillFinishLaunching:
  
  
on applicationShouldTerminate:sender
    — Insert code here to do any housekeeping before your application quits
    
return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
  
on clicked:sender
    current application’s mirrorObjC’s alloc()’s init()’s mirror()
  end clicked:
end script

★Click Here to Open This Script 

(Visited 126 times, 1 visits today)
Posted in Sandbox System | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

ファイル保存ダイアログ(SavePanel)表示

Posted on 4月 22, 2020 by Takaaki Naganoya

Mac App StoreアプリケーションをAppleScriptで作ったときに、ファイル保存ダイアログを実装するのにchoose file nameコマンドでは拡張子の処理に問題があります(ユーザーが拡張子まで入力しなかった場合への対処を行うと逆効果に)。

choose file nameコマンド
ユーザーが選択、入力したパス:Macintosh HD:Users:me:Desktop:aaaaa
Script側で拡張子を補足:Macintosh HD:Users:me:Desktop:aaaaa.kame
--> エラー(Sandbox制限による)

その回避策としてありものを引っ張り出してきて手直ししたNSSavePanelベースのファイル保存ダイアログ表示サブルーチンです。


▲choose file nameコマンドで表示したファイル名入力ダイアログ。ダイアログ上でタグの選択はできるがchoose file nameにはタグ受信のための機能がない。また、拡張子が入力されていなかったらそのまま


▲本Scriptで表示したNSSavePanelによる保存ファイル名入力ダイアログ。タグの選択、指定した拡張子の指定などが行える。ユーザーが拡張子を入力していなくても自動で補える

choose file nameによるダイアログも、NSSavePanelによるダイアログも、見た目は同じで見分けはつきません。

普通にAppleScriptを書いて自分で実行している分にはchoose file nameコマンドでも問題はありません。

AppleScript名:ファイル保存ダイアログ(SavePanel)表示
— Original by Shane Stanley
— Modified 2020-04-22 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property savePath : ""
property saveTag : {}

set savePath to ""
set saveTag to {}

set aParam to {extList:{"kame"}, saveMsg:"新規ファイルの名前と場所を指定", saveName:"Some name", initialDir:(POSIX path of (path to documents folder))}
my performSelectorOnMainThread:"showModalSave:" withObject:(aParam) waitUntilDone:true
return {savePath:savePath as string, saveTags:saveTag as list}
–> {savePath:"/Users/me/Desktop/Some name.kame", saveTags:{"ブルー", "ヤンキー"}}

on showModalSave:sender
  set tmpExt to extList of sender as list
  
set tmpMsg to saveMsg of sender as string
  
set defName to saveName of sender as string
  
set tmpInit to initialDir of sender as string
  
  
using terms from scripting additions
    set startURL to current application’s NSURL’s fileURLWithPath:(tmpInit)
  end using terms from
  
  
set thePanel to current application’s NSSavePanel’s savePanel()
  
tell thePanel
    its setMessage:(tmpMsg)
    
its setAllowedFileTypes:(tmpExt)
    
its setNameFieldStringValue:(defName)
    
its setShowsHiddenFiles:false
    
its setTreatsFilePackagesAsDirectories:false
    
–its setDirectoryURL:startURL–指定しないほうが前回呼び出し時と同じフォルダが表示されて便利
    
set returnCode to its runModal() as integer
  end tell
  
  
if returnCode = (current application’s NSFileHandlingPanelOKButton) as integer then
    set my savePath to thePanel’s |URL|()’s |path|()
    
    
if (thePanel’s respondsToSelector:"tagNames") as boolean then
      set my saveTag to thePanel’s tagNames()
    end if
    
  else
    — cancel button
    
error -1
  end if
end showModalSave:

★Click Here to Open This Script 

(Visited 316 times, 2 visits today)
Posted in dialog File path Sandbox | Tagged 10.13savvy 10.14savvy 10.15savvy NSSavePanel NSURL | Leave a comment

Xcode上で作成したアプリケーション上でDark Mode検出

Posted on 4月 22, 2020 by Takaaki Naganoya

Xcode上で作成したAppleScriptアプリケーションでDark Modeの検出を行いたいときに、NSAppearance’s currentAppearance()で取得したら、正しくModeの検出が行えませんでした。同じコードをスクリプトエディタ/Script Debugger上で動かした場合には正しくModeの判定が行えているのですが。

そこで、System Eventsの機能を用いてMode判定を行うように処理を書き換えたりしてみたのですが、Mac App Storeに出すアプリケーションでこの処理を記述していたら、これを理由にリジェクトされてしまいました。

仕方なく解決策を探してみたところ、macOS 10.13用に書いたshell scriptによる迂回処理を、そのまま他のOSバージョンでも動かせばよいのではないかと気づき、結局そこに落ち着きました。

AppleScript名:Dark Modeの検出(Xcode上でも正しく判定)
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/22
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set apRes to retLightOrDark() of me
–> true (Dark), false (Light)

on retLightOrDark()
  try
    set sRes to (do shell script "defaults read -g AppleInterfaceStyle")
    
return (sRes = "Dark") as boolean
  on error
    return false
  end try
end retLightOrDark

★Click Here to Open This Script 

(Visited 46 times, 1 visits today)
Posted in shell script System | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

Sandbox環境で封じられる処理

Posted on 4月 22, 2020 by Takaaki Naganoya

ふだん、AppleScriptの処理はOSによる制約の少ない環境で処理を行っています。サンドボックスによる制約がない、あるいはきわめて少ない環境であるといえます。

そうした自由な環境に慣れていると、Sandbox環境下で(Xcode上で)アプリケーションを作成したときに、あっと驚く制約が存在していて驚かされることが多々あります。

他のアプリケーションを操作するScriptは書き込み禁止に

Sandbox環境下では、他のアプリケーションを操作するAppleScriptについては、実行専用形式で保存したうえでファイル書き込み不可の状態にしておく必要があります。Scriptのプロパティ(resultもプロパティ)を書き換えられてはいけないので、この措置が必要になります。Sandbox環境ではこのプロパティの書き換えが、自己書き換えと判定され、禁則事項に該当してしまうためです。

ファイルの新規保存時のchoose file nameコマンド(+拡張子追加)が抵触

ファイルの新規保存時にchoose file nameコマンドで保存先のパス+ファイル名をユーザーに指定させるような処理を行っています。このときに、ファイルの拡張子が指定されていない場合には、choose file nameから返ってきたパスを文字列に変換して、拡張子を文字列で追加するような処理もよく行っています。

これが、Sandbox環境下では禁じ手になります。

最初に遭遇したときには「意味がわからない!」と、イラつきまくりましたが、落ち着いて考えつつ追試を行ってみたところ理解できました。

Sandbox化したアプリケーションでファイル保存を行うためには、Xcode上でentitlementsファイルを編集し、「com.apple.security.files.user-selected.read-write」といったエントリに「YES」を設定しておくことになります。ユーザーが選択したファイルの読み書きを許可するという設定です。

このとき、choose file nameコマンドで入力されなかった拡張子の部分をAppleScript側で勝手に補ってしまうと、「ユーザーが指定したファイル」以外のファイルを処理することになるわけで、(拡張子を文字列操作で補ったようなパスは)ファイル保存することができませんでした。

そこで、NSSavePanelを用いてファイル保存ダイアログを作成する必要に迫られます。事前に補うべき拡張子の情報を渡してダイアログ表示を行うので、ユーザーが拡張子まで入力しなくても、拡張子がついたファイルパスが返ってきます。

ほかにもいろいろありますが、作成するAppleScriptのプログラム全体の数から見ると、Sandbox環境下で動かすAppleScriptはごく一部であるため、あまりノウハウが蓄積されていません。たまーにSandboxの制約に抵触して驚かされるといったところでしょうか。

(Visited 54 times, 1 visits today)
Posted in How To Sandbox | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

新型アプリケーション「Kamenoko」をMac App Storeレビューに提出

Posted on 4月 21, 2020 by Takaaki Naganoya

本Blogにちょくちょく、その作例を掲載している6角形図形を組み合わせた図でアイデアを練るソフトウェア「Kamenoko」をMac App Storeのレビューに送信しました。2作目なので、レビューの概要もわかっており(1発で通ることはないので)前回のように却下に対して24時間即時対応、みたいな体勢にはしていません。しばらくApp Storeの審査とやりとりする必要があることを理解しています(1週間ぐらいでしょうか)。

目下App Store系の審査の担当者の人数が少ないために審査にかかる時間が長くなっているとの噂がありますが、これまでもMac App Storeの審査は時間がかかっていたので(前回1か月かかったし)、そんなもんだと思っています。

このKamenoko v1.0はAppleScriptからのスクリプティングには対応していません。ただ、中身はほとんど(99.9%)AppleScriptで書いてあるので、対応は難しくはありません。

難しくはないものの、このアプリケーションはAppleScriptによって操作してもあまり意味がないので(アイデアを練るためのアプリケーション)、書類の作成とか書類の内容取得、書類の内容変更などが行えるライブラリをアプリケーションに同梱する方向でAppleScriptサポート機能の実装を検討しています。

■リジェクト1発目

System Eventsを呼んでいることを問題視されているようです。当初、このアプリケーションでSystem Eventsを呼び出す必要はなかったのですが、Dark Modeの検出がmacOS 10.13でうまく働かなかったので、System EventsでDark Modeの検出を行わせるようにしていました。

Mac App Storeのレビューでは、指摘事項同士の矛盾については考慮されていません。(A)という指摘をされたので(B)という対応をしたら、説明の仕方を変更するだけで実は(A)の状態のままでよかった、ということがままあります。また、アプリケーション全体で複数の指摘項目があったとしても、1回のレビューで指摘されるのはだいたい1項目だけです。

なので、1項目の指摘事項があったので即時修正したとしても、さみだれ式に次々と指摘事項が(枝葉末節の問題から、徐々にセキュリティ系の事項へと)上がってくるものなので、「そういうもんだ」と割り切っていないとやりきれません。

System Eventsに依存しないAppearanceの判定を行わせると、macOS 10.13でDark Modeの検出ができなくなる可能性があり、macOS 10.13でDark Modeの検出ができなくなると、macOS 10.13対応が不適切であるという指摘が未来のタイミングで行われ、結局macOS 10.13対応を行うためにはSystem Eventsを呼ぶしかなかった、という結論に落ち着きそうな感じがとっても嫌な雰囲気です。

ただ、これまで1発目のレビュー(リジェクト)とかいうと、10分ぐらいであっという間に終わっていたので(溢れる「流れ作業」感)、1時間もかかったのは新記録ではないでしょうか。お茶飲みながらの1時間かもしれませんけれども。

■リジェクト2発目(2020/5/1)

のちのちのバージョンでやりたい(実現できるとは言っていない)機能のために宣言しておいたEntitlementsが、アプリケーションの機能に存在していないので却下、と判断されました。

あれ? AppleのWeb側にもXcode上にもそんな設定は(残ってい)ないのですがー(ーー;;;

(Visited 206 times, 3 visits today)
Posted in PRODUCTS | Tagged 10.13savvy 10.14savvy 10.15savvy | Leave a comment

ディスプレイの設定切り替えをAppleScriptでも行えるSwitchResX

Posted on 4月 19, 2020 by Takaaki Naganoya

Cocoaの機能やサードパーティのFrameworkの機能を呼び出して、ディスプレイの回転コントロールを行なったり、解像度の変更を行うなどの制御がAppleScript単体でできるようになってきましたが、ディスプレイの位置関係を定義して変更したいとか凝ったことを言い出すと、さすがにまだまだ市販のツール類を併用したほうが話が早かったりします。

以前からSwitchResX(16USドル)がAppleScriptからコントロールできることを知っていましたが、どの程度のコントロールが行えるかについては調べたことはありませんでした。どのように使えて、どのようにコントロールできて、どのような点に注意すべきなのかを実際に調べてみました。

SwitchResX v4.9.2を実際にためしてみた

SwitchResXの解像度変更機能自体は水準通りだと思います。この手のツールは割といろいろ存在しており、自分はQuickResを利用しています。どちらのツールも画面のオーバースキャン(実解像度よりも大きな解像度の表示)ができます。画面の回転や色数、画面の相対位置の変更、アプリケーションごとに解像度を変更する機能などが本ツールの特徴でしょう。

画面解像度の変更機能について1つ、QuickResを大幅に上回っているものがありました。HiDPI(Retina解像度)の表示サポートです。非Retina解像度のディスプレイであっても、表示ピクセル数を抑えることでHiDPI表示を可能とするもので、表示すること自体にはあまり意味はありませんが、「HiDPI解像度の画面キャプチャが行える」というメリットがあります(非Retina環境でRetina解像度の画面キャプチャが行えるのは、資料作成時にとても便利)。このあたり、Mac miniでちょっと困らされた点でした。

Mac mini 2014(macOS 10.15.4)+HDMI Display(1980x1080)の組み合わせで、OSが標準で提供しているのは960x540解像度におけるHiDPI表示ですが、SwitchResXは1280x800 / 960x600 / 840x525 / 1280x720 / 800x450 などの解像度におけるHiDPI表示もサポートしています。

SwitchResXはメニューバーに常駐して、解像度設定の切り替えが行えるようになっています。設定自体はこのメニューバーから「SwitchResX Preferences…」を選択するか、システム環境設定の「SwitchResX」を選択すると起動される「SwitchResX Control」というGUIアプリケーションから行います。

SwitchResXでは、画面解像度、色(カラー、グレイ)、表示ON/OFF、ミラーリング、メインディスプレイ、画面回転、カラープロファイルなどを設定したディスプレイセットを作成し、このディスプレイセットをメニューやキーボードショートカット、コンテクストメニューから切り替えられるようになっており、その一環としてAppleScriptから同様にディスプレイセットの切り替えが行えるようになっています。

AppleScriptからのコントロール

SwitchResXのAppleScript用語辞書は以下のとおりです。

AppleScriptによるコントロールは「SwitchResX Daemon」という不可視アプリケーションプロセスに対して行うようになっており、基本的には各種ディスプレイの状態を取得(Read Only)することと若干の基本的なコマンドの実行と、ユーザーが定義したディスプレイセットの切り替えを行う、という内容です。

AppleScript側からこまかくディスプレイのIDを指定して個別にグレースケール表示を行わせるといったことはできません。逆に、指定したIDのディスプレイの現在の状況(properties)を取得することはできます。そういう感じです。

けっこう多い注意点

ディスプレイセットを新規作成したあとで、GUI側から明示的に保存(Command-S)を実行しないと、追加したディスプレイセットは認識されませんでした。ここは、たいへんに注意が必要です(なかなか気づきませんでした)。

ディスプレイの回転や位置関係の変更など、込み入った設定変更を行ったディスプレイセットを作成した場合には、それらを元に戻したディスプレイセットを作成しておく必要があります。restore desktopコマンドで元の状態に戻してくれるのかと思っていたのですが、そういうものではないようです。

こういうツールを使おうかと考えているユーザーは複数のディスプレイをMacにつないで使っているはずなので、本ツールで一気に切り替えたあと、戻す設定が定義されていないと手作業で元に戻すというScripterにとってはたいへんに屈辱的な作業を強いられることになります。

ディスプレイセットには、すべての設定項目が同居できるというわけではなく、特定の設定項目のみが単独で切り替えられるというものもあるようなので、この点も注意が必要です(注意点ばっかりやな)。

AppleScriptのプログラム中からSwitchResXの機能を呼び出して画面構成の変更を自由に行えるわけですが、個人的には「そこまでやる必要があるのか?」という印象です。あと、AppleScript用語辞書にサンプルScript掲載しとけとか画面キャプチャのグラフィックを入れておかないとわかりにくいので、そのあたり改善の余地ありといったところでしょうか。他人のアプリケーションについては、山のように不満点や改善点が出てくるものです。

あと、SwitchResXを操作するAppleScriptをどこから呼び出すことになるのか、については少し興味があります。macOS標準搭載のスクリプトメニューから呼び出すのが一番「ありそう」な利用パターンですが、それ単体で呼び出すと「隣り合ったSwitchResXのメニューから呼び出すのとほぼ等価な内容をスクリプトメニューに入れたAppleScriptから呼び出す」という間抜けな状態になります。

まとまった自動処理を行うのに必要な画面設定(速度重視のために画面解像度を極端に落とすとか、メニュー操作が想定外の動きを行わないように特定の解像度に明示的に変更するとか)を行い、処理本体を実行したうえで、後処理で画面設定を元に戻しておく、といったところでしょうか。

AppleScript名:ディスプレイを数えて、プロパティを取得
tell application "SwitchResX Daemon"
  set dCount to count every display
  
–> 2
  
  
repeat with i from 1 to dCount
    properties of display i
    
–> {built in:false, position:{0, 0}, name:"Cinema HD Display", class:display, brightness:-1.0, id:69501836, mirroring:{}, enabled:true, overscan:false, index:1, current mode:mode 1 of display 1 of application "SwitchResX Daemon", orientation:0, underscan:1.0, current depth:32, display profile:profile "Cinema HD Display" of application "SwitchResX Daemon"}
    
    
–> {built in:false, position:{-1920, 0}, name:"Cinema HD", class:display, brightness:-1.0, id:69513479, mirroring:{}, enabled:true, overscan:false, index:2, current mode:mode 1 of display 2 of application "SwitchResX Daemon", orientation:0, underscan:1.0, current depth:32, display profile:profile "Cinema HD" of application "SwitchResX Daemon"}
  end repeat
end tell

★Click Here to Open This Script 


▲display set 2だと2行目の「disp set 2 (90 degree rotation)」が指定される

AppleScript名:display set の切り替え
tell application "SwitchResX Daemon"
  set dSet to display set 2
  
apply dSet
end tell

★Click Here to Open This Script 

(Visited 1,098 times, 4 visits today)
Posted in System | Tagged 10.14savvy 10.15savvy SwitchResX | Leave a comment

プログラムで作ったアラートダイアログ上のTextViewをフォーカスする

Posted on 4月 14, 2020 by Takaaki Naganoya

AppleScriptでダイナミックに(動的に)生成したアラートダイアログ上にテキストビューを生成し、そのテキストビュー上にテキスト入力用のキャレットを移動させる(フォーカスする)AppleScriptです。

Xcode上でAppleScriptでGUIアプリケーションを作っていて、文字サイズの大きなテキスト入力フィールドを動的に生成。さらに、テキスト入力フィールドにテキスト入力用のキャレットを移動させようとして、割と手こずっていました。

テキスト入力を受け付けるGUI部品に対して、入力フォーカスを与え、テキスト入力のキャレットが置かれた状態を再現するには、GUIの画面上からはマウスカーソルでクリック操作するとできます。同じ状態をプログラム側から作る場合にはFirstResponderにセットするという処理になります。

この、FirstResponderに指定することは理解でき、Xcode(Interface Builder)上で配置した部品に対して実行する処理は昔からやっています。NSTextFieldに強制的に入力フォーカスを置いた上でバーコードリーダーからのテキスト入力を受け付けるといった処理です。

一方、プログラムから動的に生成したGUI部品に対して、Cocoaが用意しているAPIのどれを用いて、どのように指定すべきなのかで困りました。

親WindowのFirstResponderにしてみたり、アラートウィンドウのFirstResponderにしてみたり、いろいろ試した末に、

parentWin's setInitialFirstResponder:aView

で、Alert DialogのWindowをparentWinに、alert dialog上のテキストビューをaViewに代入した状態で実行して、入力キャレットをテキストビュー上に移動できました。本Xcode Projectは上記の1行の記述の検証を行うためのテストプロジェクトです。

–> Download Xcode Project

AppleScript名:AppDelegate.applescript
—
— AppDelegate.applescript
— dialogCarret
—
— Created by Takaaki Naganoya on 2020/04/14.
— Copyright © 2020 Takaaki Naganoya. All rights reserved.
—

script AppDelegate
  property parent : class "NSObject"
  
  
property |NSURL| : a reference to current application’s |NSURL|
  
property NSFont : a reference to current application’s NSFont
  
property NSView : a reference to current application’s NSView
  
property NSAlert : a reference to current application’s NSAlert
  
property NSColor : a reference to current application’s NSColor
  
property NSTextView : a reference to current application’s NSTextView
  
property NSScrollView : a reference to current application’s NSScrollView
  
property NSRunningApplication : a reference to current application’s NSRunningApplication
  
  
— IBOutlets
  
property theWindow : missing value
  
  
property returnCode : 0
  
property resStr : ""
  
  
  
on applicationWillFinishLaunching:aNotification
    —
  end applicationWillFinishLaunching:
  
  
on applicationShouldTerminate:sender
    return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
  
on clicked:sender
    set tmpStr to "TEST"
    
set paramObj to {myMessage:"Input Text", mySubMessage:"", mes1:(tmpStr), mesWidth:400, mesHeight:200}
    
my performSelectorOnMainThread:"dispTextViewWithAlertdialog:" withObject:paramObj waitUntilDone:true
    
set aResText to (my resStr) as string
    
    
display dialog aResText
  end clicked:
  
  
on dispTextViewWithAlertdialog:paramObj
    –Receive Parameters
    
set aMainMes to (myMessage of paramObj) as string –Main Message
    
set aSubMes to (mySubMessage of paramObj) as string –Sub Message
    
set mesStr to (mes1 of paramObj) as string –Text Input field 1 Label
    
set aWidth to (mesWidth of paramObj) as integer –TextView width
    
set aHeight to (mesHeight of paramObj) as integer –TextView height
    
    
set vNum to system attribute "sys2"
    
if vNum > 13 then
      set apRes to retLightOrDark() of me
      
if apRes = true then
        set textColor to (current application’s NSColor’s cyanColor())
      else
        set textColor to (current application’s NSColor’s blackColor())
      end if
    else
      set textColor to (current application’s NSColor’s blackColor())
    end if
    
    
    
— Create a TextView with Scroll View
    
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
    
set aView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
    
aView’s setDelegate:me
    
aView’s setRichText:true
    
aView’s useAllLigatures:true
    
aView’s setTextColor:textColor
    
aView’s setFont:(current application’s NSFont’s systemFontOfSize:90.0)
    
set aColor to current application’s NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.1
    
    
aView’s setBackgroundColor:aColor
    
aView’s setString:mesStr
    
aScroll’s setDocumentView:aView
    
aView’s enclosingScrollView()’s setHasVerticalScroller:true
    
    
— set up alert
    
set theAlert to NSAlert’s alloc()’s init()
    
tell theAlert
      its setMessageText:aMainMes
      
its setInformativeText:aSubMes
      
its addButtonWithTitle:"OK"
      
–its addButtonWithTitle:"Cancel"
      
its setAccessoryView:aScroll
      
set parentWin to its |window|()
    end tell
    
    
parentWin’s setAlphaValue:0.8
    
–parentWin’s setOpaque:(false)
    
parentWin’s setLevel:(current application’s NSScreenSaverWindowLevel)
    
    
parentWin’s setInitialFirstResponder:aView —Place Input Carret to Alert Dialog
    
    
— show alert in modal loop
    
NSRunningApplication’s currentApplication()’s activateWithOptions:0
    
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
    
    
if (my returnCode as number) = 1001 then
      —
    else
      set my resStr to aView’s |string|()
    end if
  end dispTextViewWithAlertdialog:
  
  
  
on doModal:aParam
    set (my returnCode) to aParam’s runModal()
  end doModal:
  
  
  
on retLightOrDark()
    return true
  end retLightOrDark
  
end script

★Click Here to Open This Script 

(Visited 145 times, 1 visits today)
Posted in AppleScript Application on Xcode dialog How To | Tagged 10.13savvy 10.14savvy 10.15savvy NSAlert NSColor NSFont NSMakeRect NSRunningApplication NSScrollView NSTextView | Leave a comment

システムフォントの名称を取得

Posted on 4月 13, 2020 by Takaaki Naganoya

システムフォントの名称を取得するAppleScriptです。

GUIベースのアプリケーションを作っているときに、「もう、無難なフォントなんでもいいからとりあえず指定できるものがあれば指定しておこうよ」という局面はあります。ないフォントを指定するとクラッシュすることもあるので、とりあえずコレ指定しておけば大丈夫だから!

という「安全パイ」のフォントとしてSystem Fontを取得&指定したいという時に書いたScriptでもあります。プログラム内容がつまらない割には、切実なニーズを満たすためのものです。

AppleScript名:システムフォントの名称を取得.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2020/04/06
—
–  Copyright © 2020 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aFont to current application’s NSFont’s systemFontOfSize:24.0
set aInfo to aFont’s fontDescriptor()
set aSize to (aInfo’s pointSize()) as real
set aPSName to (aInfo’s postscriptName()) as string
–> ".SFNSDisplay"

set bFont to current application’s NSFont’s boldSystemFontOfSize:24.0
set bInfo to bFont’s fontDescriptor()
set bSize to (bInfo’s pointSize()) as real
set bPSName to (bInfo’s postscriptName()) as string
–> ".SFNSDisplay-Bold"

★Click Here to Open This Script 

(Visited 24 times, 1 visits today)
Posted in AppleScript Application on Xcode Font | Tagged 10.13savvy 10.14savvy 10.15savvy NSFont | Leave a comment

NSFontPanelでフォントを選択

Posted on 4月 7, 2020 by Takaaki Naganoya

自前でメニューを作ってフォントおよびサイズの選択を行おうとするとめんどくさい(時間がかかるし管理も大変)ので、NSFontPanelを用いてフォントを選択する試作品を作ってみました。

ただし、まだ完全ではなく「1つ前の状態が取得できる」という状態なので、修正が必要です。作成はmacOS 10.14.6+Xcode 11.3.1上で行っています。

–> Download Xcode Project

AppleScript名:AppDelegate.applescript
—
— AppDelegate.applescript
— fontPanel
—
— Created by Takaaki Naganoya on 2020/03/12.
— Copyright © 2020 Takaaki Naganoya. All rights reserved.
—

script AppDelegate
  property parent : class "NSObject"
  
  
— IBOutlets
  
property theWindow : missing value
  
property theField : missing value
  
  
property aFontNameField : missing value
  
property aFontSizeField : missing value
  
  
property aFontManager : missing value
  
  
property font : missing value
  
property aFP : missing value
  
  
on applicationWillFinishLaunching:aNotification
    set aFontManager to current application’s NSFontManager’s sharedFontManager()
    
aFontManager’s setAction:"appSpecificChangeFont:"
    
set aFP to current application’s NSFontPanel’s sharedFontPanel()
    
set aFont to current application’s NSFont’s fontWithName:"Helvetica" |size|:16
    
aFontManager’s setSelectedFont:aFont isMultiple:false
    
    
theField’s setStringValue:"ぴよまるソフトウエア, Piyomaru Software"
  end applicationWillFinishLaunching:
  
  
on applicationShouldTerminate:sender
    — Insert code here to do any housekeeping before your application quits
    
return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
  
on clicked:aSender
    theWindow’s makeFirstResponder:theField
    
aFP’s makeKeyAndOrderFront:me
  end clicked:
  
  
on appSpecificChangeFont:sender
    set aSelFont to sender’s selectedFont()
    
set aDesc to aSelFont’s fontDescriptor()
    
set aPSID to (aDesc’s postscriptName()) as string
    
set aSize to (aDesc’s pointSize()) as real
    
theField’s setFont:aSelFont
    
aFontNameField’s setStringValue:aPSID
    
aFontSizeField’s setStringValue:(aSize as string)
  end appSpecificChangeFont:
end script

★Click Here to Open This Script 

(Visited 50 times, 1 visits today)
Posted in AppleScript Application on Xcode Font | Tagged 10.13savvy 10.14savvy 10.15savvy NSFont NSFontManager NSFontPanel | 1 Comment

パネル構成書類「入出力音量調整」

Posted on 4月 3, 2020 by Takaaki Naganoya

新型コロナウィルスの蔓延により、リモートワークでビデオ/オーディオチャットを行う機会が増え、音量調整のためのいい方法がないと嘆いている方が多いようなので、便利な方法をご紹介します。

macOSにはアクセシビリティ系(障害者支援)の機能が多々用意されており、その中核をなす「スイッチコントロール」という機能があります。この機能を利用して、画面上に操作可能な機能ボタンを貼り付けておくことができるため、オーディオ系の音量調整機能などをここに割り振って利用すると便利です。

# 業務系のScriptをフローティングパレット表示しておいて、ワンクリックで実行できるようにしておくと、とても便利だと思います

このスイッチコントロールの機能はユーザーごとに自由にカスタマイズすることができ、さらにその内容を書類に書き出して他のユーザーと共有できるようになっています。

それがパネル構成書類です。これはmacOS標準装備の機能「スイッチコントロール」の書類で、ボタン定義データやボタンから呼び出すAppleScriptが内包されています。

–> Download switchControlUserPanels1.ascconfig(12KB)

[Update] English Version included in same panel

このパネル構成書類はmacOS 10.14上で作成し、macOS10.14.6およびmacOS 10.15.4上での動作を確認しています。

Zipアーカイブから展開して、ユーザーの~/Library/Application Support/com.apple.AssistiveControlフォルダに入れてください。macOS 10.15上では展開後.ascconfig書類がフォルダに見えるかもしれませんが、気にせずそのまま上記フォルダに移動してください。

# 「com.apple.AssistiveControl」フォルダがApplication Support内に存在しない場合には作成してください。このフォルダ名を間違えるとOSに認識されないので、ご注意ください

「システム環境設定」を起動。

「アクセシビリティ」を選択。

左の一覧リストの下の方にある「スイッチコントロール」を選択。

「スイッチコントロールを有効にする」にチェックを入れます。この際に、管理者パスワードを求められる場合があります。

フローティング操作パレット「スイッチコントロール」のホーム画面が表示されるので、「カスタム」をクリック。

カスタムの中にさきほど追加した「入出力音量調整」があるので、それをクリック(”Sound Volume Control” is English version)。

これで、フローティングパレットの入出力音量調整パレットが表示されます。


▲SwitchControlの操作方法。macOS標準のGUI体系とは若干異なる

パレット上の各ボタンをクリックするか、あるいはMacのサブディスプレイとしてiPadを接続している場合には、iPadの画面上を指でタップすると各ボタンの機能が実行されます。

スイッチコントロールのフローティングパレットはパレット左上の「x」をクリックすると消去できます。いったん消去しても、ふたたび「システム環境設定」>「アクセシビリティ」>「スイッチコントロール」に戻って、「スイッチコントロールを有効にする」のチェックを入れると表示されます。

iPadをMacのサブディスプレイとして利用するアプリケーションは、Apple純正のSide Car(macOS 10.15標準装備)のほか、サードパーティ製の「duet display」や「AirDisplay」などが有名です。

とくに、サードパーティ製のアプリケーションは、古めのiOSや古めのiPadをサポートしている(Apple純正機能は最新のiPadしかサポートしていない)ため、身の回りに転がっている古いiPadや中古のiPadをMacのタッチ操作コントローラーにお安く利用できるため、とてもおすすめです。

(Visited 90 times, 1 visits today)
Posted in news Tools | Tagged 10.14savvy 10.15savvy Switch Control | Leave a comment

NSURLSessionでREST API呼び出しv4.4.2a

Posted on 4月 3, 2020 by Takaaki Naganoya

REST API呼び出しに欠かせない部品をアップデートしました。URLキャッシュが効くようになったような気がします。

–> GET method REST API v4.4.1
–> GET method REST API v4.4
–> GET method REST API v4.3
–> GET method REST API v4.1
–> GET method REST API v4

前バージョンではmacOS 10.15上でクラッシュしないかわりにURLキャッシュが効かないという特徴がありました。ただ、edama2さんと協議した結果、macOS 10.15上でクラッシュする原因と考えられていた内容にあまり根拠がないことがわかってきました(人ごとではなく自分のことなのですが)。

可能な範囲でトライアル&エラーで調査を行なったところ、本バージョンのような処理に落ち着きました。AppleScriptからのCocoa利用については明確にドキュメントがAppleから出ているわけではないので、すでに存在するObjective-Cのプログラムの処理を参考にしつつ、Objective-CからAppleScriptへの置き換えが可能かを検討しています。

URLキャッシュについては、(当然のことながら)処理1回目には効きません。なぜか2回目も効きません。3回目とか4回目あたりから効いていることが実感できる感じです(回数ではなく、前回処理時からの経過時間を見ているのかも? GUIアプリケーションに入れて使うと2回目から効いたりします)。URLキャッシュが効いていない場合には1.5秒ぐらい、URLキャッシュが効き出すと0.02秒ぐらいで結果が返ってきています。

macOS 10.13/14/15で検証を行い、繰り返し処理を行ってもクラッシュしないことを確認しています。ただ、動作保証するというレベルではないので(本Blog掲載のScriptすべてそうですが)、問題があったら知らせてください。

ためしに、Xcode上で作成したGUIベースのAppleScriptアプリケーションに本処理を導入したところ、REST APIへの問い合わせがキャッシュされて著しい高速化を実現できました。

AppleScript名:GET method REST API v4.4.2a_wikipedia.scptd
— Created 2019-05-02 by Takaaki Naganoya
— Modified 2020-04-03 by Takaaki Naganoya
— 2020 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property |NSURL| : a reference to current application’s |NSURL|
property NSString : a reference to current application’s NSString
property NSURLCache : a reference to current application’s NSURLCache
property NSURLSession : a reference to current application’s NSURLSession
property NSMutableData : a reference to current application’s NSMutableData
property NSURLQueryItem : a reference to current application’s NSURLQueryItem
property NSOperationQueue : a reference to current application’s NSOperationQueue
property NSURLComponents : a reference to current application’s NSURLComponents
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSMutableURLRequest : a reference to current application’s NSMutableURLRequest
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSURLSessionConfiguration : a reference to current application’s NSURLSessionConfiguration
property NSURLRequestReturnCacheDataElseLoad : a reference to current application’s NSURLRequestReturnCacheDataElseLoad

property retData : missing value
property retCode : 0
property retHeaders : 0
property drecF : false
property aSession : missing value
property aCache : missing value

on run
  set retData to missing value
  
set drecF to false
  
set aSession to missing value
  
  
set aQueryKeyTitle to "AppleScript"
  
set aRes to getWikiText(aQueryKeyTitle) of me
  
return aRes
end run

on getWikiText(aQueryKeyTitle)
  set reqURLStr to "https://en.wikipedia.org/w/api.php"
  
set aRec to {action:"parse", page:aQueryKeyTitle, |prop|:"wikitext", format:"json"}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
  
set aRes to callRestGETAPIAndParseResults(aURL, 5) of me
  
if aRes = missing value then return false
  
  
set bRes to (aRes’s valueForKeyPath:"parse.wikitext.*") as string
  
return bRes
end getWikiText

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(reqURLStr as string, timeoutSec as integer)
  set (my retData) to NSMutableData’s alloc()’s init()
  
set (my retCode) to 0
  
set (my retHeaders) to {}
  
set (my drecF) to false
  
  
–if my aCache = missing value then
  
set cachePath to (POSIX path of (path to library folder from user domain)) & "/Caches/AppleScriptURLCache"
  
set my aCache to NSURLCache’s alloc()’s initWithMemoryCapacity:512000 diskCapacity:1024 * 1024 * 5 diskPath:cachePath
  
–end if
  
NSURLCache’s setSharedURLCache:(my aCache)
  
  
  
set aURL to |NSURL|’s URLWithString:reqURLStr
  
set aRequest to NSMutableURLRequest’s requestWithURL:aURL
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setTimeoutInterval:timeoutSec
  
aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
  
aRequest’s setValue:"AppleScript/Cocoa" forHTTPHeaderField:"User-Agent"
  
aRequest’s setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type"
  
  
  
set aConfig to NSURLSessionConfiguration’s defaultSessionConfiguration()
  
aConfig’s setRequestCachePolicy:(NSURLRequestReturnCacheDataElseLoad)
  
  
aConfig’s setURLCache:(my aCache)
  
  
–どちらでも速度差がない
  
set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(NSOperationQueue’s mainQueue())
  
–set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(missing value)
  
  
set aTask to aSession’s dataTaskWithRequest:aRequest
  
  
aTask’s resume() –Start URL Session
  
  
repeat (1000 * timeoutSec) times
    if (my drecF) is not equal to false then
      exit repeat
    end if
    
delay "0.001" as real
  end repeat
  
  
–delegateの無効化
  
my aSession’s finishTasksAndInvalidate()
  
set my aSession to missing value
  
  
return my parseSessionResults()
end callRestGETAPIAndParseResults

on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData
  (my retData)’s appendData:tmpData
end URLSession:dataTask:didReceiveData:

on URLSession:tmpSession task:tmpTask didCompleteWithError:tmpError
  if tmpError = missing value then
    set (my drecF) to true
  else
    error "Donwload Failed"
  end if
end URLSession:task:didCompleteWithError:

on parseSessionResults()
  set resStr to NSString’s alloc()’s initWithData:(my retData) encoding:(NSUTF8StringEncoding)
  
set jsonString to NSString’s stringWithString:(resStr)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
return aJsonDict
end parseSessionResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set 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 (NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

★Click Here to Open This Script 

(Visited 72 times, 1 visits today)
Posted in Internet JSON REST API URL | Tagged 10.13savvy 10.14savvy 10.15savvy NSJSONSerialization NSMutableData NSMutableDictionary NSMutableURLRequest NSOperationQueue NSString NSURL NSURLCache NSURLComponents NSURLQueryItem NSURLRequestReturnCacheDataElseLoad NSURLSession NSURLSessionConfiguration NSUTF8StringEncoding | 1 Comment

NSURLSessionでREST API呼び出しv4.4.1

Posted on 4月 2, 2020 by Takaaki Naganoya

REST API呼び出しに欠かせない部品をアップデートしました。macOS 10.15でクラッシュしないように書き換えています。

–> GET method REST API v4.4
–> GET method REST API v4.3
–> GET method REST API v4.1
–> GET method REST API v4

前バージョンではmacOS 10.15上でクラッシュするかわりにmacOS 10.14上で高速という特徴がありました。ただ、macOS 10.15上でクラッシュしないように調整を行っていくと、処理速度については別段速くもなく(キャッシュがヒットしていない雰囲気)、よりいっそうキャッシュについて調べる必要が出てきています。

一応、キャッシュの作り方については調べてみたものの、

キャッシュが効いている雰囲気がまったくありません。

キャッシュの内容を確認してみると、からっぽですね(^ー^; もう少し(時間のあるときに)キャッシュの連携などを調べておくといいのかもしれません。最低限、macOS 10.15でクラッシュしなくなったので、NSURLConnectionを呼び出してリジェクトを喰らわないように準備しておくという感じでしょうか。

AppleScript名:GET method REST API v4.4.1_wikipedia.scptd
— Created 2019-05-02 by Takaaki Naganoya
— Modified 2020-04-02 by Takaaki Naganoya
— 2020 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property |NSURL| : a reference to current application’s |NSURL|
property NSString : a reference to current application’s NSString
property NSURLSession : a reference to current application’s NSURLSession
property NSMutableData : a reference to current application’s NSMutableData
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSMutableURLRequest : a reference to current application’s NSMutableURLRequest
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSURLSessionConfiguration : a reference to current application’s NSURLSessionConfiguration

property retData : missing value
property retCode : 0
property retHeaders : 0
property drecF : false
property aSession : missing value
property aCache : missing value

on run
  set retData to missing value
  
set drecF to false
  
set aSession to missing value
  
  
set aQueryKeyTitle to "AppleScript"
  
set aRes to getWikiText(aQueryKeyTitle) of me
  
return aRes
end run

on getWikiText(aQueryKeyTitle)
  set reqURLStr to "https://en.wikipedia.org/w/api.php"
  
set aRec to {action:"parse", page:aQueryKeyTitle, |prop|:"wikitext", format:"json"}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
  
set aRes to callRestGETAPIAndParseResults(aURL, 5) of me
  
if aRes = missing value then return "Error"
  
  
set bRes to (aRes’s valueForKeyPath:"parse.wikitext.*") as string
  
return bRes
end getWikiText

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(reqURLStr as string, timeoutSec as integer)
  set (my retData) to NSMutableData’s alloc()’s init()
  
set (my retCode) to 0
  
set (my retHeaders) to {}
  
set (my drecF) to false
  
  
–if my aCache = missing value then
  
set cachePath to (POSIX path of (path to library folder from user domain)) & "/Caches/AppleScriptURLCache"
  
set my aCache to current application’s NSURLCache’s alloc()’s initWithMemoryCapacity:512000 diskCapacity:1024 * 1024 * 5 diskPath:cachePath
  
–end if
  
current application’s NSURLCache’s setSharedURLCache:(my aCache)
  
  
  
set aURL to |NSURL|’s URLWithString:reqURLStr
  
set aRequest to NSMutableURLRequest’s requestWithURL:aURL
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setTimeoutInterval:timeoutSec
  
aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
  
aRequest’s setValue:"AppleScript/Cocoa" forHTTPHeaderField:"User-Agent"
  
aRequest’s setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type"
  
  
set v2 to system attribute "sys2" –> 14
  
if v2 < 15 then
    –macOS 10.14まで
    
set aConfig to NSURLSessionConfiguration’s defaultSessionConfiguration()
  else
    –macOS 10.15以降。10.15でdefaultSessionConfigurationを使うとクラッシュしやすい???
    
set identifier to "BackgroundSessionConfiguration"
    
set aConfig to NSURLSessionConfiguration’s backgroundSessionConfiguration:(identifier)
  end if
  
  
aConfig’s setRequestCachePolicy:(current application’s NSURLRequestReturnCacheDataElseLoad)
  
  
aConfig’s setURLCache:(my aCache)
  
–aConfig’s setTimeoutIntervalForRequest:0.5
  
–aConfig’s setTimeoutIntervalForResource:timeoutSec
  
–aConfig’s setWaitsForConnectivity:false
  
  
–どちらでも速度差がない
  
set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(current application’s NSOperationQueue’s mainQueue())
  
–set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(missing value)
  
  
set aTask to aSession’s dataTaskWithRequest:aRequest
  
  
aTask’s resume() –Start URL Session
  
  
repeat (1000 * timeoutSec) times
    if (my drecF) is not equal to false then
      exit repeat
    end if
    
delay "0.001" as real
  end repeat
  
  
–aSession’s invalidateAndCancel() –delegateの無効化
  
my aSession’s finishTasksAndInvalidate()
  
set my aSession to missing value
  
  
return my parseSessionResults()
end callRestGETAPIAndParseResults

on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData
  (my retData)’s appendData:tmpData
end URLSession:dataTask:didReceiveData:

on URLSession:tmpSession dataTask:tmpTask willCacheResponse:cacheRes completionHandler:aHandler
  set (my drecF) to true
end URLSession:dataTask:willCacheResponse:completionHandler:

on URLSession:tmpSession task:tmpTask didCompleteWithError:tmpError
  if tmpError = missing value then
    set (my drecF) to true
  else
    error "Donwload Failed"
  end if
end URLSession:task:didCompleteWithError:

on parseSessionResults()
  set resStr to NSString’s alloc()’s initWithData:(my retData) encoding:(NSUTF8StringEncoding)
  
set jsonString to NSString’s stringWithString:(resStr)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
return aJsonDict
end parseSessionResults

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 

(Visited 85 times, 1 visits today)
Posted in Internet REST API URL | Tagged 10.13savvy 10.14savvy 10.15savvy NSJSONSerialization NSMutableData NSMutableDictionary NSMutableURLRequest NSString NSURL NSURLCache NSURLComponents NSURLQueryItem NSURLSession NSURLSessionConfiguration NSUTF8StringEncoding | 1 Comment

Post navigation

  • Older posts

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

Google Search

Popular posts

  • AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 13, Ventura(継続更新)
  • ドラッグ&ドロップ機能の未来?
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • PFiddlesoft UI Browserが製品終了に
  • macOS 12.3 beta 5、ASの障害が解消される(?)
  • SF Symbolsを名称で指定してPNG画像化
  • 新刊発売:AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 12.3 beta4、まだ直らないASまわりの障害
  • macOS 12.3上でFinder上で選択中のファイルをそのままオープンできない件
  • Safariで表示中のYouTubeムービーのサムネイル画像を取得
  • macOS 12のスクリプトエディタで、Context Menu機能にバグ
  • Pixelmator Pro v2.4.1で新機能追加+AppleScriptコマンド追加
  • 人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!
  • CotEditor v4.1.2でAppleScript系の機能を追加
  • macOS 12.5(21G72)がリリースされた!
  • UI Browserがgithub上でソース公開され、オープンソースに
  • Pages v12に謎のバグ。書類上に11枚しか画像を配置できない→解決
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1391) 10.14savvy (586) 10.15savvy (434) 11.0savvy (274) 12.0savvy (174) 13.0savvy (34) CotEditor (60) Finder (47) iTunes (19) Keynote (97) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (21) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (42) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (118) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWorkspace (20) Numbers (55) Pages (36) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) 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
  • 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年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