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

タグ: 10.11savvy

指定フォルダ内のファイルのうち、指定UTIに該当するものをすべて取得してフルパスを返す

Posted on 4月 13, 2019 by Takaaki Naganoya

指定フォルダ内のファイルのうち、指定のUTIに該当するものをすべて取得してフルパス(file/POSIX)で返すAppleScriptです。

指定フォルダの直下のみを走査し、サブフォルダ内は走査しません。調べてみるとUTIでファイルを絞り込む機能は存在していないようなので(見落としているだけ?)、すべてファイルを取得してからループでUTIを調べつつ該当するかどうかチェックしています。

書けば書くほど「それってSpotlightでよくね?」という気がしますが、確実に取得したい(Spotlightインデックスが破損していたり、サーバー上のファイルではSpotlightが効かない場合もある)場合に使うとよいでしょうか。

AppleScript名:指定フォルダ内のファイルのうち、指定UTIに該当するものをすべて取得してフルパスを返す
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/04/13
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSPredicate : a reference to current application’s NSPredicate
property NSFileManager : a reference to current application’s NSFileManager
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles

set aUTI to "com.adobe.pdf"
set libPath to ((path to documents folder) as string)
set posixLibPath to POSIX path of libPath

set f1List to getFilepathListByUTI(posixLibPath, aUTI, "file") of me
–> {file "Cherry:Users:me:Documents:0718kenpo.pdf", file "Cherry:Users:me:Documents:2013-09Rekihaku.pdf", file "Cherry:Users:maro:Documents:airserver.pdf"}

set f2List to getFilepathListByUTI(posixLibPath, aUTI, "POSIX") of me
–> {"/Users/me/Documents/0718kenpo.pdf", "/Users/me/Documents/2013-09Rekihaku.pdf", "/Users/me/Documents/airserver.pdf"}

on getFilepathListByUTI(aFolPOSIX, aUTI as string, aFileType as string)
  script spdFile
    property urlList : {}
  end script
  
  
if aFileType is not in {"file", "POSIX"} then return {}
  
  
set aFM to NSFileManager’s defaultManager()
  
set aFolExt to (aFM’s fileExistsAtPath:aFolPOSIX isDirectory:true) as boolean
  
if aFolExt = false then return {} –フォルダ自体が存在しなければヌルリストを返す
  
  
set aURL to |NSURL|’s fileURLWithPath:aFolPOSIX
  
set theOptions to ((current application’s NSDirectoryEnumerationSkipsPackageDescendants) as integer) + ((current application’s NSDirectoryEnumerationSkipsHiddenFiles) as integer)
  
set urlArray to (aFM’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value))
  
if urlArray = missing value then return {}
  
  
set (urlList of spdFile) to urlArray as list
  
set newList to {}
  
  
repeat with i in (urlList of spdFile)
    set j to POSIX path of i
    
set tmpUTI to my retUTIfromPath(j)
    
set utiRes to my filterUTIList({tmpUTI}, aUTI)
    
    
if utiRes is not equal to {} then
      if aFileType = "POSIX" then
        set the end of newList to j
      else if aFileType = "file" then
        set the end of newList to POSIX file j
      end if
    end if
    
  end repeat
  
  
return newList
end getFilepathListByUTI

–指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
  set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath
  
set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
  
  
if theResult = true then
    return theValue as string
  else
    return theResult
  end if
end retUTIfromPath

–UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
  set anArray to NSArray’s arrayWithArray:aUTIList
  
set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return bRes
end filterUTIList

★Click Here to Open This Script 

Posted in file File path list UTI | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSArray NSDirectoryEnumerationSkipsHiddenFiles NSFileManager NSPredicate NSURL NSURLTypeIdentifierKey | 3 Comments

FireFoxでURLをオープン(URLの間接指定)

Posted on 3月 31, 2019 by Takaaki Naganoya

FireFoxで指定URLをオープンするAppleScriptです。

FireFoxのAppleScript対応機能は、指定のURLをオープンさせるだけです。しかも、AppleScript用語辞書からアクセスできないという体たらく。2004年ごろに出回っていたサンプルScriptがそのまま最新のバージョンでも動きました。

FireFoxのAppleScript用語辞書を見てもdo javascript的な命令はないですし、ほぼ何もできない内容です(ダミーといってもいいでしょう)。なので、これをスクリプタブルだとはとても言えません。AppleScriptからコントロールする用途でFireFoxを用いることに意味はありません。

いろいろ試していたら、FireFoxが起動していない状態で本Scriptを実行すると、実行が完了しないという問題が報告されました。追試してみるとそのとおりです。あらかじめ、起動しておくなりactivateしておくなどの処理が必要です。

AppleScript名:FireFoxでURLをオープン(URLの間接指定)
set aURL to "http://piyocast.com/as"

tell application "Firefox"
  «event WWW!OURL» aURL
end tell

★Click Here to Open This Script 

Posted in Internet Raw AppleEvent Code URL | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy FireFox | Leave a comment

頭のいいタイマー割り込み実行

Posted on 3月 27, 2019 by Takaaki Naganoya

頭のいいタイマー割り込み(on idle)実行のAppleScriptを追求してみました。

AppleScript開闢(かいびゃく)以来、すでに20年以上の時間が経過しているので、on idleによるタイマー割り込み処理なんて、探せばサンプルが山のように出てくるものです。

AppleScript名:timer interrupt
property targetTime : "9:37:00"
property timerInterval : 30

on run
  set timerInterval to 30
end run

on idle
  set curTime to current date
  
set cString to time string of curTime
  
  
if cString ≥ targetTime then
    display dialog "It’s time to take off!" buttons {"OK"} default button 1 giving up after 30
    
quit
  end if
  
  
return timerInterval
end idle

★Click Here to Open This Script 

これが基礎的な内容で、このScriptをScript Editor上でアプリケーション(アプレット)形式で、「ハンドラの実行後に終了しない」をオンにして書き出すとタイマー実行アプレットが出来上がります(3分間クッキング)。

ただ、実行時刻のパラメータがプログラム内に直打ちなのが気になります。知能レベルが低い感じがします。

そこで、実行時刻のパラメータの外部供給ということを考え出すわけですが、

 (1)設定ファイルから読み込み
 (2)アプレット自身のコメント(File Comment)から読み込み
 (3)ファイル名自体から読み込み
 (4)コマンドラインから実行し、実行時にパラメータ(argv)を指定

などの方法を考えつきます(20世紀にすでにさんざんやった内容)。ただし、全角数字を半角に変換したり、ファイル名の場合には時刻セパレータの「:」がmacOS上ではファイル名に使えない文字(ディレクトリ・セパレータ)だったり、Finderが管理しているファイル名はUnicodeのNormalize方式が異なる(処理しやすいようにNormalizeし直さないとダメ)など割と頭の痛い問題がいろいろあります。

そこで利用したいのが、CocoaのDataFormatter。自然言語風に書かれた「10時41分」(全角数字入り)といった文字列から日時データをピックアップします。

そうして書いたのがこれ(↓)です。

ファイル名に書かれた時刻から実行時刻を拾ってタイマー実行します。けっこう頭がいい感じがします。

実際に、こうした処理の延長線上にTanzakuで行なっているファイル名から取得した文字列に対する形態素解析&コマンドピックアップの処理があります。

AppleScript名:10時42分
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

property targTime : missing value
property timerInterval : 1

on run
  set timerInterval to 1
  
  
–Get filename from this applet
  
set myPath to path to current application
  
tell application "System Events"
    set myName to name of myPath
  end tell
  
  
–Validate filename as a natural language format date by using NSDataDetector
  
set dList to getDatesIn(myName) of me
  
repeat while dList = {}
    set myName to text returned of (display dialog "There is no time elements in my filename. Input the target time in x時xx分" default answer "午後5時45分")
    
set dList to getDatesIn(myName) of me
  end repeat
  
  
set targDate to first item of dList
  
set targTime to time string of targDate
  
display notification targTime
end run

on idle
  set curTime to current date
  
set curTimeStr to time string of curTime
  
  
if curTimeStr ≥ targTime then
    activate
    
display dialog "It’s time to take off!" buttons {"OK"} default button 1 giving up after 30
    
quit
  end if
  
  
return timerInterval
end idle

on getDatesIn(aString)
  set anNSString to current application’s NSString’s stringWithString:aString
  
set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeDate) |error|:(reference)
  
set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()}
  
set theResults to theMatches’s valueForKey:"date"
  
return theResults as list
end getDatesIn

★Click Here to Open This Script 

Posted in File path Natural Language Processing | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSDataDetector NSString | Leave a comment

アラートダイアログ上にsegmented controlを表示 v4

Posted on 3月 27, 2019 by Takaaki Naganoya

アラートダイアログ上にsegmented controlを複数表示して、複数選択項目を取得するAppleScriptです。

複数選択項目にpopup buttonではなくsegmented controlを使う必要性というのは、あんまりないですが………すべての選択項目が見える状態で少量の選択項目から1つを選ぶような用途には使えるのではないでしょうか。

segmented controlをそれぞれBoxに入れ、Viewにまとめ、Scroll viewに突っ込んでいますが、あまり効果があるんだかないんだか不明です。

AppleScript名:アラートダイアログ上にsegmented controlを表示 v4
— Created 2019-03-26 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
–Scroll Viewをつかってみた

property NSBox : a reference to current application’s NSBox
property NSView : a reference to current application’s NSView
property NSAlert : a reference to current application’s NSAlert
property NSScrollView : a reference to current application’s NSScrollView
property NSSegmentedControl : a reference to current application’s NSSegmentedControl
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application’s NSSegmentStyleTexturedRounded

property returnCode : 0
property returnSels : {}

set paramObj to {myMessage:"複数項目選択", mySubMessage:"どれか選択してください。", segmentMes:{{"Red1", "Blue1", "Yellow1", "Brown1", "White1", "Cyan1", "Grey1"}, {"Red2", "Blue2", "Yellow2", "Brown2", "White2", "Cyan2", "Grey2"}, {"Red3", "Blue3", "Yellow3", "Brown3", "White3", "Cyan3", "Grey3"}, {"Red4", "Blue4", "Yellow4", "Brown4", "White4", "Cyan4", "Grey4"}, {"Red5", "Blue5", "Yellow5", "Brown5", "White5", "Cyan5", "Grey5"}}, segmentTitles:{"1st Segments", "2nd Segments", "3rd Segments", "4th Segments", "5th Segments"}}

my chooseMultipleSegments:paramObj

return my returnSels
–> {1, 2, 3, 4, 5}

on chooseMultipleSegments:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set segMes2DList to segmentMes of paramObj
  
set segTitleList to segmentTitles of paramObj
  
  
set aTmpY to (length of segMes2DList) * 60
  
  
–BoX + Segmented Control をつくる
  
set segsList to {}
  
set boxLIst to {}
  
set segsCount to 0
  
set tmpMaxX to 500
  
  
set aCount to 1
  
  
repeat with i in segMes2DList
    set aSeg to makeSegmentedControlWithStartY(i, aTmpY – segsCount – 40, 500, 40) of me
    
    
set aDBounds to aSeg’s |bounds|()
    
set tmpWidth to getWidth(aDBounds) of me
    
    
set aBox to (NSBox’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, aTmpY – segsCount – 60, 500, 60)))
    (
aBox’s setTitle:(item aCount of segTitleList))
    (
aBox’s addSubview:aSeg)
    
    
if tmpWidth > tmpMaxX then set tmpMaxX to tmpWidth
    
    
set the end of segsList to aSeg –選択検出用
    
set the end of boxLIst to aBox –表示用
    
    
set segsCount to segsCount + 60
    
set aCount to aCount + 1
  end repeat
  
  
— create a view
  
set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, tmpMaxX, aTmpY))
  
theView’s setSubviews:boxLIst
  
  
— create a Scroll View
  
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, tmpMaxX, aTmpY))
  
aScroll’s setDocumentView:theView
  
theView’s enclosingScrollView()’s setHasHorizontalScroller:false
  
theView’s enclosingScrollView()’s setHasVerticalScroller:false
  
  
— 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
  end tell
  
  
— 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 error number -128
  
  
set my returnSels to {}
  
repeat with i in segsList
    set tmpSegSel to (i’s selectedSegment()) as number
    
set the end of (my returnSels) to tmpSegSel + 1
  end repeat
end chooseMultipleSegments:

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

on makeSegmentedControlWithStartY(titleList, aStartY, aWidth, aHeight)
  set aLen to length of titleList
  
  
set aSeg to NSSegmentedControl’s alloc()’s init()
  
aSeg’s setSegmentCount:aLen
  
  
set aCount to 0
  
repeat with i in titleList
    set j to contents of i
    (
aSeg’s setLabel:j forSegment:aCount)
    
set aCount to aCount + 1
  end repeat
  
  
aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false
  
aSeg’s setSegmentStyle:(NSSegmentStyleTexturedRounded)
  
aSeg’s setFrame:(current application’s NSMakeRect(20, aHeight – 35, aWidth, aHeight – 40))
  
  
aSeg’s setTrackingMode:0
  
aSeg’s setTarget:me
  
aSeg’s setAction:"clickedSeg:"
  
aSeg’s setSelectedSegment:0
  
  
return aSeg
end makeSegmentedControlWithStartY

on clickedSeg:aSender
  set aSel to aSender’s selectedSegment()
end clickedSeg:

on getWidth(aDBounds)
  if class of aDBounds = list then
    –macOS 10.13 or later
    
return item 1 of item 1 of aDBounds
  else
    –macOS 10.10….10.12
    
return width of |size| of aDBounds
  end if
end getWidth

★Click Here to Open This Script 

Posted in GUI list | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSAlert NSBox NSRunningApplication NSScrollView NSSegmentedControl NSView | 1 Comment

青空文庫のテキストのルビタグを超高速削除

Posted on 3月 18, 2019 by Takaaki Naganoya

青空文庫のテキストのルビタグをすべて削除するAppleScriptです。

CotEditorでオープン中の青空文庫のテキストからルビタグを削除し、元のドキュメントに置換結果を反映させます。

テストに使用したのは、夏目漱石の「こころ」のテキストです。上記ページの「テキストファイル(ルビあり)」をダウンロードして、Zipアーカイブを展開して使用しました。

ファイルサイズ373KB、当該部分4,570箇所。開始文字「《」、終了文字「》」で囲われたエリアをすべて削除するという、AppleScriptにはあからさまに不得意そうな処理で、最初に書いたAppleScriptでは1分半以上かかっていました。内容は、おおよそ常識的なサブルーチンを組み合わせてループで回しただけです。わざと遅くなるように組んだりはしていません。

これを、

 (1)CotEditorからの文章テキストの取得
 (2)置換当該箇所のリストアップ
 (3)文字置換
 (4)CotEditorへの文章テキストの転送

の4つのステージに分け、それぞれ処理時間を計測。すると、(1)、(2)、(4)については1秒かかるかかからないかぐらいの速度で実行していることが判明。圧倒的に(3)文字置換の処理に時間がかかっていました。

もともと文字置換には、AppleScript処理系最速のtext item delimitersを用いるサブルーチンを使用していました。これ以上、この方向に頑張っても速く処理することはできません。一応、ダメ元で4,570個の要素を持つ巨大なtext item delimitersを作成し一括処理できないか試してみたものの、さすがに処理系のキャパシティを超過しているようで処理が戻ってきません(迷走状態)。完全にお手上げです。

そこで、AppleScriptの処理系に依存したtext item delimitersによる処理をやめ、メモリ管理効率がよくないAppleScriptのstring型のデータで保持することをやめ、置換のたびにAppleScriptのstring型に変換(cast)することをやめ、置換中は最初から最後までNSMutableStringで管理するようにしました。

このように大幅に書き換えたところ、トータルで3.58秒で処理終了するようになりました。

すべての置換が終了したあとにNSMutableStringをAppleScriptのstringに変換し、CotEditorの最前面のドキュメントに結果を転送しています。

AppleScript名:青空文庫のテキストのルビタグを削除
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/18
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

property NSScanner : a reference to current application’s NSScanner
property NSOrderedSet : a reference to current application’s NSOrderedSet
property NSMutableString : a reference to current application’s NSMutableString
property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch

tell application "CotEditor"
  tell front document
    set aCon to contents
  end tell
end tell

set bCon to trimStrFromTo(aCon, "《", "》") of me

tell application "CotEditor"
  tell front document
    set contents to bCon
  end tell
end tell

–開始文字と終了文字に囲われた文字列をすべて削除する
on trimStrFromTo(aParamStr, fromStr, toStr)
  script hsAry
    property anArray : {}
    
property curStr : ""
  end script
  
  
set theScanner to NSScanner’s scannerWithString:aParamStr
  
set (anArray of hsAry) to {}
  
  
repeat until (theScanner’s isAtEnd as boolean)
    set {aResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference)
    
theScanner’s scanString:fromStr intoString:(missing value)
    
    
set {bResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference)
    
if theValue is missing value then set theValue to ""
    
    
theScanner’s scanString:toStr intoString:(missing value)
    
set the end of (anArray of hsAry) to (fromStr & theValue & toStr)
  end repeat
  
  
–Case: Not found
  
if length of (anArray of hsAry) = 0 then return aParamStr
  
  
–Uniquefy
  
set (anArray of hsAry) to makeUniqueListFrom((anArray of hsAry)) of me
  
  
–Replace strings as NSMutableString
  
set (curStr of hsAry) to NSMutableString’s stringWithString:aParamStr
  
repeat with i in (anArray of hsAry)
    set j to contents of i
    
set (curStr of hsAry) to ((curStr of hsAry)’s stringByReplacingOccurrencesOfString:(j) withString:"" options:(NSRegularExpressionSearch) range:{location:0, |length|:((curStr of hsAry)’s |length|())})
  end repeat
  
  
return (curStr of hsAry) as string
end trimStrFromTo

–1D Listをユニーク化(重複削除)
on makeUniqueListFrom(theList)
  set theSet to NSOrderedSet’s orderedSetWithArray:theList
  
return (theSet’s array()) as list
end makeUniqueListFrom

★Click Here to Open This Script 

Posted in list Text | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy CotEditor NSMutableString NSOrderedSet NSRegularExpressionSearch NSScanner | 2 Comments

アラートダイアログ上にBrowser+Map Viewを表示 v2

Posted on 3月 17, 2019 by Takaaki Naganoya

ダイアログ上に表示したNSBrowserで都道府県→都道府県別データを選択し、選択したデータの位置情報をMap Viewに表示するAppleScriptの改良版です。

初版掲載時のおかしな挙動を減らし、地図種別の切り替えができるようになっています。本掲載リストだけだと動作が完結しないため、ライブラリを含んだスクリプトバンドルをダウンロードして実行してください。下記リストは「参考までに」掲載しているものです。

–> Download whole Script bundle with Library

前バージョンでは、NSBrowser上でデータが存在していない箇所に空白のセルが表示され、クリックするとクラッシュするという状態でした。本バージョンでもたまに出てくるので完全ではないのですが、

 ・予想どおりデータの行数をカウントするハンドラで絞り込みを行うPredicates文の文字列に問題があった
 ・データに半角のシングルクォートが入っていて、これによってデータの絞り込みに問題が出た

という問題を解消しました。前者はこまめにstringにcastし、後者は全角文字に置き換えました。

それでもまだ問題が出るケースがあるので、まだしばらく実際に使いつつ様子見といったところでしょうか。

AppleScript名:アラートダイアログ上にBrowser+Map Viewを表示 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/10
—
–  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 "MapKit"
use scripting additions
use skLib : script "senjoNoKizunaLib"

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 NSBrowser : a reference to current application’s NSBrowser
property MKMapView : a reference to current application’s MKMapView
property NSScrollView : a reference to current application’s NSScrollView
property NSMutableArray : a reference to current application’s NSMutableArray
property MKMapTypeHybrid : a reference to current application’s MKMapTypeHybrid
property MKMapTypeSatellite : a reference to current application’s MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application’s MKMapTypeStandard
property NSSegmentedControl : a reference to current application’s NSSegmentedControl
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn
property NSSegmentStyleTexturedRounded : a reference to current application’s NSSegmentStyleTexturedRounded

property zLevel : 17
property aMaxViewWidth : 1000
property aMaxViewHeight : 500
property theResult : 0
property returnCode : 0
property theDataSource : {}
property aSelection : {}
property aMapView : missing value
property aBrowser : missing value
property skDataList : {}

property prefList : {"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "新潟県", "富山県", "石川県", "福井県", "山梨県", "長野県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県", "沖縄県"}

if my skDataList = {} then
  set my skDataList to current application’s NSMutableArray’s arrayWithArray:(getSenjoNokizunaGameCenterDataList() of skLib)
end if

set tmpLen to length of (my skDataList as list)

set aSelection to {}

set paramObj to {myMessage:"Choose a Game Center", mySubMessage:("Choose an appropriate Game Center from list (" & tmpLen as string) & ") to play Senjo-no-Kizuna"}

my performSelectorOnMainThread:"chooseItemByBrowser:" withObject:(paramObj) waitUntilDone:true
if (my returnCode as number) = 1001 then error number -128

return my aSelection
–> {loc_id:"QIEXj9er5QSA_Y42-OjPNg", gcName:"THE 3RD PLANET ジャングルパーク鹿児島", latitude:31.5703088, longitude:130.5653137, address:"鹿児島県 鹿児島市 与次郎 1-11-1 フレスポジャングルパーク2F"}

on chooseItemByBrowser:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
  
— create a view
  
set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aMaxViewWidth, aMaxViewHeight))
  
set aMapView to MKMapView’s alloc()’s initWithFrame:(current application’s NSMakeRect(410, 30, aMaxViewWidth – 410, aMaxViewHeight – 30))
  
tell aMapView
    its setMapType:(MKMapTypeStandard)
    
its setZoomEnabled:true
    
its setScrollEnabled:true
    
its setPitchEnabled:true
    
its setRotateEnabled:true
    
its setShowsCompass:true
    
its setShowsZoomControls:true
    
its setShowsScale:true
    
its setShowsUserLocation:true
    
its setDelegate:me
  end tell
  
  
— make browser view with scroll view
  
set aScrollWithTable to makeBrowserView(prefList, 400, aMaxViewHeight) of me
  
  
–Segmented Controlをつくる
  
set segTitleList to {"Map", "Satellite", "Satellite + Map"}
  
set aSeg to makeSegmentedControl(segTitleList, 410, 0, 150, 20) of me
  
  
–Compose Views in NSView
  
theView’s setSubviews:{aScrollWithTable, aMapView, aSeg}
  
  
— 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:theView
  end tell
  
  
— show alert in modal loop
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
end chooseItemByBrowser:

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

on makeBrowserView(aList as list, aWidth as number, aHeight as number)
  set (my theDataSource) to NSMutableArray’s arrayWithArray:aList
  
  
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
set aBrowser to NSBrowser’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
  
aBrowser’s setDelegate:(me)
  
aBrowser’s setTarget:(me)
  
aBrowser’s setAction:"browserCellSelected:"
  
aBrowser’s setMinColumnWidth:120
  
aBrowser’s setSeparatesColumns:true
  
aBrowser’s setMaxVisibleColumns:2
  
aBrowser’s setAutohidesScroller:true
  
aBrowser’s setTakesTitleFromPreviousColumn:true
  
–aBrowser’s setBackgroundColor:(NSColor’s grayColor())
  
  
aScroll’s setDocumentView:aBrowser
  
aBrowser’s enclosingScrollView()’s setHasHorizontalScroller:true
  
aBrowser’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
return aScroll
end makeBrowserView

–NSBrowser Event Handlers
on browser:aView numberOfRowsInColumn:aColumn
  if aColumn = 0 then
    return my theDataSource’s |count|()
  else if aColumn = 1 then
    set aPath to (text 2 thru -1 of ((aView’s |path|()) as string)) as string –ここが問題だったもよう
    
set tmpArray to (my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")) as list
    
return (length of tmpArray)
  else
    return 0
  end if
end browser:numberOfRowsInColumn:

on browser:aView willDisplayCell:(aCell) atRow:(rowIndex as integer) column:(colIndex as integer)
  if colIndex = 0 then
    –Prefectures
    
aCell’s setTitle:((item (rowIndex + 1) of prefList) as string)
    
aCell’s setLeaf:false
    
  else if colIndex = 1 then
    –Each Game Centers in the Prefecture
    
set aPath to text 2 thru -1 of ((aView’s |path|()) as string)
    
set tmpArray to my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")
    
set tmpItem to (tmpArray’s objectAtIndex:rowIndex)
    
    
set aGameCenterName to (tmpItem’s gcName) as string
    
aCell’s setTitle:(aGameCenterName)
    
aCell’s setLeaf:true
    
  else if colIndex ≥ 2 then
    error "Wrong NSBrowser status"
  end if
end browser:willDisplayCell:atRow:column:

on browserCellSelected:aSender
  set aPath to my aBrowser’s |path|()
  
set aList to (aPath’s pathComponents()) as list
  
set aLen to length of aList
  
  
if aLen = 3 then
    –set aPref to contents of item 2 of aList
    
set aGc to contents of last item of aList
    
    
set tmpArray to my filterRecListByLabel1(skDataList, "gcName == ’" & aGc & "’")
    
–set tmpArray to my filterRecListByLabel1(skDataList, "gcName == " & aGc)
    
set tmpItem to contents of first item of (tmpArray as list)
    
    
copy tmpItem to my aSelection
    
    
set aLatitude to (latitude of tmpItem) as real
    
set aLongitude to (longitude of tmpItem) as real
    
    
tell aMapView
      set aLocation to current application’s CLLocationCoordinate2DMake(aLatitude, aLongitude)
      
its setCenterCoordinate:aLocation zoomLevel:(zLevel) animated:false
    end tell
    
  end if
end browserCellSelected:

–NSArrayに入れたNSDictionaryを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList, aPredicate as string)
  set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aRecList’s filteredArrayUsingPredicate:aPredicate
  
return filteredArray
end filterRecListByLabel1

–Segmented Controlをつくる
on makeSegmentedControl(titleList, startX, startY, aWidth, aHeight)
  set aLen to length of titleList
  
  
set aSeg to NSSegmentedControl’s alloc()’s init()
  
aSeg’s setSegmentCount:aLen
  
  
set aCount to 0
  
repeat with i in titleList
    set j to contents of i
    (
aSeg’s setLabel:j forSegment:aCount)
    
set aCount to aCount + 1
  end repeat
  
  
aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false
  
aSeg’s setSegmentStyle:(NSSegmentStyleTexturedRounded)
  
aSeg’s setFrame:(current application’s NSMakeRect(startX, startY, aWidth, aHeight))
  
aSeg’s setTrackingMode:0
  
aSeg’s setTarget:me
  
aSeg’s setAction:"clickedSeg:"
  
aSeg’s setSelectedSegment:0
  
  
return aSeg
end makeSegmentedControl

–Segmented Controlのクリック時のイベントハンドラ
on clickedSeg:aSender
  set aSel to aSender’s selectedSegment()
  
set tList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
  
set tmpType to contents of item (aSel + 1) of tList
  
  
aMapView’s setMapType:(tmpType)
  
  
set selSeg to aSel
end clickedSeg:

★Click Here to Open This Script 

Posted in geolocation GUI list Map regexp | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy MKMapTypeHybrid MKMapTypeSatellite MKMapTypeStandard MKMapView NSAlert NSAlertSecondButtonReturn NSBrowser NSColor NSMutableArray NSRunningApplication NSScrollView NSSegmentedControl NSSegmentStyleTexturedRounded NSView | Leave a comment

HTMLをplain textに変換(文字コード自動認識ライブラリ展開)

Posted on 3月 14, 2019 by Takaaki Naganoya

HTMLをプレーンテキストに変換するAppleScriptです。

本来は、文字コードの自動推測を行う部分はライブラリ化していますが、掲載用にライブラリをScript中に展開してみました。

コードの自動推測部分の処理は、昔のCotEditorのソースを読んで前半部分の「古くからある文字エンコーディングの勝ち抜け」処理を書き、後半部分のマイナー文字エンコーディングの多数決&文字化け検出方式がオリジナル処理部分です。

本コード自動推測は、意味のある日本語のテキストを処理するように設計してあるので、寿司屋の湯呑みのように魚の名前の漢字が1文字で羅列されているようなテキストの文字コード自動判別でミスを起こす可能性があります(実際に魚の名前のテキストを作って読み込んでみましたが、とくに問題はありませんでした)。

ただ、最近はUTF-8だけでなんとかなりそうなので、そこまでエグいテキストに遭遇することもないでしょう。

AppleScript名:HTMLをplain textに変換(文字コード自動認識ライブラリ展開)
— Created 2017-09-08 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–use jLib : script "japaneseTextEncodingDetector"

property NSString : a reference to current application’s NSString
property NSMutableArray : a reference to current application’s NSMutableArray
property NSAttributedString : a reference to current application’s NSAttributedString
property NSUnicodeStringEncoding : a reference to current application’s NSUnicodeStringEncoding

set aFile to choose file
set aRes to readJapanesTextFileWithGuessingEncoding(POSIX path of aFile) of me
if aRes = false then return ""

set aPlainText to HTMLDecode(aRes) of me

on HTMLDecode(HTMLString)
  set theString to current application’s NSString’s stringWithString:HTMLString
  
set theData to theString’s dataUsingEncoding:(NSUnicodeStringEncoding)
  
set attStr to NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value)
  
return (attStr’s |string|()) as string
end HTMLDecode

–Read Japanese text with detecting its text encoding
on readJapanesTextFileWithGuessingEncoding(aPOSIXpath as string)
  
  
–ISO2022JP check
  
set aNSData to current application’s NSData’s dataWithContentsOfFile:aPOSIXpath
  
set aDataLength to aNSData’s |length|()
  
if aDataLength > 1024 then set aDataLength to 1024
  
  
–0x1B check
  
set anNSString to current application’s NSString’s stringWithString:(character id 27) — 0x1B
  
set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set theRange to aNSData’s rangeOfData:theData options:0 range:(current application’s NSMakeRange(0, aDataLength))
  
  
–found 0x1B in aNSData
  
if |length| of theRange = 1 and location of theRange < aDataLength then
    set aStr to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSISO2022JPStringEncoding)) –21
    
if aStr is not equal to missing value then return (aStr as text) — ISO2022JP
  end if
  
  
–EUC
  
set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSJapaneseEUCStringEncoding))
  
if resValue is not equal to missing value then return (resValue as text)
  
  
–UTF-8
  
set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF8StringEncoding))
  
if resValue is not equal to missing value then return (resValue as text)
  
  
–SHift JIS
  
set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSShiftJISStringEncoding))
  
if resValue is not equal to missing value then return (resValue as text)
  
  
–多数決を取る
  
–UTF-16BE/LE/無印Unicodeは多数決を取る
  
set resValue1 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF16BigEndianStringEncoding)) as text
  
set sample1 to getTextSample(resValue1) of me
  
set lang1 to specifyLanguageOfText(sample1) of me
  
set para1 to length of (paragraphs of sample1)
  
set words1 to length of (words of sample1)
  
  
–UTF-16LE  
  
set resValue2 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF16LittleEndianStringEncoding)) as text
  
set sample2 to getTextSample(resValue2) of me
  
set lang2 to specifyLanguageOfText(sample2) of me
  
set para2 to length of (paragraphs of sample2)
  
set words2 to length of (words of sample2)
  
  
–無印Unicode
  
set resValue3 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUnicodeStringEncoding)) as text
  
set sample3 to getTextSample(resValue3) of me
  
set lang3 to specifyLanguageOfText(sample3) of me
  
set para3 to length of (paragraphs of sample3)
  
set words3 to length of (words of sample3)
  
  
–文字および文法的に見て「日本語」ならそれを返す
  
if lang1 = "ja" then return resValue1
  
if lang2 = "ja" then return resValue2
  
if lang3 = "ja" then return resValue2
  
  
  
–文字化けしたときには、日本語の「Word」として認識されづらく、Paragraphも少ない(1とか)なので条件で除外する
  
if para1 is not equal to 1 then
    if (words1 ≤ words2) or (words1 ≤ words3) then
      return resValue1
    end if
  end if
  
  
if para2 is not equal to 1 then
    if (words2 ≤ words1) or (words2 ≤ words3) then
      return resValue2
    end if
  end if
  
  
if para3 is not equal to 1 then
    if (words3 ≤ words1) or (words3 ≤ words2) then
      return resValue3
    end if
  end if
  
  
return false –文字コード判定に失敗した
end readJapanesTextFileWithGuessingEncoding

on specifyLanguageOfText(aStr)
  set aNSstring to current application’s NSString’s stringWithString:aStr
  
set tagSchemes to current application’s NSArray’s arrayWithObjects:(current application’s NSLinguisticTagSchemeLanguage)
  
set tagger to current application’s NSLinguisticTagger’s alloc()’s initWithTagSchemes:tagSchemes options:0
  
tagger’s setString:aNSstring
  
set aLanguage to tagger’s tagAtIndex:0 |scheme|:(current application’s NSLinguisticTagSchemeLanguage) tokenRange:(missing value) sentenceRange:(missing value)
  
return aLanguage as text
end specifyLanguageOfText

on getTextSample(aText)
  set aLen to length of aText
  
if aLen < 1024 then
    set bLen to aLen
  else
    set bLen to 1024
  end if
  
return (text 1 thru bLen of aText)
end getTextSample

★Click Here to Open This Script 

Posted in file Text | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSAttributedString NSMutableArray NSString | Leave a comment

アラートダイアログ上にBrowser+Map Viewを表示

Posted on 3月 12, 2019 by Takaaki Naganoya

ダイアログ上に表示したNSBrowserで都道府県→都道府県別データを選択し、選択したデータの位置情報をMap Viewに表示するAppleScriptです。

# 2021年11月末にアーケードゲーム「戦場の絆」のサービスが終了したため、Webサイト側からデータ取得する部分の処理は動作しません(Webサイトごとアクセスできなくなったため)


▲macOS 10.12.6上で動作

「OK」ボタンをクリックすると選択したゲームセンターの諸元情報を返してきます。リスト一覧から名称を選択し、途中でその位置情報を地図で見られるという程度のインタフェースです。

NSBrowserを利用したUIの習作で、Web上の「戦場の絆」公式ページから導入店舗の情報を取得し(このあたりライブラリにまとめて部品化)、データを都道府県別にしぼりこんでNSBrowser上で選択できるようにしてみました。


▲macOS 10.13.6上で動作


▲macOS 10.14.4上で動作(Light Mode)


▲macOS 10.14.4上で動作(Dark Mode)

ぱっと見は「何かすごいもの」に見えるのですが、キーワード検索が実装されていないため、データしぼりこみに柔軟性がないと感じるものです。

NSBrowserをAppleScriptから利用した場合に、Xcodeを利用しないとこのレベルの単純なデータ階層固定でデータしぼりこみを行う程度のものしか作りにくいところでしょうか。不可能とは言わないものの、「普通にXcode上で作れば?」という話になると思います。

さらに、該当するカラムの行数を返すハンドラでデータをしぼりこんでレコード数を返しているものの、いまひとつここがうまく動いていない印象を受けます。NSBrowser上で「存在しないデータのような何か」をクリックできるケースがあり、それをクリックすると実行環境(スクリプトエディタとか、Script Debuggerとか)ごとクラッシュします。

→ 原因がわかりました。問題箇所も想像どおり。目下、修正版で謎のゴースト行も出てこなくなったので、あとで修正版を掲載しておきます。問題が2つあって、1つがPredicates文をas stringですべてcastしていなかった件、もうひとつは、、、副次的に発生した問題で、愛知県のデータに「M’s」という半角クォート文字を含むものがあって、これでPredicates文が中断されてしまった問題。半角シングルクォートを全角に置き換えるとかいろいろ手を加える必要がありました。

本Script全体をダウンロードして、スクリプトエディタ上で実行すると、最初に「戦場の絆」公式ホームページの日本国内の店舗情報をスキャンして店舗情報を抽出するため、サーバーの混雑具合やネットワーク回線速度にも左右されますが、自分の環境では5〜15秒程度でデータの取得が終了し、ダイアログが表示されます。

–> Download whole Script bundle with Library

AppleScript名:アラートダイアログ上にBrowser+Map Viewを表示.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/10
—
–  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 "MapKit"
use scripting additions
use skLib : script "senjoNoKizunaLib"

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 NSBrowser : a reference to current application’s NSBrowser
property MKMapView : a reference to current application’s MKMapView
property NSScrollView : a reference to current application’s NSScrollView
property NSMutableArray : a reference to current application’s NSMutableArray
property MKMapTypeStandard : a reference to current application’s MKMapTypeStandard
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn

property zLevel : 17
property aMaxViewWidth : 1000
property aMaxViewHeight : 500
property theResult : 0
property returnCode : 0
property theDataSource : {}
property aSelection : {}
property aMapView : missing value
property aBrowser : missing value
property skDataList : {}

property prefList : {"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "新潟県", "富山県", "石川県", "福井県", "山梨県", "長野県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県", "沖縄県"}

if my skDataList = {} then
  set my skDataList to current application’s NSMutableArray’s arrayWithArray:(getSenjoNokizunaGameCenterDataList() of skLib)
end if

set aSelection to {}

set paramObj to {myMessage:"Choose a Game Center", mySubMessage:"Choose an appropriate Game Center from list to play Senjo-no-Kizuna"}

my performSelectorOnMainThread:"chooseItemByBrowser:" withObject:(paramObj) waitUntilDone:true
return my aSelection
–> {loc_id:"QIEXj9er5QSA_Y42-OjPNg", gcName:"THE 3RD PLANET ジャングルパーク鹿児島", latitude:31.5703088, longitude:130.5653137, address:"鹿児島県 鹿児島市 与次郎 1-11-1 フレスポジャングルパーク2F"}

on chooseItemByBrowser:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
  
— create a view
  
set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aMaxViewWidth, aMaxViewHeight))
  
set aMapView to MKMapView’s alloc()’s initWithFrame:(current application’s NSMakeRect(410, 0, aMaxViewWidth – 410, aMaxViewHeight))
  
tell aMapView
    its setMapType:(MKMapTypeStandard)
    
its setZoomEnabled:true
    
its setScrollEnabled:true
    
its setPitchEnabled:true
    
its setRotateEnabled:true
    
its setShowsCompass:true
    
its setShowsZoomControls:true
    
its setShowsScale:true
    
its setShowsUserLocation:true
    
its setDelegate:me
  end tell
  
  
— make browser view with scroll view
  
set aScrollWithTable to makeBrowserView(prefList, 400, aMaxViewHeight) of me
  
  
–Compose Views in NSView
  
theView’s setSubviews:{aScrollWithTable, aMapView}
  
  
— 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:theView
  end tell
  
  
— 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 error number -128
end chooseItemByBrowser:

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

on makeBrowserView(aList as list, aWidth as number, aHeight as number)
  set (my theDataSource) to NSMutableArray’s arrayWithArray:aList
  
  
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
set aBrowser to NSBrowser’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
  
aBrowser’s setDelegate:(me)
  
aBrowser’s setTarget:(me)
  
aBrowser’s setAction:"browserCellSelected:"
  
aBrowser’s setMinColumnWidth:120
  
aBrowser’s setSeparatesColumns:true
  
aBrowser’s setMaxVisibleColumns:2
  
aBrowser’s setAutohidesScroller:true
  
aBrowser’s setTakesTitleFromPreviousColumn:true
  
–aBrowser’s setBackgroundColor:(NSColor’s grayColor())
  
  
aScroll’s setDocumentView:aBrowser
  
aBrowser’s enclosingScrollView()’s setHasHorizontalScroller:true
  
aBrowser’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
return aScroll
end makeBrowserView

–NSBrowser Event Handlers
–ここ、いまひとつきちんと動いていないかも???
on browser:aView numberOfRowsInColumn:aColumn
  if aColumn = 0 then
    return my theDataSource’s |count|()
  else if aColumn = 1 then
    set aPath to text 2 thru -1 of ((aView’s |path|()) as string)
    
set tmpArray to (my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")) as list
    
return (length of tmpArray)
  end if
end browser:numberOfRowsInColumn:

on browser:aView willDisplayCell:(aCell) atRow:(rowIndex as integer) column:(colIndex as integer)
  if colIndex = 0 then
    –Prefectures
    
aCell’s setTitle:((item (rowIndex + 1) of prefList) as string)
    
aCell’s setLeaf:false
    
  else if colIndex = 1 then
    –Each Game Centers in the Prefecture
    
set aPath to text 2 thru -1 of ((aView’s |path|()) as string)
    
set tmpArray to my filterRecListByLabel1(skDataList, "address BEGINSWITH ’" & aPath & "’")
    
set tmpItem to (tmpArray’s objectAtIndex:rowIndex)
    
    
set aGameCenterName to (tmpItem’s gcName) as string
    
aCell’s setTitle:(aGameCenterName)
    
aCell’s setLeaf:true
    
  end if
end browser:willDisplayCell:atRow:column:

on browserCellSelected:aSender
  set aPath to my aBrowser’s |path|()
  
set aList to (aPath’s pathComponents()) as list
  
set aLen to length of aList
  
  
if aLen = 3 then
    –set aPref to contents of item 2 of aList
    
set aGc to contents of last item of aList
    
    
set tmpArray to my filterRecListByLabel1(skDataList, "gcName == ’" & aGc & "’")
    
set tmpItem to contents of first item of (tmpArray as list)
    
    
copy tmpItem to my aSelection
    
    
set aLatitude to (latitude of tmpItem) as real
    
set aLongitude to (longitude of tmpItem) as real
    
    
tell aMapView
      set aLocation to current application’s CLLocationCoordinate2DMake(aLatitude, aLongitude)
      
its setCenterCoordinate:aLocation zoomLevel:(zLevel) animated:false
    end tell
    
  end if
end browserCellSelected:

–NSArrayに入れたNSDictionaryを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList, aPredicate as string)
  set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aRecList’s filteredArrayUsingPredicate:aPredicate
  
return filteredArray
end filterRecListByLabel1

★Click Here to Open This Script 

Posted in geolocation list Record | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy MKMapTypeStandard MKMapView NSAlert NSAlertSecondButtonReturn NSBrowser NSColor NSMutableArray NSRunningApplication NSScrollView NSView | Leave a comment

アラートダイアログ上に複数のNSBoxを作成してMKMapViewを表示

Posted on 3月 11, 2019 by Takaaki Naganoya

指定されたIPアドレスの位置情報(geo location)を検索して、アラートダイアログ上に拡大レベルの異なる4つの地図を表示するAppleScriptです。

IP Goecodingのサービスはipinfo.ioを利用しています。ただ、この手のサービスは入れ替わりが激しいので、長期的に使い続けられることを期待できないと感じています(有償サービスは別)。

このズームレベルが異なる地図の同時表示Viewは、作成したときには「これは画期的!」「ものすごく使いやすい!」と、狂喜乱舞したものですが、他のユーザーに見せてデモしたら、

「実際には限定されたエリア内の位置データを見ることが多いので、World LevelとかCountry Levelのビューは無駄なことが多い」
「地球を侵略しに来た異星人には向いているが、地球人向けには冗長」

といった意見が多く、オクラ入りしていました。アラートダイアログでさまざまなデータを可視化する部品の整備計画時に倉庫から引っ張り出されてきたものです。

唯一、IPアドレスという「見ただけではどこの国のものだかわからない」(Class AのIPは別。17.のAppleとか)データを可視化するときにはバッチリ合っています。

macOS 10.12〜10.14で確認していますが、唯一、macOS 10.14.4上では初期状態でピンが表示されません。ピン自体は存在しているので、地図表示タイプを変更すると表示されるのですが、一体これはどうしたものか。仕様なのかバグなのかわかりません。


▲なぜか本Blogにロス市警からのアクセスが(汗)


▲macOS 10.12.6 Map


▲macOS 10.12.6 Satellite


▲macOS 10.12.6 Map + Satellite


▲macOS 10.13.6 Map


▲macOS 10.14.4 Map (Light Mode)


▲macOS 10.14.4 Map (Dark Mode)

AppleScript名:アラートダイアログ上に複数のNSBoxを作成してMKMapViewを表示
— Created 2019-03-11 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"

property NSAlert : a reference to current application’s NSAlert
property NSString : a reference to current application’s NSString
property NSScreen : a reference to current application’s NSScreen
property MKMapView : a reference to current application’s MKMapView
property MKMapTypeHybrid : a reference to current application’s MKMapTypeHybrid
property MKPointAnnotation : a reference to current application’s MKPointAnnotation
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property MKMapTypeSatellite : a reference to current application’s MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application’s MKMapTypeStandard
property NSSegmentedControl : a reference to current application’s NSSegmentedControl
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding
property NSSegmentStyleTexturedRounded : a reference to current application’s NSSegmentStyleTexturedRounded

property windisp : false
property selSeg : 0
property aMapViewList : {}

property segTitleList : {"Map", "Satellite", "Satellite + Map"}

property returnCode : 0

set aClip to the clipboard –このへんてきとう
set anIP to text returned of (display dialog "Input IP address to find its location" default answer aClip)

set windisp to false
set geoInfo to getGeoLocationByIPinfo(anIP) of me
if geoInfo = missing value then
  error "Network Error"
end if

set aInfo to loc of geoInfo

set aPos to offset of "," in aInfo
set aLatitude to text 1 thru (aPos – 1) of aInfo
set aLongitude to text (aPos + 1) thru -1 of aInfo

set aWidth to 1000
set aHeight to 600

set aButtonMSG to "OK"
set aMapViewList to {}

set paramObj to {viewWidth:aWidth, viewHeight:aHeight, viewTitle:anIP, viewSubTitle:"IP-Geocoding Service by ipinfo.io", viewLat:aLatitude, viewLong:aLongitude}

my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true

on dispMapViewinDifferentScales:paramObj
  set aWidth to (viewWidth of paramObj) as real
  
set aHeight to (viewHeight of paramObj) as real
  
set aLat to (viewLat of paramObj) as real
  
set aLong to (viewLong of paramObj) as real
  
set aTitle to (viewTitle of paramObj) as string
  
set aSubTitle to (viewSubTitle of paramObj) as string
  
  
set selSeg to 0
  
  
–NSViewをつくる
  
set aView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
  
–各レベルのMapViewをBoxでつくる
  
set wList to {{3, "🌏World Level Map"}, {5, "🏰Country Level Map"}, {10, "🏢City Level Map"}, {17, "🏠Town Level Map"}}
  
set xPos to 0
  
repeat with i in wList
    copy i to {aLevelNum, aBoxTitle}
    
    
–Boxをつくる
    
set aBox to (current application’s NSBox’s alloc()’s initWithFrame:(current application’s NSMakeRect(xPos, 40, aWidth * 0.25, aHeight – 70)))
    (
aBox’s setTitle:aBoxTitle)
    
    
–MapView+Pinをつくる
    
set aMapView to makeMKMapView(aWidth * 0.25, aHeight – 70, aLevelNum, aLat, aLong, aTitle) of me
    
    (
aBox’s addSubview:aMapView)
    (
aView’s addSubview:aBox)
    
    
set the end of aMapViewList to aMapView
    
set xPos to xPos + (aWidth * 0.25)
  end repeat
  
  
–Segmented Controlをつくる
  
set aSeg to makeSegmentedControl(segTitleList, aWidth, aHeight) of me
  
aView’s addSubview:aSeg
  
  
— set up alert  
  
set theAlert to NSAlert’s alloc()’s init()
  
tell theAlert
    its setMessageText:aTitle
    
its setInformativeText:aSubTitle
    
its addButtonWithTitle:"OK"
    
its addButtonWithTitle:"Cancel"
    
its setAccessoryView:aView
  end tell
  
  
— 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 error number -128
end dispMapViewinDifferentScales:

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

–MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong, aTitle)
  set aMapView to MKMapView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
aMapView’s setMapType:(current application’s MKMapTypeStandard)
  
  
aMapView’s setZoomEnabled:true
  
aMapView’s setScrollEnabled:true
  
aMapView’s setPitchEnabled:false
  
aMapView’s setRotateEnabled:false
  
aMapView’s setShowsCompass:true
  
aMapView’s setShowsZoomControls:true
  
aMapView’s setShowsScale:true
  
aMapView’s setShowsUserLocation:true
  
  
set aLocation to current application’s CLLocationCoordinate2DMake(aLat, aLong)
  
aMapView’s setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
  
aMapView’s setDelegate:me
  
  
–MapにPinを追加
  
set anAnnotation to current application’s MKPointAnnotation’s alloc()’s init()
  
anAnnotation’s setCoordinate:aLocation
  
anAnnotation’s setTitle:aTitle
  
aMapView’s addAnnotation:anAnnotation
  
  
return aMapView
end makeMKMapView

–Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
  set aLen to length of titleList
  
  
set aSeg to NSSegmentedControl’s alloc()’s init()
  
aSeg’s setSegmentCount:aLen
  
  
set aCount to 0
  
repeat with i in titleList
    set j to contents of i
    (
aSeg’s setLabel:j forSegment:aCount)
    
set aCount to aCount + 1
  end repeat
  
  
aSeg’s setTranslatesAutoresizingMaskIntoConstraints:false
  
aSeg’s setSegmentStyle:(NSSegmentStyleTexturedRounded)
  
aSeg’s setFrame:(current application’s NSMakeRect(10, 5, 260, 30))
  
aSeg’s setTrackingMode:0
  
aSeg’s setTarget:me
  
aSeg’s setAction:"clickedSeg:"
  
aSeg’s setSelectedSegment:0
  
  
return aSeg
end makeSegmentedControl

–Segmented Control’s clicked event handler
on clickedSeg:aSender
  set aSel to aSender’s selectedSegment()
  
set selSeg to (aSel + 1)
  
set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
  
set curMap to contents of item selSeg of mapList
  
  
repeat with i in aMapViewList
    set aView to contents of i
    (
aView’s setMapType:(curMap))
  end repeat
end clickedSeg:

–http://ipinfo.io/developers
on getGeoLocationByIPinfo(myIP)
  set aURL to "http://ipinfo.io/" & myIP
  
set aRes to callRestGETAPIAndParseResults(aURL, 10) of me
  
return aRes as record
end getGeoLocationByIPinfo

on callRestGETAPIAndParseResults(reqURLStr as string, timeoutSec as integer)
  set tmpData to (do shell script "curl -X GET \"" & reqURLStr & "\"")
  
set jsonString to NSString’s stringWithString:tmpData
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set aJsonDict to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
if aJsonDict = missing value then return false
  
return (aJsonDict as record)
end callRestGETAPIAndParseResults

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 

Posted in geolocation Network REST API | Tagged 10.11savvy 10.12savvy MKMapTypeHybrid MKMapTypeSatellite MKMapTypeStandard MKMapView MKPointAnnotation NSAlert NSJSONSerialization NSRunningApplication NSScreen NSSegmentedControl NSSegmentStyleTexturedRounded NSString NSUTF8StringEncoding | Leave a comment

Keyboard Viewerを表示する

Posted on 3月 10, 2019 by Takaaki Naganoya

入力メニューから呼び出すことのできる「キーボードビューア」(ソフトウェアキーボード)を表示するAppleScriptです。

macOSには、成り立ちのことなる2系統のソフトウェアキーボードが搭載されています。

文字入力補助用の「キーボードビューア」

1つが、本Scriptで対象にしている「キーボードビューア」。入力メニューから呼び出すタイプのソフトウェアキーボードです。昔のClassic Macintoshの時代には、ノート型のPowerBookが登場するまで(Macintosh Portableもあったものの、キーボードが取り外し可能でした)本体とキーボード、マウスが分離するタイプのコンピュータであったため、ソフトウェアキーボードの存在は必要なものでした。

キーボード未接続時の緊急用に「キー配列」というキーボードビューアの前進であるソフトウェアを使うことがありました。利用頻度ははとても低いものでした。マウスしかつなげていない時にキー入力するといった程度のものです。

昔、Classic MacOSの時代に、日本語手書きフォントでカタカナにマッピングしていたもの(R研究所の日本語手書きフォントなど)があったので、そうした変則的なフォントを入力するための補助UIとして使ったとかいう記憶はあります。

障害者補助用の「キーボード」スイッチ

もう1つが、アクセシビリティ系の機能で提供されている「キーボード」。システム環境設定>アクセシビリティ>スイッチコントロールで、「ホーム」のスイッチから、「キーボード」ボタンを選択すると表示されるソフトウェアキーボードです。

単なる文字入力だけではなく、キーボード自体が使えないユーザー向けにカーソル移動やマウスの左右クリック、ドラッグ&ドロップ、スクロールや日本語かな漢字変換などの機能を提供しています。

「キーボード」スイッチは、表示位置を変更できるものの、サイズの変更は行えませんとサイズの変更が可能です。キーボードビューアもリサイズが可能です。

昨今ソフトウェアキーボードの存在に注目が集まる

これらソフトウェアキーボード類は、タブレット型のPCでmacOSを動かしたり、Macの外部ディスプレイとしてiPadを用いる利用方法が紹介されるようになって、近年その存在が再注目されつつあります。

本AppleScriptはキーボードビューアを呼び出して表示します。呼び出しにBridgePlusを必要としており、macOS 10.14.x上で実行するためには、AppleScriptをバンドル形式のアプレットとして書き出し、アップレットのバンドル中にBrdgePlusを同梱する必要があります。

AppleScript名:Keyboard Viewerを表示する
— Created 2015-09-16 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePLus : script "BridgePlus" version "1.2" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property targInputID : "com.apple.KeyboardViewer"

load framework
set aList to (current application’s SMSForder’s allAvailableInputSourceIDs()) as list
if targInputID is in aList then
  set x to current application’s SMSForder’s changeInputSourceTo:targInputID
  
if x = false then error "Can not change input source."
end if

★Click Here to Open This Script 

Posted in Input Method System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy | Leave a comment

Myriad Tables Libが1.0.9にアップデート

Posted on 3月 9, 2019 by Takaaki Naganoya

Shane StanleyのAppleScript Libraries「Myriad Tables Lib」の新バージョンv1.0.9がリリースされました。

→ ライブラリのページ


▲ソリューションによっては、機能のほとんどにテーブルビューUIが必要とされるものも

さまざまなソリューション開発時に登場頻度が高いものの、作ると割と手間が必要で大変な「テーブルビュー」を利用するScriptの作成を大幅に省力化できるライブラリです。

今回のv1.0.9ではリクエストを出していたhiddenフィールドの機能が追加されました。ユーザー名とパスワードを表UIで編集・表示したい場合にパスワード部分をhidden表示できると便利です。

このほか、macOS 10.14のDark Modeへの対応やドイツ語ローカライズの追加が行われました。

AppleScript名:Sample using version 1.0.9 hidden field.scpt
use AppleScript version "2.4"
use scripting additions
use script "Myriad Tables Lib" version "1.0.9"

set theHeads to {"First Name", "Last Name", "Index", "Police", "Score", "Some Date"}
set theDate to current date
set someData to {{"Saga", "Norén", 1, true, 12.0, theDate}, ¬
  {"Rasmus", "Larsson", 2, true, 13.5, missing value}, ¬
  {
"Freddie", "Holst", 3, false, 9.0, theDate + 40000}, ¬
  {
"Claes", "Sandberg", 4, false, 1.23456789E+4, theDate + 50000}, ¬
  {
"John", "Lundqvist", 5, true, 13.4567, theDate + 30000}, ¬
  {
"Annika", "Melander", 6, false, 22.0, theDate + 60000}}

— typical process: make table with "table dialog with data", modify if required, then display
set myTable to make new table with data someData with title "Sample table" column headings theHeads with prompt "You can select multiple rows. Everything is editable. Uses a row template. The Last Name field’s values are hidden." editable columns {} row template {"", "hidden", 1, true, 1.0, current date, missing value} with multiple selections allowed, row numbering and empty selection allowed
modify table myTable highlighted rows {2, 4} grid style grid both dashed between rows OK button name "Cool"
set theResult to display table myTable

★Click Here to Open This Script 

Posted in GUI | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy | Leave a comment

レコード in リストのラベルを置換する

Posted on 3月 9, 2019 by Takaaki Naganoya

レコード in リスト(配列に入れたレコード)のラベルを置換するAppleScriptです。

普通にプログラムを書いているととても必要な処理なので、Cocoaに最初から用意されているような気がするのですが、自分が知らないだけなんでしょうか?

# レコードのラベル部分を書き換えるのではなく、アクセス時の「valueForKeyPath」で指定するパス指定文字列のほうを書き換えたほうがまっとうな処理な気がします

AppleScript名:レコード in リストのラベルを置換する
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/09
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aList to {{gcName:"ディノスパーク札幌中央", address:"北海道 札幌市中央区 南三条西 1-8 ゲオディノス札幌中央ビル", loc_id:"94fQ-vXfZIWbNCIhWjMEEQ", latitude:43.0568243, longitude:141.3567126}, {gcName:"アドアーズ狸小路", address:"北海道 札幌市中央区 南三条西 4-12-1 アルシュビル5・6F", loc_id:"BaAw8srtioGkso463Jlrpg", latitude:43.0569575, longitude:141.352524}}

set labelRepList to {{"gcName", "latitude", "longitude"}, {"placeName", "aLat", "aLong"}}
set origLabel to contents of first item of labelRepList
set toLabel to contents of second item of labelRepList

set rList to repRecListLabels(aList, origLabel, toLabel) of me
–> {{placeName:"ディノスパーク札幌中央", loc_id:"94fQ-vXfZIWbNCIhWjMEEQ", aLong:141.3567126, address:"北海道 札幌市中央区 南三条西 1-8 ゲオディノス札幌中央ビル", aLat:43.0568243}, {placeName:"アドアーズ狸小路", loc_id:"BaAw8srtioGkso463Jlrpg", aLong:141.352524, address:"北海道 札幌市中央区 南三条西 4-12-1 アルシュビル5・6F", aLat:43.0569575}}

on repRecListLabels(aList as list, origLabel as list, toLabel as list)
  script spdRecList
    property newList : {}
    
property aList : {}
  end script
  
  
set (newList of spdRecList) to {}
  
set (aList of spdRecList) to aList
  
  
repeat with i in (aList of spdRecList)
    set aDict to (current application’s NSMutableDictionary’s dictionaryWithDictionary:i)
    
set allKeys to (aDict’s allKeys()) as list
    
set bDict to (current application’s NSMutableDictionary’s new())
    
    
repeat with ii in allKeys
      set jj to contents of ii
      
set tmpVal to (aDict’s valueForKey:jj)
      
      
if jj is in origLabel then
        using terms from scripting additions
          set aRes to offset of jj in origLabel
        end using terms from
        
set aLabel to contents of item aRes of toLabel
        (
bDict’s setObject:tmpVal forKey:aLabel)
      else
        (bDict’s setObject:tmpVal forKey:jj)
      end if
      
    end repeat
    
    
set bDict to bDict as record
    
set the end of (newList of spdRecList) to bDict
  end repeat
  
  
return (newList of spdRecList)
end repRecListLabels

on offset of bArg in anArg
  set aClass to class of anArg
  
set bClass to class of bArg
  
  
if {aClass, bClass} = {text, text} then –case 1
    return getOffset(anArg, bArg) of me
  else if {aClass, bClass} = {list, list} then –case 2 (The target case)
    return execOffsetList(bArg, anArg) of me
  else if {aClass, bClass} = {text, list} then –case 3 (Illegular case)
    return execOffsetList(bArg, {anArg}) of me
  else if {aClass, bClass} = {list, text} then –case 4 (Illegular case)
    return execOffsetList({bArg}, anArg) of me
  end if
end offset

–1D List同士のoffset演算を行うルーチンの本体
on execOffsetList(aList as list, bList as list)
  set resList to {}
  
repeat with i in aList
    set j to contents of i
    
set aCount to 1
    
    
repeat with ii in bList
      set jj to contents of ii
      
if jj = j then
        set the end of resList to aCount
        
exit repeat
      end if
      
set aCount to aCount + 1
    end repeat
  end repeat
  
  
–見つかったItem No.が連続値かどうかチェック
  
set sRes to chkSequential(resList) of me
  
if sRes = true then
    return contents of first item of resList
  else
    return false
  end if
end execOffsetList

–与えられた1D Listが連続値かどうかをチェックする
on chkSequential(aList as list)
  if length of aList = 1 then return true
  
if aList = {} then return false
  
  
set aFirst to first item of aList
  
set aList to rest of aList
  
  
repeat with i in aList
    set j to contents of i
    
if j is not equal to (aFirst + 1) then
      return false
    end if
    
copy j to aFirst
  end repeat
  
  
return true
end chkSequential

–テキスト同士のoffset ofを(2.5x fasterで)実行する
on getOffset(str as string, searchStr as string)
  set d to divideBy(str, searchStr)
  
if (count d) is less than 2 then return 0
  
return (length of item 1 of d) + 1
end getOffset

on divideBy(str, separator)
  set delSave to AppleScript’s text item delimiters
  
set the AppleScript’s text item delimiters to separator
  
set strItems to every text item of str
  
set the AppleScript’s text item delimiters to delSave
  
return strItems
end divideBy

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy | Leave a comment

画像の空白判定 v3

Posted on 3月 7, 2019 by Takaaki Naganoya

指定の画像のドットがすべて白色かをチェックする(=画像の空白判定)AppleScriptです。

画像の空白判定処理は自分的にはひじょうに重要な処理であり、画像をグレースケール化しておいて、

(方法1)Photoshopを用いて明度ヒストグラムを取得し、明度=255のデータだけが存在することを確認
(方法2)GPUImage.frameworkを用いて明度ヒストグラムを取得し、明度=255のデータだけが存在することを確認

といった方法で確認を行なっていました。

PDFの余白ページ判定処理や、画像同士の差分確認など、Photoshopを使わずに済めば利用範囲も広がるため(Mac App Storeに出せるため)、AppleScript+Frameworkぐらいで高速処理できることにはものすごく価値があります。

そんな中、GPUImageは急速に2度の方向転換を行い、AppleScriptからは付き合いにくいフレームワークに変化しました。

全面的にSwiftで書き換えたGPUImage2、さらにmacOS 10.14で行われた「OpenGL/OpenCLの非推奨化」という方針転換(わかっていたことですが)を受け、Metalを活用するように書き換えられたGPUImage3へと姿を変えました。オリジナルから見るとほぼ別物です。

GPUImage 3はまだまだ機能不足なうえにAppleScriptから呼べない状態。ヒストグラムの計算フィルタも搭載されていません。Objective-Cで書かれ、中国のスマホ開発者が写真加工するのに活用しまくった、人民に愛されまくったGPUImageの姿はもう見られないのでしょうか。

GPUImageを用いた他のフィルタ処理はCIFilterで代替できるのであまり問題にはなりませんが、この空白画像検出処理だけはなんとしても代替手段を見つける必要に迫られました。応用例が多すぎるからです。

そこで思いついたのが、「チェック対象の画像と同サイズの白い画像を作って、データ内容が同じかどうか調べる」というシンプルな方法(最初から思いついてほしい>自分)。

これならCPUパワーもそれほど必要とせず、GPUの力を絞り出す必要もありません(あたりまえ)。

さっそく書いてみたものの、今度はどうも「白い色」の値が合わず、頭をひねりまくりました。


▲1×1ドット画像を新規作成して白く塗りつぶして比較。Photoshopで作成してファイルから読み込んだ画像とDataが同じにならない

カラープロファイルが合わないために「白い色」を指定してもイコールにならないようだったので、オリジナル画像をコピーしてそれ自体を白く塗りつぶして空白検出の比較対象としてみました。これで空白検出が無事できるようになりました。しかも、GPUImage.frameworkを使っていたバージョンよりもあからさまに高速、、、、

処理速度をPhotoshop版、GPUImage版のAppleScriptと比較してみたところ、1980×1200ピクセルぐらいの画像だとGPUImage版の倍ぐらい高速、8K(7680×4320)ぐらいになるとPhotoshopに負けるといったところです。


▲同一環境にて、Photoshop CC 2018、GPUImage、本Scriptで各種サイズの画像の空白検出を実行(単位:秒)

処理内容がシンプルなだけに小さい画像の処理は得意で、大きな画像は不向きといえるかもしれません。テスト機は例によってCore i7 2.66GHzメモリ8GBのマシンであり、より搭載メモリ量の多いマシンで実行すると挙動が変わってくるかもしれません。

補足までに、GPUImageの明度ヒストグラム検出は、結果を数値の配列ではなく、1×256ドットの「画像」として返してくる変態仕様なので、結果を判定するために1×256ピクセルの画像をループでチェックする必要があります。この仕様が余計なオーバーヘッドを生んでいる(つまり、GPUで処理しているから爆速、という世間の期待値を大幅に下回る処理内容になっている)可能性は否定できません。

ただ、PhosothopなしでPhotoshopと同様のヒストグラム処理が行えるという「手軽さ」がいいと思ってGPUImageを使い出したので、速度をベンチマークしてみると「こんなもんだろ」という印象です。

AppleScript名:画像の空白判定 v3.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/07
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

property NSData : a reference to current application’s NSData
property NSDate : a reference to current application’s NSDate
property |NSURL| : a reference to current application’s |NSURL|
property NSColor : a reference to current application’s NSColor
property NSImage : a reference to current application’s NSImage
property NSBezierPath : a reference to current application’s NSBezierPath
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep

set aPOSIXpath to POSIX path of (choose file of type {"public.image"})

set a1Dat to NSDate’s timeIntervalSinceReferenceDate()

set iRes to checkImageIsWhite(aPOSIXpath) of me

set b1Dat to NSDate’s timeIntervalSinceReferenceDate()
set c1Dat to b1Dat – a1Dat

return {iRes, c1Dat}

–Compare Original Data and
on checkImageIsWhite(aPOSIXpath)
  set aURL to |NSURL|’s fileURLWithPath:(aPOSIXpath)
  
set anNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL)
  
  
copy anNSImage to bNSImage
  
  
set fillColor to makeNSColorFromRGBAval(65535, 65535, 65535, 65535, 65535) of me
  
–set fillColor to NSColor’s whiteColor()
  
set blankImage to drawImageWithFilledColor(bNSImage, fillColor) of me
  
  
set imgA to anNSImage’s TIFFRepresentation()
  
set imgB to blankImage’s TIFFRepresentation()
  
  
set chkWhite to (imgA’s isEqualToData:imgB) as boolean
  
return chkWhite
end checkImageIsWhite

on getSizeOfImage(anNSImage)
  set aSize to anNSImage’s |size|()
  
set aClass to class of aSize
  
if aClass = record then
    copy aSize to theSize –To macOS 10.12.x
  else –macOS 10.13 or later
    set sizeX to (item 1 of item 2 of aSize)
    
set sizeY to (item 2 of item 2 of aSize)
    
set theSize to {width:sizeX, height:sizeY}
  end if
  
return theSize
end getSizeOfImage

–指定サイズの画像を作成し、背景を指定色で塗る
on drawImageWithFilledColor(anImage, fillColor)
  set aSize to getSizeOfImage(anImage) of me
  
  
anImage’s lockFocus()
  
  
set theRect to {{x:0, y:0}, {width:(width of aSize), height:(height of aSize)}}
  
set theNSBezierPath to NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
fillColor’s |set|()
  
theNSBezierPath’s fill()
  
  
anImage’s unlockFocus()
  
  
return anImage
end drawImageWithFilledColor

–aMaxValを最大値とする数値でNSColorを作成して返す
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

★Click Here to Open This Script 

Posted in Image | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSBezierPath NSColor NSData NSDate NSImage NSURL | 3 Comments

WiFiデバイスのパワーを操作

Posted on 3月 6, 2019 by Takaaki Naganoya

Macに内蔵/接続されている無線LAN(WiFi)デバイスのパワー(電源)をオン/オフ操作するAppleScriptです。

AppleScript名:WiFiデバイスのパワーを操作
— Created 2015-08-18 by Shane Stanley
— Modified 2019-03-06 by Takaaki Naganoya
use AppleScript version "2.4" –macOS 10.10 or later
use scripting additions
use framework "Foundation"
use framework "CoreWLAN"

property CWInterface : a reference to current application’s CWInterface

–Power On WiFi
set w1Res to powerControlEveryWiFiDevices(true) of me

delay 5

–Power Off WiFi
set w2Res to powerControlEveryWiFiDevices(false) of me

on powerControlEveryWiFiDevices(aFlag as boolean)
  set allNames to CWInterface’s interfaceNames()’s allObjects() as list
  
if allNames = {} then return false
  
  
set powerList to {}
  
repeat with i in allNames
    set j to contents of i
    
set aInterface to (CWInterface’s interfaceWithName:j)
    
set wRes to (aInterface’s setPower:aFlag |error|:(missing value))
    
    
–Get Power state and check it
    
set aPower to (aInterface’s powerOn()) as boolean
    
if aPower = (not aFlag) then
      if aFlag = true then
        set aStat to " on "
      else
        set aStat to " off "
      end if
      
display notification "Error occured in power" & aStat & "an Wifi deviece ( " & j & " )…."
    end if
    
set the end of powerList to aPower
  end repeat
  
  
return ({aFlag} is in powerList) –return whether some WiFi interface is on/off
end powerControlEveryWiFiDevices

★Click Here to Open This Script 

Posted in System WiFi | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy CWInterface | Leave a comment

アラートダイアログ上にWebViewを表示

Posted on 3月 5, 2019 by Takaaki Naganoya

アラートダイアログ上にWKWebViewを表示するAppleScriptです。

テストのためにYouTubeのムービーの自動再生URL(戦場の絆のリプレイムービー)をオープンしています。


▲Table ViewとWeb Viewを組み合わせて、所定の場所にYouTubeムービーの頭出しを行う試作品を作ってみたものの、JavaScript経由でWKWebViewをコントロールするのが難しくて頓挫

本来であれば、WkWebViewに対してJavaScript経由でさまざまな操作を行いたいところですが、オープン中のWebコンテンツに対して新たなJavaScriptのインスタンスを生成してセキュリティチェックを行なって、実際に実行を行うのが(自分には)少々難しく感じました。WkWebViewだとあまり凝った制御はできない印象です。Safariをコントロールするほうが自由度が高いところ。

WkWebViewを「自由にこづきまわして操作できる部品」として使うためには、素のままのWkWebViewではない何かを使ったほうがいいのかも?

AppleScript名:アラートダイアログ上にWebViewを表示.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 NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property WKWebView : a reference to current application’s WKWebView
property NSScrollView : a reference to current application’s NSScrollView
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 NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered
property WKUserContentController : a reference to current application’s WKUserContentController
property NSMomentaryLightButton : a reference to current application’s NSMomentaryLightButton
property WKWebViewConfiguration : a reference to current application’s WKWebViewConfiguration
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn
property WKUserScriptInjectionTimeAtDocumentEnd : a reference to current application’s WKUserScriptInjectionTimeAtDocumentEnd

property theResult : 0
property returnCode : 0
property theDataSource : {}

set aURL to "https://www.youtube.com/embed/GP_tVXTYdmY?autoplay=1&hd=1"
set paramObj to {myMessage:"Browse a Replay", mySubMessage:"Browse Senjo-No-Kizuna Replay Movie", targURL:aURL}
my performSelectorOnMainThread:"browseWebContents:" withObject:(paramObj) waitUntilDone:true

on browseWebContents:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set tmpURL to (targURL of paramObj)
  
  
set aWidth to 600
  
set aHeight to 450
  
  
–WebViewをつくる
  
set aConf to WKWebViewConfiguration’s alloc()’s init()
  
  
–指定URLのJavaScriptをFetch
  
set jsSource to my fetchJSSourceString(tmpURL)
  
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 URLWithString:tmpURL
  
set aReq to NSURLRequest’s requestWithURL:bURL
  
aWebView’s loadRequest:aReq –Webコンテンツのローディング
  
  
— 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
  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 browseWebContents:

on doModal:aParam
  set (my returnCode) to aParam’s runModal()
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

★Click Here to Open This Script 

Posted in Internet URL | Tagged 10.11savvy 10.12savvy 10.13savvy NSAlert NSAlertSecondButtonReturn NSBackingStoreBuffered NSButton NSColor NSMomentaryLightButton NSRunningApplication NSScreen NSString NSURL NSURLRequest NSUTF8StringEncoding WKUserContentController WKUserScript WKUserScriptInjectionTimeAtDocumentEnd WKWebView WKWebViewConfiguration | Leave a comment

アラートダイアログ上に縦棒グラフを表示

Posted on 3月 4, 2019 by Takaaki Naganoya

アラートダイアログ上に縦棒グラフを表示するAppleScriptです。

グラフを作成するのであれば、NumbersやKeynoteなどを使ったほうがいいですが、簡単にデータの傾向だけダイアログ上で見せたいような場合のために作成した部品です。

NSImageViewの表示実験がしたかっただけなので、別に他の言語処理系で作成したグラフ画像を読み込んでNSImageに読み込んでからNSIMageViewで表示するぐらいの使い方でもぜんぜんOKでしょう。

WebViewを表示してインタラクティブなグラフ(マウスカーソルに反応してアニメーション表示を行うとか)を表示するといった使い方もよさそうです(アプリケーション化しないとダメかも)。

macOS 10.14上のDark Modeについては、実行環境によって対応度が異なります。


▲macOS 10.14上のDark Modeで実行したところ。左側からスクリプトエディタ、AppleScriptアプレット、スクリプトメニュー


▲macOS 10.14上のDark Modeで実行したところ。左側からScript Debugger、Script DebuggerからSD拡張アプレットで書き出したもの

グラフの描画については、色指定をblackColor()とかgrayColor()などと指定せずに、Dark Mode時に制御されるUI色で指定しておく必要があることでしょう。

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

property NSFont : a reference to current application’s NSFont
property NSAlert : a reference to current application’s NSAlert
property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property NSScreen : a reference to current application’s NSScreen
property NSDictionary : a reference to current application’s NSDictionary
property NSBezierPath : a reference to current application’s NSBezierPath
property NSImageView : a reference to current application’s NSImageView
property NSMutableArray : a reference to current application’s NSMutableArray
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName
property NSImageScaleProportionallyUpOrDown : a reference to current application’s NSImageScaleProportionallyUpOrDown

property returnCode : 0

set plotData to {20, 30, 100, 80, 150, 90}
set paramObj to {myMessage:"This is a graph", mySubMessage:"Browse bar graph", aPlotData:plotData}
my performSelectorOnMainThread:"displayBarGraph:" withObject:(paramObj) waitUntilDone:true

on displayBarGraph:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set aTList to (aPlotData of paramObj) as list
  
  
— make NSIMageView with graph image
  
set anImage to makeGraphImage(aTList, 300, 200) of me
  
set anImageView to NSImageView’s alloc()’s initWithFrame:{origin:{x:0.0, y:0.0}, |size|:{width:300.0, height:200.0}}
  
anImageView’s setImageScaling:(NSImageScaleProportionallyUpOrDown)
  
anImageView’s setEditable:false
  
anImageView’s setImage:anImage
  
anImageView’s display()
  
  
— 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:anImageView
  end tell
  
  
— 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 error number -128
end displayBarGraph:

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

on makeGraphImage(plotData, aWidth, aHeight)
  set innerGapL to 30
  
set innerGapU to 10
  
set innerGapR to 20
  
set innerGapD to 20
  
set barGap to 10
  
  
–パラメータから下地になる画像を作成する
  
set aSize to current application’s NSMakeSize(aWidth, aHeight)
  
set anImage to NSImage’s alloc()’s initWithSize:aSize
  
  
–各種パラメータの計算
  
–copy plotArea to {plotWidth, plotHeight}
  
set itemNum to count every item of plotData
  
set barThickness to (aWidth – (itemNum * barGap * 2)) div itemNum
  
  
–プロットデータの最大値
  
set anArray to NSArray’s arrayWithArray:plotData
  
set aYmax to (anArray’s valueForKeyPath:"@max.self")’s intValue()
  
set aMaxYVal to aHeight – innerGapU – innerGapD
  
set aYPlotArea to aHeight – innerGapU – innerGapD – 20
  
set aYUnit to aYPlotArea / aYmax
  
  
–数値データをもとに描画データを組み立てる
  
set drawList to {}
  
  
set startX to innerGapL
  
copy startX to origX
  
  
repeat with i in plotData
    set the end of drawList to current application’s NSMakeRect(startX, innerGapD, barThickness, innerGapD + (i * aYUnit))
    
set startX to startX + barThickness + barGap
  end repeat
  
  
–グラフ塗りつぶし処理呼び出し
  
set fillColor to (NSColor’s colorWithCalibratedRed:0.1 green:0.1 blue:0.1 alpha:0.3)
  
set resImage to drawImageWithColorFill(anImage, drawList, fillColor) of me
  
  
–数値データ(文字)をグラフィックに記入
  
set fillColor2 to NSColor’s blackColor()
  
set resImage to drawImageWithString(resImage, drawList, fillColor2, plotData, "Eurostile Bold", 20.0) of me
  
  
–補助線を引く
  
set fillColor3 to (NSColor’s colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.8)
  
set aVertical to current application’s NSMakeRect(origX, innerGapD, aWidth – innerGapL – innerGapR, 1)
  
set aHorizontal to current application’s NSMakeRect(origX, innerGapD, 1, aHeight – innerGapU – innerGapD)
  
set draw2List to {aVertical, aHorizontal}
  
set resImage to drawImageWithColorFill(resImage, draw2List, fillColor3) of me
  
  
return resImage
end makeGraphImage

–NSImageに対して文字を描画する
on drawImageWithString(anImage, drawList, fillColor, dataList, aPSFontName, aFontSize)
  set retinaF to (NSScreen’s mainScreen()’s backingScaleFactor()) as real
  
–>  2.0 (Retina) / 1.0 (Non Retina)
  
  
set aDict to (NSDictionary’s dictionaryWithObjects:{NSFont’s fontWithName:aPSFontName |size|:aFontSize, fillColor} forKeys:{NSFontAttributeName, NSForegroundColorAttributeName})
  
  
anImage’s lockFocus() –描画開始
  
  
set aLen to length of drawList
  
repeat with i from 1 to aLen
    set i1 to contents of item i of drawList
    
    
set v2 to system attribute "sys2"
    
if v2 ≤ 12 then
      –To macOS 10.12.x
      
set origX to (x of origin of i1) / retinaF
      
set origY to (y of origin of i1) / retinaF
      
set sizeX to (width of |size| of i1) / retinaF
      
set sizeY to (height of |size| of i1) / retinaF
      
set theRect to {{x:origX, y:origY}, {width:sizeX, height:sizeY}}
    else
      –macOS 10.13 or later
      
set origX to (item 1 of item 1 of i1) / retinaF
      
set origY to (item 2 of item 1 of i1) / retinaF
      
set sizeX to (item 1 of item 2 of i1) / retinaF
      
set sizeY to (item 2 of item 2 of i1) / retinaF
      
set theRect to {{origX, origY}, {sizeX, sizeY}}
    end if
    
    
set aString to (current application’s NSString’s stringWithString:((contents of item i of dataList) as string))
    (
aString’s drawAtPoint:(current application’s NSMakePoint(origX + (sizeX / 2), sizeY)) withAttributes:aDict)
  end repeat
  
  
anImage’s unlockFocus() –描画ここまで
  
  
return anImage –returns NSImage
end drawImageWithString

–NSImageに対して矩形を塗りつぶす
on drawImageWithColorFill(anImage, drawList, fillColor)
  set retinaF to (NSScreen’s mainScreen()’s backingScaleFactor()) as real
  
–>  2.0 (Retina) / 1.0 (Non Retina)
  
  
anImage’s lockFocus() –描画開始
  
  
repeat with i in drawList
    set v2 to system attribute "sys2"
    
if v2 ≤ 12 then
      –To macOS 10.12.x
      
set origX to (x of origin of i) / retinaF
      
set origY to (y of origin of i) / retinaF
      
set sizeX to (width of |size| of i) / retinaF
      
set sizeY to (height of |size| of i) / retinaF
      
set theRect to {{x:origX, y:origY}, {width:sizeX, height:sizeY}}
    else
      –macOS 10.13 or later
      
set origX to (item 1 of item 1 of i) / retinaF
      
set origY to (item 2 of item 1 of i) / retinaF
      
set sizeX to (item 1 of item 2 of i) / retinaF
      
set sizeY to (item 2 of item 2 of i) / retinaF
      
set theRect to {{origX, origY}, {sizeX, sizeY}}
    end if
    
    
set theNSBezierPath to NSBezierPath’s bezierPath
    (
theNSBezierPath’s appendBezierPathWithRect:theRect)
    
    
fillColor’s |set|() –色設定
    
theNSBezierPath’s fill() –ぬりつぶし
  end repeat
  
  
anImage’s unlockFocus() –描画ここまで
  
  
return anImage –returns NSImage
end drawImageWithColorFill

★Click Here to Open This Script 

Posted in GUI Image list | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSAlert NSAlertSecondButtonReturn NSArray NSBezierPath NSBitmapImageRep NSColor NSDictionary NSFont NSFontAttributeName NSForegroundColorAttributeName NSImage NSImageScaleProportionallyUpOrDown NSImageView NSMutableArray NSRunningApplication NSScreen NSString | Leave a comment

「戦場の絆」の日本国内の設置店舗数をカウント

Posted on 3月 3, 2019 by Takaaki Naganoya

アーケードゲーム「戦場の絆」が設置されている日本国内のゲームセンターの件数をカウントするAppleScriptです。

指定ページを取得し、本文内容をプレーンテキスト化して正規表現でデータ抽出して加算を行うという処理内容です。

部品がありきたりなので、新規作成部分はほとんどありません(メイン部分だけ)。

AppleScript名:戦場の絆の日本国内の設置店舗数をカウント.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/03/03
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSPredicate : a reference to current application’s NSPredicate
property NSURLQueryItem : a reference to current application’s NSURLQueryItem
property NSAttributedString : a reference to current application’s NSAttributedString
property NSURLComponents : a reference to current application’s NSURLComponents
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSUnicodeStringEncoding : a reference to current application’s NSUnicodeStringEncoding

set aTotal to 0

–「戦場の絆」公式ページに掲載されている店舗一覧の都道府県別コード
set areaList to {"JP-01", "JP-02", "JP-03", "JP-04", "JP-05", "JP-06", "JP-07", "JP-08", "JP-09", "JP-10", "JP-11", "JP-12", "JP-13", "JP-14", "JP-15", "JP-16", "JP-17", "JP-18", "JP-19", "JP-20", "JP-21", "JP-22", "JP-23", "JP-24", "JP-25", "JP-26", "JP-27", "JP-28", "JP-29", "JP-30", "JP-31", "JP-32", "JP-33", "JP-34", "JP-35", "JP-36", "JP-37", "JP-38", "JP-39", "JP-40", "JP-41", "JP-42", "JP-43", "JP-44", "JP-45", "JP-46", "JP-47"}

–「戦場の絆」公式ページに掲載されている店舗一覧のBase URL
set baseURL to "https://gundam-kizuna.jp/locations/list"

–都道府県コードでループ
repeat with i in areaList
  –店舗一覧の都道府県別ページをダウンロード
  
set j to contents of i
  
set aRec to {area:j}
  
set aRes to retURLwithParams(baseURL, aRec) of me
  
set shellText to "curl -s " & aRes
  
set htmlRes to (do shell script shellText)
  
set aPlainText to (HTMLDecode(htmlRes) of me)
  
  
–店舗件数が入っている箇所を抽出して加算
  
set aList to paragraphs of aPlainText
  
set anArray to (NSArray’s arrayWithArray:aList)
  
set aPred to (NSPredicate’s predicateWithFormat:"SELF MATCHES ’[0-9]{1,3}件の店舗が見つかりました。’")
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
  
if bRes is not equal to {} then
    set a to repChar(contents of first item of bRes, "件の店舗が見つかりました。", "") of me
    
try
      set aNum to a as integer
      
set aTotal to aTotal + aNum
    on error
      log {"Error in ", j}
    end try
  end if
end repeat

return aTotal
–> 622 (2019/3/3)

(*
set countryList to {"HKG", "TWN"}–香港と台湾の店舗のコード
set baseURL2 to "https://gundam-kizuna.jp/locations/list"
*)

–パラメータつきURLを作成
on retURLwithParams(aBaseURL as string, aRec as record)
  set aDic to NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
set bLen to length of aValList
  
if aLen is not equal to bLen then return false
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to (contents of item i of aKeyList) as string
    
set aVal to (contents of item i of aValList) as string
    
set the end of qList to (NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

–テキストをHTMLとして解釈しプレーンテキスト化
on HTMLDecode(HTMLString)
  set theString to NSString’s stringWithString:HTMLString
  
set theData to theString’s dataUsingEncoding:(current application’s NSUnicodeStringEncoding)
  
set attStr to NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value)
  
set aStr to attStr’s |string|()
  
return aStr as string
end HTMLDecode

–文字置換
on repChar(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

★Click Here to Open This Script 

Posted in list regexp Text URL | Tagged 10.11savvy 10.12savvy 10.13savvy NSArray NSAttributedString NSMutableDictionary NSPredicate NSString NSUnicodeStringEncoding NSURLComponents NSURLQueryItem | Leave a comment

アラートダイアログ上にBrowserを表示

Posted on 3月 2, 2019 by Takaaki Naganoya

アラートダイアログ上にNSBrowserを表示するAppleScriptの試作品です。

# 本Scriptは実験用であって実用性はありません(^ー^;;

Finderに使われていることで認知度は高いものの、使いどころが難しいうえにサンプル・コードも多くないので登場頻度が少ないNSBrowserです。(Mac OS Xの前身である)NEXTSTEPのFile Managerがこんな感じのUIでした。

(個人的に)敷居の高かったNSBrowserも、いろいろ試してみたらいい感じになってきました。現状では単に2次元配列っぽいものにアクセスしているだけですが、ツリー型の構造データを与えるようにするとか、配列から徐々にデータ抽出する(大分類→中分類→小分類)ようにすれば、だいたい期待どおりの動作になるはずです。

NSBrowserも、使いたいときに調べると情報が見つからずに困りますが、用のないときなら調べておくといろいろ大丈夫そうです。

macOS 10.14上のスクリプトエディタ/Script Debugger上で動くようにハンドラをメインスレッド上で明示的に動作させるように変更しましたが、10.14上でだけ独特な挙動があるため要注意です。ただ、テストプログラムなのでそんなものだろうかと。

AppleScript名:アラートダイアログ上にBrowserを表示 v1.1.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 scripting additions

property NSAlert : a reference to current application’s NSAlert
property NSColor : a reference to current application’s NSColor
property NSBrowser : a reference to current application’s NSBrowser
property NSScrollView : a reference to current application’s NSScrollView
property NSMutableArray : a reference to current application’s NSMutableArray
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSAlertSecondButtonReturn : a reference to current application’s NSAlertSecondButtonReturn

property theResult : 0
property returnCode : 0
property theDataSource : {}

set paramObj to {myMessage:"Choose a data", mySubMessage:"Choose an appropriate data from tree structured data list", aTableList:{{"1", "1.1", "1.1.1"}, {"2", "2.1", "2.1.1"}, {"3", "3.1", "3.1.1"}, {"4", "4.1", "4.1.1"}}}
–set aRes to my chooseItemByBrowser:paramObj
my performSelectorOnMainThread:"chooseItemByBrowser:" withObject:(paramObj) waitUntilDone:true

on chooseItemByBrowser:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set aTList to (aTableList of paramObj) as list
  
  
— make browser view with scroll view
  
set aScrollWithTable to makeBrowserView(aTList, 500, 200) of me
  
  
— 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:aScrollWithTable
  end tell
  
  
— 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 error number -128
end chooseItemByBrowser:

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

on makeBrowserView(aList as list, aWidth as number, aHeight as number)
  set (my theDataSource) to NSMutableArray’s arrayWithArray:aList
  
  
set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
set aView to NSBrowser’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
  
aView’s setDelegate:(me)
  
aView’s setMinColumnWidth:120
  
aView’s setSeparatesColumns:true
  
–aView’s setMaxVisibleColumns:2
  
aView’s setAutohidesScroller:true
  
–aView’s setBackgroundColor:(NSColor’s grayColor())
  
aView’s setReusesColumns:true
  
  
aScroll’s setDocumentView:aView
  
aView’s enclosingScrollView()’s setHasHorizontalScroller:true
  
aView’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
return aScroll
end makeBrowserView

–NSBrowser Event Handlers
on browser:aView numberOfRowsInColumn:aColumn
  set aCount to my theDataSource’s |count|()
  
return aCount
end browser:numberOfRowsInColumn:

on browser:aView willDisplayCell:(aCell) atRow:rowIndex column:colIndex
  set selRow to (my theDataSource)’s objectAtIndex:(rowIndex)
  
set selCol to selRow’s objectAtIndex:(colIndex)
  
aCell’s setTitle:(selCol as string)
  
  
set colMax to ((my theDataSource)’s objectAtIndex:0)’s |count|()
  
log {"colMax, colIndex", colMax, colIndex}
  
set exMax to (colIndex < colMax – 1) as boolean
  
aCell’s setLeaf:(not exMax)
end browser:willDisplayCell:atRow:column:

★Click Here to Open This Script 

Posted in GUI list | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSAlert NSAlertSecondButtonReturn NSBrowser NSColor NSMutableArray NSRunningApplication NSScrollView | Leave a comment

SFPSDWriterでレイヤー分けしたPhotoshopデータを作成

Posted on 2月 27, 2019 by Takaaki Naganoya

オープンソースのSFPSDWriterを呼び出して、レイヤー分けしたPhotoshopデータを作成するAppleScriptです。

SFPSDWriterは基本的にCGImageを与えてPSDを作成するプログラムですが、AppleScriptからはCGImageを作れません。

SFPSDWriterのソースコードを読んでみたら、コメントにNSImageからCGImageへの変換方法が書かれていたので、NSImageを受け付けるメソッドを新規追加してAppleScriptから呼べるようにしてみました。

–> Download SFPSDWriter.framework(To ~/Library/Frameworks)

PSDを読み書きするObjective-Cのプログラムは割とすぐに見つかるのですが、AppleScriptから呼べない(CGImageをパラメータにするため)ものが多く、どれか1つぐらいは使えてほしいと思っていました。

SFPSDWriterの機能はすべて試せていませんが、名前どおり本ソフトはPSDファイルを書き込む専用であって、読むためのものではありません。これ以外にPSDファイルを読み込んでオブジェクトを取り出せる(ように改造できる)ものを探しておきたいところです。Photoshopを用いないでPSDデータにアクセスできることには価値があります。Mac App Storeに出すソフトウェアだととくにそうです。

Photoshopを操作してほぼ同様の処理を行うものを書いてみましたが、SFPSDWriterを用いて処理したほうがシンプルなことに驚きます。

AppleScript名:SFPSDWriterのじっけん
— Created 2016-03-12 by Takaaki Naganoya
— Modified 2019-02-27 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "SFPSDWriter" –https://github.com/shinyfrog/SFPSDWriter

property |NSURL| : a reference to current application’s |NSURL|
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property SFPSDWriter : a reference to current application’s SFPSDWriter
property NSWorkspace : a reference to current application’s NSWorkspace

property SFPSDResolutionUnitPPI : 1
property SFPSDResolutionUnitPPC : 2

set layer1 to NSImage’s alloc()’s initWithContentsOfURL:(|NSURL|’s fileURLWithPath:(POSIX path of (choose file of type {"public.png"})))
set layer2 to NSImage’s alloc()’s initWithContentsOfURL:(|NSURL|’s fileURLWithPath:(POSIX path of (choose file of type {"public.png"})))

set anWriter to SFPSDWriter’s alloc()’s initWithDocumentSize:(current application’s CGSizeMake(1024, 400)) andResolution:72.0 andResolutionUnit:(SFPSDResolutionUnitPPI)

anWriter’s addLayerWithNSImage:layer1 andName:"Layer_01" andOpacity:1.0 andOffset:{x:0.0, y:0.0}
anWriter’s addLayerWithNSImage:layer2 andName:"Layer_02" andOpacity:1.0 andOffset:{x:50.0, y:50.0}

set aPSD to anWriter’s createPSDData()

set pathString to NSString’s stringWithString:"~/Desktop/output.psd"
set newPath to pathString’s stringByExpandingTildeInPath()

aPSD’s writeToFile:newPath atomically:true

–Open with Adobe Photoshop
NSWorkspace’s sharedWorkspace()’s openFile:newPath

★Click Here to Open This Script 

AppleScript名:PhotoshopでSFPSDWriterと同じような処理を行う
— Created 2019-02-27 by Takaaki Naganoya
— 2019 Piyomaru Software
set layer1 to (choose file of type {"public.png"}) as string
set layer2 to (choose file of type {"public.png"}) as string

tell application id "com.adobe.photoshop"
  activate
  
close every document saving no
  
  
set aDoc to make new document with properties {height:900, width:900, resolution:72.0, mode:RGB, bits per channel:eight, initial fill:transparent, color profile kind:none, pixel aspect ratio:1.0}
  
tell aDoc
    set origLayer to (first item of (name of every art layer)) as string
  end tell
  
  
open file layer1
  
set bDoc to a reference to current document
  
tell bDoc
    select all
    
copy
    
close without saving
  end tell
  
  
tell aDoc
    set artLayerRef to make new art layer with properties {name:"Layer_01"}
    
set current layer to artLayerRef
    
tell artLayerRef
      paste
    end tell
  end tell
  
  
open file layer2
  
set cDoc to a reference to current document
  
tell cDoc
    select all
    
copy
    
close without saving
  end tell
  
  
tell aDoc
    set artLayerRef to make new art layer with properties {name:"Layer_02"}
    
set current layer to artLayerRef
    
tell artLayerRef
      paste
    end tell
    
    
delete art layer origLayer
  end tell
end tell

★Click Here to Open This Script 

Posted in file Image | Tagged 10.11savvy 10.12savvy 10.13savvy Photoshop | 1 Comment

クリップボード内のRTFをStyled Stringとして解釈して行ごとに分割して画面描画サイズ幅で昇順ソートして再結合

Posted on 2月 27, 2019 by Takaaki Naganoya

クリップボードにコピーしておいた書式つきテキストを、行ごとに画面上の描画サイズで昇順ソートし、再結合してクリップボードに転送するAppleScriptです。

ほぼ毎日使っているAppleScriptの改良版です。

Cocoaの機能を呼び出すと、AppleScript冒頭にproperty宣言文を書くことになりますが(書かなくてもいいんですが)、この宣言文を「プロポーショナルフォントを考慮しつつ実際の画面上の描画サイズを考慮」して短いものから長いものにソートして掲載しています。

ただし、従来のバージョンでは実際のフォントではなく一律に指定フォントで描画したさいの描画幅でソートしていたため、ごくまれに掲載時のリストが短い順になっていませんでした(世界中で誰も気にしてねえよ! ^ー^;)。

これを、実際の指定フォントやサイズを考慮した行単位のスタイル付きテキストに分解し、描画幅でソートしたあとにスタイル付きテキストを再合成するようにしました。

従来のOld Style AppleScriptではスタイル付きテキストの操作は一切できませんでしたが、Cocoaの機能を活用することでこのように自由度の高い処理ができるようになった、という好例です。

なお、本ルーチンによって厳密に描画幅でソートしても、Web掲載時にはWebブラウザのレンダリングのルール(&ユーザー側で指定しているWebブラウザのフォント)が適用されるため、macOS上のアプリケーション上での見た目と若干違いが出るという残念な結果も出ています。

macOS標準装備のスクリプトメニューに入れて呼び出しています。

AppleScript名:クリップボード内のRTFをStyled Stringとして解釈して行ごとに分割して画面描画サイズ幅で昇順ソートして再結合
— Created 2017-04-24 by Shane Stanley
— Modified 2019-02-27 by Takaaki Naganoya
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property NSFont : a reference to current application’s NSFont
property NSColor : a reference to current application’s NSColor
property NSPasteboard : a reference to current application’s NSPasteboard
property NSAttributedString : a reference to current application’s NSAttributedString
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName

–クリップボードの内容をNSAttributedStringに
set anAttr to my getClipboardASStyledText()
if anAttr = missing value then return

–Split Attributed Strings into lines
set attrList to splitAttributedStringByLines(anAttr) of me

–Sort by Width in ascending
set sortedList to shellSortListAscending(attrList, 1) of me

–Append Attributed Strings
set mutableReturn to NSMutableAttributedString’s alloc()’s initWithString:(return)
set outStr to NSMutableAttributedString’s alloc()’s initWithString:""

repeat with i in sortedList
  copy i to {tmpWidth, tmpAttr}
  (
outStr’s appendAttributedString:tmpAttr)
  
  
set tmpStr to tmpAttr’s |string|() as string
  
if (tmpStr does not end with string id 10) and (tmpStr does not end with string id 13) then
    (outStr’s appendAttributedString:mutableReturn)
  end if
end repeat

— Set Styled String to Clipboard
set theArray to {outStr}
restoreClipboard(theArray) of me

–Clipboardにデータを設定する
on restoreClipboard(theArray as list)
  set thePasteboard to NSPasteboard’s generalPasteboard()
  
thePasteboard’s clearContents()
  
thePasteboard’s writeObjects:theArray
end restoreClipboard

— クリップボードの内容をNSAttributedStringとして取り出して返す
on getClipboardASStyledText()
  try
    set theNSPasteboard to NSPasteboard’s generalPasteboard()
    
set theAttributedStringNSArray to theNSPasteboard’s readObjectsForClasses:({NSAttributedString}) options:(missing value)
    
set theNSAttributedString to theAttributedStringNSArray’s objectAtIndex:0
    
return theNSAttributedString
  on error
    return missing value
  end try
end getClipboardASStyledText

on splitAttributedStringByLines(theStyledText)
  set outList to {}
  
set outAttr to NSMutableAttributedString’s alloc()’s initWithString:""
  
  
set thePureString to theStyledText’s |string|() –pure string from theStyledText
  
set theLength to theStyledText’s |length|()
  
set startIndex to 0
  
  
repeat until (startIndex = theLength)
    set {theAtts, theRange} to theStyledText’s attributesAtIndex:startIndex longestEffectiveRange:(reference) inRange:{startIndex, theLength – startIndex}
    
set aText to (thePureString’s substringWithRange:theRange) as string –String
    
    
set aColor to (theAtts’s valueForKeyPath:"NSColor") –Color
    
set aFont to (theAtts’s valueForKeyPath:"NSFont") –Font
    
if aFont is equal to missing value then error "Not font name and size are specified" –Font Name error
    
set aDFontName to aFont’s displayName() –Font Name
    
set aDFontSize to aFont’s pointSize() –Font Size
    
    
set tmpAttrStr to generateAttributedString(aText, aDFontName, aDFontSize, aColor) of me
    
outAttr’s appendAttributedString:tmpAttrStr
    
    
if (aText contains return) or (aText contains string id 10) then –CR or LF
      set tmpSize to outAttr’s |size|()
      
set tmpWidth to retWidthFromSize(tmpSize) of me
      
      
set the end of outList to {tmpWidth, outAttr}
      
set outAttr to NSMutableAttributedString’s alloc()’s initWithString:""
    end if
    
    
set startIndex to current application’s NSMaxRange(theRange)
  end repeat
  
  
set tmpSize to outAttr’s |size|()
  
set tmpWidth to retWidthFromSize(tmpSize) of me
  
set the end of outList to {tmpWidth, outAttr}
  
  
return outList
end splitAttributedStringByLines

on retWidthFromSize(tmpSize)
  if class of tmpSize = record then
    –macOS 10.10, 10.11, 10.12
    
set tmpWidth to width of tmpSize
  else
    –macOS 10.13, 10.14
    
set tmpWidth to first item of tmpSize
  end if
  
return tmpWidth
end retWidthFromSize

on generateAttributedString(aStr, aFontPSName, aFontSize, aColor)
  set tmpAttr to NSMutableAttributedString’s alloc()’s initWithString:aStr
  
set aRange to current application’s NSMakeRange(0, tmpAttr’s |length|())
  
set aVal1 to NSFont’s fontWithName:aFontPSName |size|:aFontSize
  
tmpAttr’s beginEditing()
  
tmpAttr’s addAttribute:(NSFontAttributeName) value:aVal1 range:aRange
  
tmpAttr’s addAttribute:(NSForegroundColorAttributeName) value:aColor range:aRange
  
tmpAttr’s endEditing()
  
return tmpAttr
end generateAttributedString

–入れ子のリストを昇順ソート
on shellSortListAscending(a, keyItem)
  return sort2DList(a, keyItem, {true}) of me
end shellSortListAscending

–入れ子のリストを降順ソート
on shellSortListDecending(a, keyItem)
  return sort2DList(a, keyItem, {false}) of me
end shellSortListDecending

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  load framework
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j – 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to "compare:"
  end repeat
  
  
–Sort
  
set resList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
return resList
end sort2DList

★Click Here to Open This Script 

Posted in Clipboard Color list RTF Sort Text | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSAttributedString NSColor NSFont NSFontAttributeName NSForegroundColorAttributeName NSMutableAttributedString NSPasteboard | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • AppleScriptによる並列処理
  • Cocoa Scripting Course 続刊計画
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • Keynote、Pages、Numbers Ver.14.0が登場
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • AppleScript入門① AppleScriptってなんだろう?
  • Numbersで最前面の書類のすべてのシート上の表の行数を合計
  • macOS 14で変更になったOSバージョン取得APIの返り値

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (129) CotEditor (66) Finder (51) iTunes (19) Keynote (116) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • 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
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • 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)
  • 未分類

アーカイブ

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

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

メタ情報

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

Forum Posts

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

メタ情報

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