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.15savvy

16bitカラー値から明度を取得 v1.1

Posted on 1月 20, 2022 by Takaaki Naganoya

choose colorコマンドやiWorkアプリケーションが返してくる16ビットのカラー値({R,G,B})から明度情報を取得するAppleScriptです。0から1までの値を返し、0に近づくほど暗く、1に近づくほど明るい色であることを表現しています。

色情報同士を比較して、明るい箇所に指定している色データと暗い箇所に指定している色データを、RGB値から自動認識するような処理に使っています。

また、ファイル名から「章」(Chapter)の番号を検出して、実際のツメ(Index)の選択部分を暗い色で塗り直す処理も行なっています。

–> Watch Demo Movie

Pages書類の上に作成したツメ(辞書などの端に印刷されているAからZのインデックス)に対して、ツメのオブジェクトを(座標や形状から)自動認識したあとに、色の塗り分け情報を本ルーチンを用いて自動認識。濃い色を現在の選択箇所の色、明るい(淡い)色を通常の箇所の色情報として扱い、あらかじめツメに指定していた色情報を用いて実際の章構成を反映させてツメのオブジェクト(表)を再構成する処理に必須のものです。

明度情報の取得には、グレースケールに変換してグレー度(whiteComponent)を明度とみなして計算しています。直感的にこちらのほうが、まっとうな処理(HSB色空間の色に変換してからBrightnessを取得)よりもしっくり来る計算結果だったので採用しています。

おまけで、2つの色情報のうちどちらが暗いか、結果をインデックス値で返す処理ルーチンをつけています。これも、選択色と非選択色の自動識別のために必要なものです。

AppleScript名:16bitカラー値から明度を取得 v1.1.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/01/20
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

set a1Col to {26148, 65535, 58650}
set a1Col to choose color default color a1Col
set w1Col to calcBrightnessFrom16bitColorList(a1Col) of colorBrightnessKit
–> 0.900838315487

set a2Col to {2966, 23133, 21203}
set a2Col to choose color default color a2Col
set w2Col to calcBrightnessFrom16bitColorList(a2Col) of colorBrightnessKit
–> 0.37399661541–0に近いのでこちらのほうが暗い

set cIndRes to calcColorPairsDarkerCol(a1Col, a2Col) of colorBrightnessKit
–> {2, 1}–暗い順にインデックス値を返す

–色情報から明度を計算するKit。0に近い値が暗い。CMYKやグレースケール値は対象外。RGBのみ
script colorBrightnessKit
  
  
use AppleScript
  
use framework "Foundation"
  
use framework "AppKit"
  
use scripting additions
  
property parent : AppleScript
  
  
property NSColor : a reference to current application’s NSColor
  
property NSString : a reference to current application’s NSString
  
property NSAttributedString : a reference to current application’s NSAttributedString
  
property NSUTF16StringEncoding : a reference to current application’s NSUTF16StringEncoding
  
property NSDeviceWhiteColorSpace : a reference to current application’s NSDeviceWhiteColorSpace
  
  
  
–2つの16ビットカラー値でそれぞれ明度を計算し、暗いものから順にインデックス値を返す
  
on calcColorPairsDarkerCol(aCol as list, bCol as list)
    set b1 to calcBrightnessFrom16bitColorList(aCol) of me
    
set b2 to calcBrightnessFrom16bitColorList(bCol) of me
    
if b1 ≥ b2 then
      return {2, 1} –bColのほうが暗い
    else
      return {1, 2} –aColのほうが暗い
    end if
  end calcColorPairsDarkerCol
  
  
  
–16ビットカラー値から明度を計算
  
on calcBrightnessFrom16bitColorList(colList as list)
    copy colList to {rVal, gVal, bVal}
    
–NSColorを作成
    
set aCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65535, 65535) of me
    
— グレースケール化
    
set gCol to aCol’s colorUsingColorSpaceName:(NSDeviceWhiteColorSpace)
    
set wComp to gCol’s whiteComponent() –whiteComponentを取得することで擬似的に明度情報を取得  
    
return wComp
  end calcBrightnessFrom16bitColorList
  
  
  
–HTMLカラー値あから明度を計算
  
on calcBrightnessFromHTMLColorCodeStr(aStr as string)
    set {rVal, gVal, bVal} to rgbHex2nunList(aStr) of me
    
–NSColorを作成
    
set aCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 255, 255) of me
    
— グレースケール化
    
set gCol to aCol’s colorUsingColorSpaceName:(NSDeviceWhiteColorSpace)
    
set wComp to gCol’s whiteComponent() –whiteComponentを取得することで擬似的に明度情報を取得  
    
return wComp
  end calcBrightnessFromHTMLColorCodeStr
  
  
  
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
  
  
  
  
on decodeCharacterReference(aStr)
    set anNSString to NSString’s stringWithString:aStr
    
set theData to anNSString’s dataUsingEncoding:(NSUTF16StringEncoding)
    
set styledString to NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value)
    
set plainText to (styledString’s |string|()) as string
    
return plainText
  end decodeCharacterReference
  
  
  
  
–HTMLコードのRGB 16進数コードを数値リストに変換
  
on rgbHex2nunList(aHexStr)
    –エラーチェック
    
if aHexStr does not start with "#" then return false
    
if length of aHexStr is not equal to 7 then return false
    
set bHex to text 2 thru -1 of aHexStr
    
    
set {rStr, gStr, bStr} to {text 1 thru 2 of bHex, text 3 thru 4 of bHex, text 5 thru 6 of bHex}
    
    
set bList to {}
    
repeat with i in {rStr, gStr, bStr}
      set j to contents of i
      
set the end of bList to aHexStrToNum(j) of me
    end repeat
    
    
return bList
  end rgbHex2nunList
  
  
  
–16進数文字列を10進数数値に変換する
  
on aHexStrToNum(hexStr)
    set hStr to "0123456789ABCDEF"
    
    
set aNum to 0
    
set aLen to length of hexStr
    
    
repeat with i from aLen to 1 by -1
      
      
set aCon to contents of character i of hexStr
      
using terms from scripting additions
        set aVal to (offset of aCon in hStr) – 1
      end using terms from
      
set aNum to aNum + aVal * (16 ^ (aLen – i))
      
    end repeat
    
    
return aNum as integer
  end aHexStrToNum
end script

★Click Here to Open This Script 

Posted in Color list | Tagged 10.15savvy 11.0savvy 12.0savvy NSColor | Leave a comment

SafariでブックマークされたURL一覧を取得

Posted on 1月 7, 2022 by Takaaki Naganoya

Safariのブックマークに登録されたURL一覧を取得するAppleScriptです。

SafariのAppleScript対応機能に、ブックマーク操作系のものは存在していません。そのため、Bookmarks.plistを直接読み込んでデータの絞り込みを行なっています。

Bookmarks.plistを実際に読んでみると、ツリー(フォルダ)とリーフ(個別のブックマーク)があって、本Scriptではリーフ部分しか読んでいないのですが、とりあえず試作レベルということで。

読むのはいいんですが、追加が大変そうなので(BookmarkのIDとか外部で勝手に追加できるものなんだろうか。UUIDっぽいけど)ブックマーク登録は野蛮にGUI Scripting経由でメニューを操作することになるのでしょう。

AppleScript名:SafariのBookmarks.plistから登録URLを取得
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/01/07
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7"
use framework "Foundation"
use scripting additions

script spdB
  property urlList : {}
end script

set (urlList of spdB) to getBookmarkedURLs() of me
set (urlList of spdB) to cleanUp1DList((urlList of spdB), missing value) of me –Sweep missing value items

–Read Bookmarked URLs
on getBookmarkedURLs()
  set newPath to "~/Library/Safari/Bookmarks.plist"
  
set aRec to my readPlistAt:newPath
  
return ((aRec’s Children)’s valueForKey:"URLString") as list
end getBookmarkedURLs

–Read Plist
on readPlistAt:thePath
  set thePath to current application’s NSString’s stringWithString:thePath
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
return theDict
end readPlistAt:

–1D Listのスイープ
on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    end if
  end repeat
  
return bList
end cleanUp1DList

★Click Here to Open This Script 

Posted in Record | Tagged 10.15savvy 11.0savvy 12.0savvy Safari | Leave a comment

Intel MacとApple Silicon Macの速度差〜画像処理

Posted on 1月 2, 2022 by Takaaki Naganoya

macOS 11から12に移行して、なぜかリリースが近づくにつれて細かいバグや巨大なバグが顕在化し、まだ手放しでおすすめできる状況にないのが心苦しいところですが、AppleScriptからのCocoa呼び出しについてはIntel Mac/Apple Silicon Macでも速度が向上。とくに、Apple Silicon Macでの速度向上が顕著です。

で、特定の処理(巨大なlistやrecord in list同士の検索)でM1 Mac miniがiMac Proの2.5倍ぐらい速いといったベンチマークは出しているわけですが、いかにもApple Silicon Macで処理が速そうな画像処理のベンチマークを実施してみました。

Intel Mac mini 2014が手元からなくなってしまったので、以前のメインマシンであったMacBook Pro Retina 2012(MacBookPro10,1)と比較してみました。

ベンチマーク内容は、指定の画像が空白かどうかをチェックするというものです。フルHD画像でも4K画像でも8K画像でも、1ピクセルでも白くない点があれば検出できるという処理内容。外部のGPUImage FrameworkやPhotoshopのヒストグラム処理を呼び出すよりもAppleScriptだけで処理した方が速いというものです(8K画像だとPhotoshopの方が速いかも)。


▲画像解像度の変化と処理時間の相関。グラフは数値が小さいほど高速。巨大な画像になると処理速度差が大きくなる傾向にある?

だいたい、MacBook Pro Retina 2012と比べて3〜4倍ぐらいM1 Mac miniの方が高速です。MacBook Pro Retina 2012は2017年のMacBook Pro 13インチといい勝負ぐらいの速度が出ており、古い割にはごく最近まで使えていました。

そこから3〜4倍高速ということで、M1 Mac miniはコストの割にはパワフルです。ファンレスのMacBook Air M1でも同程度の速度が出るはずです。

一方で、M1 Pro/M1 Max搭載のMacBook ProでM1機よりも大幅に高速なのかと言われると……このぐらいの静止画の処理程度だとごくわずかな差しかつかないはずです。下手をすると、速度差がないかもしれません。

→ Download blank_image_detection_benchmark.zip (including script libraries)

–> Download test Data


▲バーが短いほど高速。M1がM1 Maxの2倍高速という結果が出てしまった。M1 Max MBPは2014年のIntel Mac miniより少し速いだけのマシンという結果に。2018年のIntel Mac miniとの比較だとM1 Max MBPよりもIntel Mac miniのほうが速そう

M1、M1 Max、M1 Ultra(最上位機種)で処理時間を比較した結果。予想どおりM1がM1 MaxやM1 Ultraよりも2倍以上高速。もはや8K画像ぐらいは、大きなデータにならないというべきなのか。

AppleScript名:画像の空白判定 v4_bench_時間計測.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/01/01
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7"
use framework "Foundation"
use framework "AppKit"
use scripting additions
use mdLib : script "Metadata Lib" version "2.0.0"
use easyTable : script "display table by list"

set aFol to choose folder

set aResList to perform search in folders {aFol} predicate string "kMDItemContentTypeTree CONTAINS %@" search arguments {"public.image"}

set aList to {}
repeat with i in aResList
  set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate()
  
  
set iRes to checkImageIsWhite(i) of blankImageKit of me
  
  
set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate()
  
set c1Dat to b1Dat – a1Dat
  
  
set aName to (current application’s NSString’s stringWithString:i)’s lastPathComponent() as string
  
  
if iRes = true then
    set jRes to "White"
  else
    set jRes to "Black"
  end if
  
  
–log {aName, jRes}
  
  
set the end of aList to {aName, jRes, c1Dat}
end repeat

set fLabels to {"File name", "Result", "Estimate(Seconds)"}
set aRes to (display table by list aList main message "Blank Image check" size {800, 600} labels fLabels)
return aList

script blankImageKit
  use AppleScript version "2.7" — High Sierra (10.13) or later
  
use framework "Foundation"
  
use framework "AppKit"
  
use scripting additions
  
property parent : AppleScript
  
  
property NSData : a reference to current application’s NSData
  
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
  
  
  
–Compare Original Data and
  
on checkImageIsWhite(aFile)
    set aPOSIXpath to POSIX path of aFile
    
set aURL to |NSURL|’s fileURLWithPath:(aPOSIXpath)
    
    
set aNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL)
    
set bNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL)
    
    
set fillColor1 to NSColor’s clearColor()
    
set blankNSImage1 to drawImageWithFilledColor(aNSImage, fillColor1) of me
    
    
set fillColor2 to makeNSColorFromRGBAval(65535, 65535, 65535, 65535, 65535) of me –white
    
set blankNSImage2 to drawImageWithFilledColor(bNSImage, fillColor2) of me
    
    
set aTiff to blankNSImage1’s TIFFRepresentation()
    
set bTiff to blankNSImage2’s TIFFRepresentation()
    
    
set chkWhite to (aTiff’s isEqualToData:bTiff) 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
  
end script

★Click Here to Open This Script 

Posted in Image | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy NSBezierPath NSBitmapImageRep NSColor NSData NSDate NSImage NSURL | Leave a comment

CotEditorで選択範囲の行頭にある数字をリナンバーする v1

Posted on 1月 2, 2022 by Takaaki Naganoya

CotEditorでオープン中の最前面の書類の選択範囲のテキストを行ごとにチェックし、行頭に存在する数字を、それらのうちの最小値を検出しつつ、指定のステップ数でリナンバー(番号振り直し)を行うAppleScriptです。

さまざまな項目の整理のために、テキストの先頭に仮想的なノンブル(ページ番号的なもの、ソート順を指定するための番号)を振っています。この番号ではじまるテキストをもとにFinder上でフォルダ整理をしており、項目の前後関係を入れ替えると…前後関係を明示するために、番号を振り直す必要が出てくるわけです。

# Numbers上やExcel上で行うと、余計な書体スタイルなどが入ってきて邪魔なので、テキストエディタ上で行うことが多いです

その番号の振り直しを行うAppleScriptです。macOS 12.2beta+CotEditor v4.0.9で動作確認を行っています。


▲選択範囲内の行頭の番号を振り直す。ちなみに、表示例はボツになった本の企画

本来は「行頭にある数字」を指示する必要があるものの、まだうまく機能していません。桁数でなんとなく判別しているだけです。


▲フォント作者の方々の合意を得られなさそうで流れた企画「同人フォントソムリエ」

AppleScript名:選択範囲の行頭にある数字をリナンバーする v1.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/01/02
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

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

property NSMutableArray : a reference to current application’s NSMutableArray

property myNumStep : 1000

tell application "CotEditor"
  tell front document
    set aSel to contents of selection
    
set aSelList to paragraphs of aSel
  end tell
end tell

–範囲指定がない場合
if length of aSelList = 0 then
  display dialog "Error: No Selection" buttons {"OK"} default button 1 with icon 2
  
return
end if

–過大な範囲指定チェック
if length of aSelList > 1000 then
  set bRes to button returned of (display dialog "選択範囲が1000行を超えています。処理に時間がかかることが予想されますが、実行しますか?")
end if

–行頭の数字部分のみ取得して、数字の最小値を取得する
set topNumList to getNumbersAtLineBegennings(aSelList) of me
set minNum to calcIntMinAsStr(topNumList) of me

–行頭部分の数字部分のみリナンバー
set bRes to renumberNumsAtLineBegennings(aSelList, minNum as integer, myNumStep) of me

–1D Arrayを改行コードをデリミタに指定しつつテキスト化
set outStr to retDelimedText(bRes, return) of me

tell application "CotEditor"
  tell front document
    set contents of selection to outStr
  end tell
end tell

–行頭に入っている数字の文字のみリナンバー
–実際には行頭判定はまだ行えていない。行頭の番号と文の途中に出てくる数字の区別は「桁数」でのみ行っている
on renumberNumsAtLineBegennings(aSelList as list, firstNum as string, stepNum as number)
  set aDigit to length of firstNum –桁数
  
copy firstNum to aCount
  
  
set aRes to {}
  
  
repeat with i in aSelList
    set j to (contents of i) as string
    
set nRes to (getNumberCharsOnlyAtBegening(j) of me) as string
    
    
if nRes is not equal to "" then –InputとOutputが違った
      set n1Res to removeNumCharsOnly(j, aDigit) of me
      
set tmpNumStr to zeroPadding(aCount, aDigit) of me
      
set n1Res to tmpNumStr & n1Res
      
set aCount to aCount + stepNum
      
set the end of aRes to n1Res
    else
      set the end of aRes to j
    end if
  end repeat
  
  
return aRes
end renumberNumsAtLineBegennings

–1D Listの最小値を文字列で返す
on calcIntMinAsStr(aList as list)
  set nArray to (NSMutableArray’s arrayWithArray:aList)
  
set maxRes to (nArray’s valueForKeyPath:"@min.self")’s intValue()
  
return maxRes as string
end calcIntMinAsStr

–行頭に入っている数字の文字のみ抽出
on getNumbersAtLineBegennings(aSelList as list)
  set aRes to {}
  
repeat with i in aSelList
    set j to contents of i
    
set nRes to getNumberCharsOnlyAtBegening(j) of me
    
if nRes is not equal to "" then
      set the end of aRes to nRes
    end if
  end repeat
  
  
return aRes
end getNumbersAtLineBegennings

–数字のみ返す
on getNumberCharsOnlyAtBegening(aStr as string)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9]{4,}" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()}
  
return anNSString as text
end getNumberCharsOnlyAtBegening

–数字のみ削除して返す
on removeNumCharsOnly(aStr as string, aDigit)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set anNSString to anNSString’s stringByReplacingOccurrencesOfString:("[0-9]{" & (aDigit as string) & ",}") withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()}
  
return anNSString as text
end removeNumCharsOnly

–指定桁数で指定の数にゼロパディングして文字列を返す
on zeroPadding(aNum as number, aDigit as number)
  set aText to "00000000000" & (aNum as text)
  
set aLen to length of aText
  
set aRes to text (aLen – aDigit + 1) thru -1 of aText
  
return aRes
end zeroPadding

–1D Listを指定デリミタをはさみつつテキストに
on retDelimedText(aList as list, aDelim as string)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retDelimedText

★Click Here to Open This Script 

Posted in regexp Text | Tagged 10.15savvy 11.0savvy 12.0savvy CotEditor | Leave a comment

NSAttributedStringにtab指定つきparagraph styleを指定

Posted on 12月 31, 2021 by Takaaki Naganoya

書式付きテキスト(NSAttributedString)にパラグラフスタイルを指定して、tab指定を行い、tab展開を行うテストです。

Keynote/Pagesなどのテキストアイテムで、中にタブを含むスタイル付きテキストだった場合に、スタイル付きテキストの幅を計算したら大きな誤差が生じていたため、タブを展開して近似するサイズを取得できないかと考え、試してみたものです。

いまひとつ、まだしっくり来ていません。

AppleScript名:Attributed Stringにparagraph styleを指定してtab指定.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/27
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

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

set aStr to "Piyomaru Software  100  200  10  20"
set anAssrStr to makeRTFfromParameters(aStr, 3.0, 16, "HelveticaNeue") of me

set aTabStops to {90, 400, 500, 600, 700}
set aStyle to makeParagraphStyle(aTabStops) of me

anAssrStr’s addAttribute:(current application’s NSParagraphStyleAttributeName) value:aStyle range:(current application’s NSMakeRange(0, (anAssrStr’s |length|())))
return anAssrStr

–Make Paragraph Style
on makeParagraphStyle(tList as list)
  set tabList to {}
  
repeat with i in tList
    set the end of tabList to (current application’s NSTextTab’s alloc()’s initWithTextAlignment:(current application’s NSTextAlignmentLeft) location:i options:(missing value))
  end repeat
  
  
set aParagraphStyle to (current application’s NSParagraphStyle’s defaultParagraphStyle())’s mutableCopy()
  
aParagraphStyle’s setTabStops:tabList
  
return aParagraphStyle
end makeParagraphStyle

–Make Attributed String
on makeRTFfromParameters(aStr as string, outlineNum as real, aFontSize as real, aFontName as string)
  set aVal1 to current application’s NSFont’s fontWithName:aFontName |size|:aFontSize
  
set aKey1 to (current application’s NSFontAttributeName)
  
set aVal2 to current application’s NSColor’s redColor()
  
set aKey2 to (current application’s NSForegroundColorAttributeName)
  
set aVal3 to 1.0
  
set akey3 to (current application’s NSKernAttributeName)
  
set aVal4 to 0
  
set akey4 to (current application’s NSUnderlineStyleAttributeName)
  
  
set keyList to {aKey1, aKey2, akey3, akey4}
  
set valList to {aVal1, aVal2, aVal3, aVal4}
  
set attrsDictionary to current application’s NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList
  
  
set attrStr to current application’s NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary
  
return attrStr
end makeRTFfromParameters

★Click Here to Open This Script 

Posted in RTF | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy NSMutableParagraphStyle NSTextTab | Leave a comment

FileMaker PowerPack活用ガイドを発売!

Posted on 12月 28, 2021 by Takaaki Naganoya

既存のAppleScript対応アプリケーションから、Cocoa Scriptingの現在のパワーを利用できるようにした「PowerPack」シリーズ。

第1弾は日本国内外で多くのユーザーに支持されている「CotEditor」用PowerPack。

第2弾がさまざまなプラットフォームへの広がりを見せつつmacOSのコア機能であるAppleScriptへの対応も行っているデータベース製品「FileMaker Pro」用のPowerPack。「FileMaker PowerPack」は、このFileMaker Proの上で動くCocoa AppleScriptサンプル集です。

サンプル集としてAppleScriptが掲載されているだけなく、FileMaker Pro上でそのまま実行でき、サンプルデータをもとに実行して、その結果をFileMaker Pro上に返してきます。

一般的なAppleScriptランタイム環境と異なるFileMaker Pro上のランタイム環境においても動作するようFileMaker Pro向けのチューニングを行ってあります。

「FileMaker PowerPack」は、日本国内のFileMaker系最難関コンテスト「FM-1グランプリ」への応募作品でもあり、業務効率化部門準賞 入賞作品でもあります。

日頃、AppleScriptになじみのない開発者にもこのPowerPackを試し、実際に実行し、そして書き換えて自分のシステムで使うための使い方を詳細に解説する電子書籍「FileMaker PowerPack」活用ガイドを刊行いたしました。

→ 電子書籍販売ページ

PDF 317ページ、FileMaker PowerPackの最新版 v2.1を同梱。

さらに、期間限定おまけコンテンツ「FM-1 GP戦記」(PDF 43ページ)を添付。前代未聞の難関コンテストを勝ち残るためにチーム内で行われた詳細な議論。試作品を没にしてたどりついた、コンテスト攻略の秘策とは?! いまだからこそ明かされる、真剣かつ呑気なやりとりに全米が泣いた、総ページ数350ページを超える1作です。

Posted in PRODUCTS | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy FileMaker Pro | Leave a comment

Pagesのテキストアイテム内の文字の実際の幅でリサイズ

Posted on 12月 25, 2021 by Takaaki Naganoya

Pagesでオープン中の最前面の書類の上に載っている(Pagesの仕様でページ指定はできない)テキストアイテムから、テキスト、フォント、フォントサイズを取得し、その内容でスタイル付きテキストを作成して画面上に描画するサイズを取得し、その幅でテキストアイテムをリサイズするAppleScriptです。

macOS 12.2beta+Pages 11.2で動作確認しています。


▲処理前(内容は妄想にもとづくもので、本物ではありません)


▲処理後(内容は妄想にもとづくもので、本物ではありません)

Pages上のタブの指定までスタイル付きテキスト内で再現できていないためか、中身の幅よりも外側のテキストアイテム(枠)が小さくなってしまうケースが見られますが、もう少し枠のリサイズを余裕を持って(大きめに)指定すると回避できます。

AppleScript名:Pagesのテキストアイテム内の文字の実際の幅でリサイズ
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/25
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSFont : a reference to current application’s NSFont
property NSString : a reference to current application’s NSString
property NSDictionary : a reference to current application’s NSDictionary
property NSFontAttributeName : a reference to current application’s NSFontAttributeName

tell application "Pages"
  tell front document
    set tList to every text item
    
if tList = {} then return
    
    
repeat with i in tList
      set j to contents of i
      
      
tell j
        set tmpFontName to font of its object text
        
set tmpFontSize to size of its object text
        
set tmpText to (its object text as string) & " "
        
        
set aSize to getSizeFromAttributedText(tmpText, tmpFontName, tmpFontSize) of me
        
set aWidth to |width| of aSize
        
set bWidth to aWidth + 10
        
        
set its width to bWidth
      end tell
      
    end repeat
  end tell
end tell

–指定のテキスト、フォント名、フォントサイズでAttributed Stringを組み立ててサイズを取得する
on getSizeFromAttributedText(aText as string, aFont as string, aFSize as real)
  set aFont to NSFont’s fontWithName:aFont |size|:aFSize
  
set aStr to NSString’s stringWithString:aText
  
set aSize to aStr’s sizeWithAttributes:(NSDictionary’s dictionaryWithObjects:{aFont} forKeys:{NSFontAttributeName})
  
return aSize
end getSizeFromAttributedText

★Click Here to Open This Script 

Posted in Object control RTF | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | 1 Comment

Pagesで選択中の表のカラム幅を自動調整

Posted on 12月 18, 2021 by Takaaki Naganoya

Pagesの最前面の書類で選択中の表オブジェクトの2列目以降を均等に自動調整するAppleScriptです。

–> Watch Demo

1列目の列幅は変更しません。表全体を選択するのではなく、どこかのセルを選択しておく必要があります。


▲処理前 カラム幅が不均一。表の1つあるいは複数のセルを選択して処理対象を指定


▲処理後 2列目以降が均一に調整される

AppleScript名:Pagesで選択中の表のカラム幅を自動調整
— Created 2021-12-18 by Takaaki Naganoya
— 2021 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Pages"
  tell curTable
    set cellList to every cell of selection range
  end tell
end using terms from

–2列目から末尾までの列幅を均等に変更する
using terms from application "Pages"
  tell curTable
    set cCount to count every column
    
set cWidth to width of every column
    
    
–表の幅を取得したら、0が返ってきたので、各カラム幅を合計した
    
set aWidth to calcSumFrom1DList(cWidth) of me
    
    
set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1)
    
    
–いったん幅を少なくしておく(予備動作)
    
–途中で幅がページ幅をオーバーすると最終的な表の幅がおかしくなるので、この処理がないとおかしくなる
    
repeat with i from 2 to cCount
      set width of column i to 10
    end repeat
    
    
–カラム幅を変更する
    
repeat with i from 2 to cCount
      set width of column i to aveWidth
    end repeat
  end tell
end using terms from

–1D List中の数値を合計して返す
on calcSumFrom1DList(aList)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
return (anArray’s valueForKeyPath:"@sum.self")’s intValue()
end calcSumFrom1DList

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Pages"
    tell front document
      try
        return (first table whose class of selection range is range)
      on error
        return false –何も選択されてなかった場合
      end try
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

★Click Here to Open This Script 

Posted in list | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a comment

Keynoteの表の選択中のセルに入っている丸つき数字をインクリメント/デクリメント

Posted on 12月 16, 2021 by Takaaki Naganoya

Keynoteの最前面の書類の現在のスライド上にある表の選択中のセルに入っている丸つき数字(①②③…… )をインクリメント(+1)、デクリメント(ー1)するAppleScriptです。

–> Watch Demo Movie

書籍などに掲載する資料で、参照番号をいじくるツールがどうしても必要になって作ったものです。

macOS標準搭載のスクリプトメニューに入れて実行することを前提にしています。丸つき数字は、

①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿

のみを前提にして処理しています。その他の、

❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴
➀➁➂➃➄➅➆➇➈➉
➊➋➌➍➎➏➐➑➒➓
⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾

については無視しています。

Keynote v11.2+macOS 12.1betaで作成&動作確認していますが、Keynoteのバージョンにはとくに(バグのあるバージョンでなれけば)依存している機能はありません。


▲選択範囲のセルに入っている丸つき数字のインクリメント(①②③→②③④)


▲選択範囲のセルに入っている丸つき数字のデクリメント(②③④→①②③)

AppleScript名:選択中の表の指定行・列のマル付き数字のインクリメント(+1).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/16
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of selection range
  end tell
end using terms from

–list中の各アイテムの冒頭に順次丸つき数字を追加する
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      –選択範囲のセルの値を取り出す
      
set aVal to value of i
      
set vList to characters of (aVal as string)
      
      
–1文字ずつに分割してループ
      
set tmpOut to ""
      
repeat with ii in vList
        set jj to contents of ii
        
        
–丸付き数字の検出
        
set tmpR to holdNumberWithSignOnly(jj) of me
        
if tmpR is equal to jj then
          –丸付き数字を数値にデコード
          
set tmpNum to decodeNumFromNumWithSign(tmpR) of me
          
          
–数値をインクリメント
          
if tmpNum ≤ 50 then
            set tmpNum to tmpNum + 1 –インクリメント
          end if
          
          
–数値を丸付き数字にエンコード
          
set tmpR to convNumToNumWithSign(tmpNum) of me
          
set tmpOut to tmpOut & tmpR
        else
          set tmpOut to tmpOut & jj
        end if
      end repeat
      
      
set value of i to tmpOut
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum ≥ 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2(簡略版)
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
  
if numStr1 contains aStr then
    using terms from scripting additions
      set bNum to offset of aStr in numStr1
    end using terms from
    
return bNum
  end if
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

★Click Here to Open This Script 

AppleScript名:選択中の表の指定行・列のマル付き数字のデクリメント(ー1).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/16
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of selection range
  end tell
end using terms from

–list中の各アイテムの冒頭に順次丸つき数字を追加する
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      –選択範囲のセルの値を取り出す
      
set aVal to value of i
      
set vList to characters of (aVal as string)
      
      
–1文字ずつに分割してループ
      
set tmpOut to ""
      
repeat with ii in vList
        set jj to contents of ii
        
        
–丸付き数字の検出
        
set tmpR to holdNumberWithSignOnly(jj) of me
        
if tmpR is equal to jj then
          –丸付き数字を数値にデコード
          
set tmpNum to decodeNumFromNumWithSign(tmpR) of me
          
          
–数値をデクリメント
          
if tmpNum > 1 then
            set tmpNum to tmpNum – 1 –デクリメント
          end if
          
          
–数値を丸付き数字にエンコード
          
set tmpR to convNumToNumWithSign(tmpNum) of me
          
set tmpOut to tmpOut & tmpR
        else
          set tmpOut to tmpOut & jj
        end if
      end repeat
      
      
set value of i to tmpOut
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum ≥ 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2(簡略版)
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
  
if numStr1 contains aStr then
    using terms from scripting additions
      set bNum to offset of aStr in numStr1
    end using terms from
    
return bNum
  end if
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

★Click Here to Open This Script 

Posted in list Text | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

漢字をパーツに分解して部品が共通する文字を検索

Posted on 12月 14, 2021 by Takaaki Naganoya

2つの漢字をパーツごとに分解し、それぞれの分解したパーツからそれを使用している漢字を検索。2つのグループ間で共通して存在している漢字を抽出することで「部品が共通する文字」をリストアップする処理を擬似的に実現しているAppleScriptです。

macOSの日本語入力プログラムで実装している、「部品が共通な漢字を検索」機能っぽいものを目指したものです。例によって、掲載プログラムリストでは部品が足りずに実行できないので、以下のすべての部品を含むScriptバンドルのアーカイブをダウンロードして実行してください。

–> Download listupKanjiWithCommonParts(including AppleScript Libraries and json data)

「際」「隆」という文字を指定すると、

--> {"際", "障", "隠", "隣", "阪", "防", "阻", "附", "降", "限", "陛", "院", "陣", "除", "陥", "陪", "腹", "堕", "陰", "墜", "陳", "陵", "陶", "陸", "険", "陽", "隅", "隆", "隊", "階", "随", "隔", "隙"}

という結果を返してきます。Mac mini M1で0.46秒ぐらいです。処理時間はデータによっては大幅に増える場合がありますが、そういうものだと思ってください。テストした範囲内では最悪で1.6秒ぐらいかかりました。もともとのJSONデータを(日本の)常用漢字の範囲内だけにシェイプアップすれば半分以下の速度で実行できると思います。

いろいろ細かく高速化のための記述していますが、実測値で0.02秒ぐらい速くなっているだけなので、わかりやすさを優先して高速化のための記述はすべて外してしまったほうがよいかもしれません。

割とロマン系の「役に立つんだかどうか不明だけど、作れることがわかったので作ってみた」プログラムです。

AppleScript名:漢字をパーツに分解して部品が共通する文字を検索.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/14
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
use jkLib : script "jyoyoKanjiLib" –常用漢字一覧を返してくるAppleScriptライブラリ

script jsonStr
  property aJsonDict : missing value
  
property jKanji : missing value
  
property vOut : missing value
end script

script spd0
  property aList : {}
  
property bList : {}
  
property cList : {}
end script

property NSString : a reference to current application’s NSString
property NSCountedSet : a reference to current application’s NSCountedSet
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set aTarg to "際"
set (aList of spd0) to getRelatedCharacters(aTarg) of me

set bTarg to "隆"
set (bList of spd0) to getRelatedCharacters(bTarg) of me

set (cList of spd0) to (aList of spd0) & (bList of spd0)

set cRes to returnDuplicatesOnly((cList of spd0)) of me
–> {"際", "障", "隠", "隣", "阪", "防", "阻", "附", "降", "限", "陛", "院", "陣", "除", "陥", "陪", "腹", "堕", "陰", "墜", "陳", "陵", "陶", "陸", "険", "陽", "隅", "隆", "隊", "階", "随", "隔", "隙"}

on returnDuplicatesOnly(aList as list)
  script spd1
    property bList : {}
    
property dupList : {}
  end script
  
  
set aSet to NSCountedSet’s alloc()’s initWithArray:aList
  
set (bList of spd1) to (aSet’s allObjects()) as list
  
  
set (dupList of spd1) to {}
  
repeat with i in (bList of spd1)
    set aRes to (aSet’s countForObject:i)
    
if aRes > 1 then
      set the end of (dupList of spd1) to (contents of i)
    end if
  end repeat
  
  
return (dupList of spd1)
end returnDuplicatesOnly

on getRelatedCharacters(aTarg as string)
  set aVector to parseKanjiToParts(aTarg) of me
  
  
set (vOut of jsonStr) to {}
  
repeat with i in aVector
    set j to contents of i
    
set tmpOut to searchKanjiFromElementJ(j) of me
    
if tmpOut is not equal to missing value then
      set (vOut of jsonStr) to (vOut of jsonStr) & tmpOut
    end if
  end repeat
  
  
set tmp2 to makeUniqueListFrom((vOut of jsonStr)) of me
  
  
return tmp2
end getRelatedCharacters

on cleanUp1DList(aList as list, cleanUpItems as list)
  script spd2
    property bList : {}
  end script
  
  
set (bList of spd2) to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of (bList of spd2) to j
    end if
  end repeat
  
return (bList of spd2)
end cleanUp1DList

on parseKanjiToParts(aTarg)
  set outList to {}
  
  
set kList to listupQueryKeysForKanji() of me
  
repeat with i in kList
    set j to contents of i
    
set aTargL to ((aJsonDict of jsonStr)’s valueForKey:j) as list
    
if aTarg is in aTargL then
      set the end of outList to j
    end if
  end repeat
  
return outList
end parseKanjiToParts

on searchKanjiFromElementJ(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
if (jKanji of jsonStr) = missing value then my initJ()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
  
set cArray to aRes’s arrayByAddingObjectsFromArray:(jKanji of jsonStr)
  
set cRes to returnDuplicatesOnly(cArray) of me
  
return cRes
end searchKanjiFromElementJ

on searchKanjiFromElement(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
return aRes as list
end searchKanjiFromElement

on listupQueryKeysForKanji()
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s allKeys()
  
return aRes as list
end listupQueryKeysForKanji

–Pure AS風のパラメータ記述
on makeUniqueListFrom(theList)
  set theSet to current application’s NSOrderedSet’s orderedSetWithArray:theList
  
return (theSet’s array()) as list
end makeUniqueListFrom

on initJ()
  set (jKanji of jsonStr) to (current application’s NSArray’s arrayWithArray:(retJyouyouKanji() of jkLib))
end initJ

on init()
  –https://github.com/yagays/kanjivg-radical
  
set aPath to (POSIX path of (path to me)) & "Contents/Resources/element2kanji.json"
  
set jsonString to NSString’s alloc()’s initWithContentsOfFile:(aPath) encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set (aJsonDict of jsonStr) to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
end init

★Click Here to Open This Script 

Posted in JSON list Text | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

構成要素を指定して漢字検索 v4

Posted on 12月 13, 2021 by Takaaki Naganoya

漢字の構成要素を指定して漢字を検索するAppleScriptです。

ライブラリやデータを含むAppleScriptバンドルをダウンロードできるようにしておきます。

–> Download kanjiSearchFromPartsV4 (Inlcuding AppleScript Libraries)

macOS標準搭載の「関連文字に変換」に似たような動作を行います。「関連文字に変換」では、いったん漢字を構成要素に分解し、分解した要素をもとに共通漢字を検索するユーザーインタフェースになっていますが、こちらはダイレクトに構成要素部品を示して、そこから共通部品を持つ文字を検索します。

 {"氵", "艹", "田"}

をパラメータに指定して実行すると、

--> {"藩"}

という結果を返します。同様に、

 {"⻖", "生"} 

をパラメータに指定して実行すると、

--> {"隆"}

という結果を出します。日本語を学習中の他国語の話者の方とか、子供、漢字をそろそろ思い出せなくなってきているお年寄りなどにニーズがあると思われるプログラムです。

元になっている「kanjiVG」データの特性なのですが、大人がよく理解して使う分には問題ないものの、義務教育過程の小中学生が使うと、学校で教える漢字部品の指定の仕方と異なるので、「宿題のために使えてもテストの点数が落ちる」といった弊害があるかもしれません。

あとは、GUIがあったほうが使いやすいプログラムだと思われるため、電卓(計算機)のようなイージーなインタフェースをつけて道具に仕立てたほうがよいのでしょう。

本プログラムリストは、スクリプトエディタに転送できますが、バンドル形式で保存してデータやライブラリを含んだ形にしないと実行できないため、実行のためには本記事冒頭にあるダウンロードリンクからバンドルのアーカイブをダウンロードしてください。

AppleScript名:構成要素を指定して漢字検索 v4.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/02/21
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
use jkLib : script "jyoyoKanjiLib" –常用漢字一覧を返してくるAppleScriptライブラリ

property NSString : a reference to current application’s NSString
property NSCountedSet : a reference to current application’s NSCountedSet
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

script jsonStr
  property aJsonDict : missing value –JSONから読み取ったNSDictionary
  
property jKanji : missing value –常用漢字データ(NSArray)
end script

–図形としての構成要素を指定して漢字検索(厳密にいえば部首ではない)
set p1List to {"氵", "艹", "田"}
set c1Res to searchKanjiFromElementListJ(p1List) of me
–> {"藩"}

set p2List to {"⻖", "生"} –こざとへん。データ内のこざとへんとおおざとの区別がよくわからなかった
set c2Res to searchKanjiFromElementListJ(p2List) of me
–> {"隆"}

–検索に使える部首のキー文字の一覧を返す
–set qList to listupQueryKeysForKanji() of me
–> {"工", "棘", "左", "位", "婁", "攴", …}

on searchKanjiFromElementListJ(aQueryList as list)
  script spd
    property aList : {}
    
property bList : {}
  end script
  
  
set aLen to length of aQueryList
  
if aLen = 1 then return searchKanjiFromElementJ(first item of aQueryList) of me
  
  
set (aList of spd) to {}
  
set (bList of spd) to {}
  
repeat with i in aQueryList
    set j to contents of i
    
set tmpRes to searchKanjiFromElementJ(j) of me
    
set the end of (aList of spd) to tmpRes
  end repeat
  
  
return searchDuplicateItemsOnlyIn2DList((aList of spd)) of me
  
end searchKanjiFromElementListJ

on searchDuplicateItemsOnlyIn2DList(allList as list)
  set aLen to length of allList
  
  
set aList to contents of first item of allList
  
set bList to contents of rest of allList
  
  
repeat with i in bList
    
    
set j to contents of i
    
    
–aListとbListを連結したListで重複が存在するかをチェック
    
set aArray to (current application’s NSArray’s arrayWithArray:aList)
    
set bArray to (current application’s NSArray’s arrayWithArray:j)
    
set cArray to (aArray’s arrayByAddingObjectsFromArray:bArray)
    
    
set aList to returnDuplicatesOnly(cArray, aLen) of me
    
  end repeat
  
  
return aList
end searchDuplicateItemsOnlyIn2DList

on searchKanjiFromElementJ(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
if (jKanji of jsonStr) = missing value then my initJ()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
  
set cArray to aRes’s arrayByAddingObjectsFromArray:(jKanji of jsonStr)
  
set cRes to returnDuplicatesOnly(cArray) of me
  
return cRes
end searchKanjiFromElementJ

on searchKanjiFromElement(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
return aRes as list
end searchKanjiFromElement

on listupQueryKeysForKanji()
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s allKeys()
  
return aRes as list
end listupQueryKeysForKanji

on initJ()
  set (jKanji of jsonStr) to current application’s NSArray’s arrayWithArray:(retJyouyouKanji() of jkLib)
end initJ

on init()
  –https://github.com/yagays/kanjivg-radical
  
set aPath to (POSIX path of (path to me)) & "Contents/Resources/element2kanji.json"
  
set jsonString to NSString’s alloc()’s initWithContentsOfFile:(aPath) encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set (aJsonDict of jsonStr) to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
end init

on returnDuplicatesOnly(anArray)
  set aSet to NSCountedSet’s alloc()’s initWithArray:anArray
  
set bList to (aSet’s allObjects()) as list
  
  
set dupList to {}
  
repeat with i in bList
    set aRes to (aSet’s countForObject:i)
    
if aRes > 1 then
      set the end of dupList to (contents of i)
    end if
  end repeat
  
  
return dupList
end returnDuplicatesOnly

★Click Here to Open This Script 

Posted in list Text | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

2021年に書いた価値あるAppleScript

Posted on 12月 10, 2021 by Takaaki Naganoya

2021年:macOS 12(自分はmacOS 11、12を使用)

毎年行なっている、Piyomaru Softwareが書いたAppleScriptの1年を振り返る記事の2021年版です。恒例行事です。

あいかわらずコロナ禍の影響で踏んだり蹴ったりの1年だったので、本を書くことに注力。これではPiyomaru SoftwareではなくPiyomaru Publishingだ、などと冗談を言っていましたが、年に13冊出して進行中のものが数冊あるという状況。本当にPiyomaru Publishingです。

→ 2018年に書いた価値あるAppleScript
→ 2019年に書いた価値あるAppleScript
→ 2020年に書いた価値あるAppleScript

本を短期間かつ1人で書いて、レイアウトして、電子書籍として仕上げるためには、それを補助する大量のAppleScriptが必要です。面倒な作業はとっととAppleScriptで自動化して、繰り返し行う煩雑な作業をMacに行わせる環境を整備してきました。逆に、そういうScriptを用意できているので作業を省力化して、書けているという状況があるともいえます。AppleScriptによる補助なしにはこんなペースで書くことは不可能でしょう。

今年書いた本の中でどれがどうということはないんですが、Cocoa Scripting本は書き始める前には、一体どういうものを書いてよいのかさっぱり分かりませんでした。戦場の絆本だってそうです。どれひとつとして、企画段階のままの内容で完成したものなどありません。

2021/1 FileMaker Pro Scripting Book with AppleScript macOS 12対応アップデート
2021/2 [Eng] FileMaker Pro Scripting Book with AppleScript
2021/3 Cocoa Scripting Course Volume #1 NSString
2021/3 Cocoa Scripting Course Volume #2 NSArray
2021/3 Macとアップルスクリプトで学ぶ いまからはじめるプログラミング①
2021/4 Macとアップルスクリプトで学ぶ いまからはじめるプログラミング②
2021/4 Cocoa Scripting Course Volume #3 NSDictionary
2021/5 ミュージック.app scripting book with AppleScript
2021/7 機能強化AppleScript集 CotEditor用 PowerPack 取扱説明書
2021/7 空前絶後あなたの知らない ヤバイAppleScriptの世界
2021/8 elgato STREAM DECK 徹底活用 Mac+STREAM DECKで時短+作業効率化!!
2021/9 空前絶後あなたの知らないヤバイAppleScriptの世界 iOS対応版
2021/10 アーケードゲーム「戦場の絆」僕らの15年戦争

書いた本はどれも「こんなものがあればいいのに!」というアイデアを形にしたものばかり(読者ニーズを形にしたものではないところにギャップはありますが、、、)。新基軸は、iOS用のFileMaker Goを電子書籍プラットフォームと見立てて、各種情報+ロジックを1つにまとまた本を出したことです。「iOSで読む」本についてはいろいろ構想もありますが、まだ試行錯誤の最中といったところでしょう。

FileMaker Pro系のコンテストには「FileMaker選手権2020」「FileMaker選手権2021」「FM-1グランプリ」の3つに応募し、FileMaker選手権2020ではDropbox賞を3つ(3年分)、FM-1グランプリでは仕事効率化部門で部門準賞をいただいています。

■BEST AppleScript of 2021

今年書いたAppleScriptの中で、個人的に一番会心の出来だったのは「部首で漢字検索」シリーズです。外部のデータに依存してはいるものの、テキスト中の漢字を部首で検索できるというのは、なかなかすごいことです。

技術レベル云々を問題にしなければ、macOS 11+M1 MacでAppleScriptの動作速度が遅いことを検証したサンプルAppleScriptと報告書のまとめでしょうか。おそらく、他にも同じようなレポートが行われていたところに、きちんとベンチマークをとって動作の異様さを報告したものが役立ったが、おそらくその前からApple社内で当たりはつけていたのでしょう。

M1 Mac miniが到着してすぐベンチマークを回して「なんだこの遅さは???」と気づいたぐらいだったので、半年前のWWDC以降に開発者向けに貸し出されたDTK(Developer Transition Kit、A12Bionic搭載マシン)を試していた人たちは揃いも揃ってみんな本当に気づかなかったんだろうか? という素朴な疑問があります。

M1上でのAppleScriptからのCocoa呼び出しの速度がmacOS 11.x上のレベルのままだったら、と思うとなかなか怖いものがあります。

■2021/1

FileMaker Pro Scripting Bookの英語版を書いていたので、こうしたものが必要になりました。日本語のままで未翻訳の文章が残っていると困るので、そのチェックのためのツールを書いてみたというところです。
1/28 最前面のKeynote書類のテキストアイテムの英語化率を求める

■2021/2

部首で漢字検索を行えるAppleScriptは、もともとのデータを作った人が偉いわけですが、実際に手元で用意したテストデータに対して実行し、その処理内容を正しく評価してAppleScriptから呼び出せるようにしたところに価値があるものと考えます。
2/21 部首で漢字検索
2/24 指定した文字で囲まれたキーワードの色を置換する

■2021/3

環境を用意するだけで手間がかかる超解像処理が、PixelMator Proで手軽に呼び出せるようになっていることに感服します。本処理は実に有用です。
3/25 画面スナップショット超解像バッチ処理 v1

■2021/4

Pagesの書類から最大サイズの文字で書かれている文章をタイトルとして取り出す処理を行うAppleScriptです。せめてPagesでタイトルの書式属性に該当するテキストを抽出できるようになっているとよいのですが、そういう機能は一切ないので困ります。
4/12 Pagesで最前面の書類中のテキストアイテムと本文テキストで文字サイズが最大のもののテキストを求める

■2021/5

Music.app本に掲載するために書いたものですが、なかなか苦労しました。AppleScriptからデータを取得したい内容です。
5/12 LAN上のdaapクライアントの共有名をリストアップ v2

■2021/6

M1 Mac miniを手にしたその日に強烈な違和感をおぼえ、ベンチマークを用意して実行してみました。これをやっていなかったら、macOS 12でもAppleScriptからのCocoa呼び出しは遅いままだったでしょう。技術的に高度かどうかはさておき、これをやらなかったら全世界的に致命的な影響が出たであろうことを考えると身震いします。
6/22 macOS 11, AppleScriptをFirestormではなくIcestormで実行か?!

■2021/7,8

とくになし。この頃が一番忙しかったので。

■2021/9

電子書籍「僕らの15年戦争」のために用意したものです。Pagesは長大なコンテンツの編集にまったく向いておらず、なるべく記事ごとに小割りに書類を分けないと困ります。そのため、全書類に対して同じ修正を行いたいといった場合、手作業で行うのは現実的な方法ではありません。AppleScriptから一括処理することが必須です。
9/22 Pagesの最前面の書類で選択中のツメを修正する

■2021/10

macOS 12で新規搭載されたショートカットをAppleScriptから呼び出したりいろいろ試し出しました。ただ、ショートカットの完成度がいまひとつというべきなのか、「よくまあこれでいままでiOSユーザーは文句を言わなかったもんだ」と呆れるほどのいい加減な出来に言葉を失っています。
10/27 AppleScriptからショートカット実行&ショートカット内でAppleScriptを実行

■2021/11

macOS 12上でショートカットをAppleScriptの処理内で実行するため、存在確認とインストールまで自動で行うことを試したAppleScriptです。まだ完全体とは言い難いですが、方向性はいいと感じています。
11/1 Shortcuts Eventsでショートカットのインストール+実行

電子書籍の作成補助Scriptの1つ。表のセルに入れた丸つき数字のリナンバー処理は、表コンテンツを編集すると必ず発生していた無意味な作業であり、これを手軽に行えるようにしたことは意義深いでしょう。
11/27 選択中の表の指定行・列のマル付き数字リナンバー v2

Posted in news | Tagged 10.15savvy 11.0savvy 12.0savvy | 2 Comments

アンダースコアが入っていたら削除して次の文字を大文字化

Posted on 12月 6, 2021 by Takaaki Naganoya

文字列の途中にアンダースコア(「_」)が入っていたら削除し、その次の文字列(たぶんアルファベット)を大文字化するAppleScriptです。

github上でたまに「あ、これいいかも」というObjective-Cで書かれたプログラムを見つけた場合に、Cocoa Frameworkとしてビルドして外部からAppleScriptから呼び出したくなります。

しかし、メソッド名にアンダースコア(「_」)が入っていて断念したことが、何回かあります。

AppleScript、とくにCocoaを呼び出すAppleScriptObjCの世界では、そうしたCocoa Framework中のメソッドにアンダースコアが入っていると、「|」でエスケープしても展開されて、パラメータがそこに入るものとして解釈されてしまいます。つまり、すべてのソースを書き換えなくてはならないわけです。

ここで、単純にアンダースコアを削除しただけでは可読性が落ちるので、アンダースコアの後にあった文字(おそらくアルファベット)を大文字化したいわけですが、なかなか手が混んでいて大変な処理です。

そこで、AppleScriptを書いて処理させるためのテストプログラムを作ってみました。1行分の処理はできているように見えます。

この種類のプログラムは、何回か組んだ記憶があるのですが、いつも大して役に立たずに立ち消えになっているような気が、、、、やっぱり、Xcode自体のAppleScript対応度が散々な出来で、やりたい処理ができずにどこかに消えてしまうから、なのかも、、、、

AppleScript名:アンダースコアが入っていたら削除して次の文字を大文字化.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/06
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to "- (void)patch_splitMax_toMax:(NSMutableArray *)patches;"
set aTarg to "_"

set b to removeAcharAndCapitalize(a, aTarg) of me
–> "- (void)patchSplitMaxToMax:(NSMutableArray *)patches;"

on removeAcharAndCapitalize(aStr, aTargChar)
  if aStr contains aTargChar then
    set bList to countAcharInStrAndDetectPos(aStr, aTargChar) of me –1行に複数のアンダースコアが登場する場合に対処
    
copy bList to {countNum, itemList}
    
    
set revList to reverse of itemList –後方から処理
    
    
repeat with i in revList –アイテム番号でループ(後方から処理)
      set a1Str to text 1 thru (i – 1) of aStr
      
set bStr to text (i + 1) thru -1 of aStr
      
set bStr2 to capitalizeHeadChar(bStr) of me
      
set aStr to a1Str & bStr2
    end repeat
  end if
  
  
return aStr
end removeAcharAndCapitalize

–指定文字列の頭文字を大文字化
on capitalizeHeadChar(aStr)
  set a1Str to text 1 of aStr
  
set a2Str to text 2 thru -1 of aStr
  
  
set a1aStr to current application’s NSMutableString’s stringWithString:a1Str
  
set a1bStr to (a1aStr’s uppercaseString()) as string
  
  
return (a1bStr & a2Str)
end capitalizeHeadChar

–文字列中に指定文字が何個入っているかカウントし、登場位置をリストで返す
on countAcharInStrAndDetectPos(aStr, aTargChar)
  set aList to {}
  
set posC to 1
  
  
considering case
    set aHit to offset of aTargChar in aStr
    
    
if aHit is not equal to 0 then
      set aaList to characters of aStr
      
set aCount to 0
      
      
repeat with i in aaList
        
        
set j to contents of i
        
        
if j = aTargChar then
          set aCount to aCount + 1
          
set the end of aList to posC
        end if
        
        
set posC to posC + 1
        
      end repeat
      
      
return {aCount, aList}
    else
      return {0, {}}
    end if
  end considering
end countAcharInStrAndDetectPos

★Click Here to Open This Script 

Posted in Text | Tagged 10.15savvy 11.0savvy 12.0savvy | Leave a comment

選択中の表の指定行・列のマル付き数字リナンバー v3

Posted on 12月 4, 2021 by Takaaki Naganoya

Keynoteの現在オープン中の書類の、現在表示中のスライド(ページ)上にある、選択中の表オブジェクトの、選択中のセルに入っているマルつき数字(①②③….)を左から順番にリナンバーするAppleScriptです。

以前のバージョン(v2)では処理対象行や列をダイアログ上で選択するようになっていましたが、本バージョンではKeynote上で処理対象のセルを選択しておくことで処理範囲をScript側に知らせるようにしました。

選択範囲のセルのデータを取得し、それらに入っているマルつき数字の最小値を計算し、ユーザーに開始値としてダイアログで確認を行います。前バージョンと違ってユーザーに確認するのはこの点だけです。

すぐに選択範囲のセルのテキスト冒頭に入っているマルつき数字をリナンバーします。本Scriptにおけるマルつき数字の範囲は1から50です(白地のマルつき数字に限る)。

AppleScript名:選択中の表の指定行・列のマル付き数字リナンバー v3.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/03
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

set aOffset to 0

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲(range)を取得
set aRange to returnSelectedRangeOnCurrentSlide() of me
if aRange = false then
  display dialog ""
  
return
end if

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of range aRange
  end tell
end using terms from

–現在選択中の表オブジェクト中の選択範囲中のセルの値をすべて取得(1D List)
set aList to returnSelectedTableCellDataOnCurrentSlide() of me
set bList to cleanUp1DList(aList, missing value) of me

—データ中に丸つき数字が存在した場合には、最小のものを取得
set aOffset to (getMinimumNumFromNumberWithSign(bList) of me)

–一応、ユーザーに推測した開始値でよいか確認を取る
set dRes to text returned of (display dialog "丸つき数字の開始値:" default answer (aOffset as string))
try
  set aOffset to (dRes as number) – 1
on error
  set aOffset to 0
end try

–Keynoteの表のセルから取得したデータから丸つき数字を除去する
set cList to removeNumberWithSignFromList(bList) of me

–list中の各アイテムの冒頭に順次丸つき数字を追加する
set dList to {}
set aCount to 1

repeat with i in cList
  set j to convNumToNumWithSign(aCount + aOffset) of me
  
set jj to contents of i
  
  
set the end of dList to (j & jj)
  
  
set aCount to aCount + 1
end repeat

–リナンバー対象のセルをリナンバーした丸つき数字+内容で置換
set aCount to 1
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      set value of i to contents of item aCount of dList
      
set aCount to aCount + 1
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum > 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–1D List上で指定データを検索してヒットしたアイテム番号を返す
on search1DList(aList, aTarg)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set anIndex to anArray’s indexOfObject:aTarg
  
if (anIndex = current application’s NSNotFound) or (anIndex > 9.99999999E+8) then
    return false
  end if
  
return (anIndex as integer) + 1 –convert index base (0 based to 1 based)
end search1DList

–1D listのクリーニング
on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    else
      set the end of bList to ""
    end if
  end repeat
  
return bList
end cleanUp1DList

–text in listから丸つき数字を除去する
on removeNumberWithSignFromList(aList as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
set j2 to removeNumberWithSign(j) of me
    
set the end of bList to j2
  end repeat
  
return bList
end removeNumberWithSignFromList

–文字列から丸つき数字を除去する
on removeNumberWithSign(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeNumberWithSign

–1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める
on getMinimumNumFromNumberWithSign(aList)
  set nList to {}
  
  
repeat with i in aList
    set j to contents of i
    
–与えられたテキストのうち、丸つき数字(白)の
    
set j2 to holdNumberWithSignOnly(j) of me
    
set n2List to characters of j2 –複数の丸つき数字が入っている場合に対処
    
    
repeat with ii in n2List
      set jj to contents of ii
      
set tmpNum to decodeNumFromNumWithSign(jj) of me
      
set the end of nList to tmpNum
    end repeat
    
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:nList
  
set cRes to (anArray’s valueForKeyPath:"@min.self") as integer
  
return cRes
end getMinimumNumFromNumberWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

–現在のスライド上で選択中の表オブジェクト内の選択中のセルの値を取得する
on returnSelectedTableCellDataOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
tell theTable
          set vList to value of every cell of selection range
          
set cCount to count of column of selection range
          
set rCount to count of row of selection range
          
          
if rCount > 1 then return false –複数行選択されていた場合にはエラーを返す
          
          
return vList
        end tell
      end tell
    end tell
  end tell
end returnSelectedTableCellDataOnCurrentSlide

–現在のスライド上で選択中の表オブジェクト内の選択中の範囲(range)のnameを取得する
on returnSelectedRangeOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
tell theTable
          set aPref to properties of selection range
          
set aRangeStr to name of aPref
          
return aRangeStr
        end tell
      end tell
    end tell
  end tell
end returnSelectedRangeOnCurrentSlide

★Click Here to Open This Script 

Posted in list Number | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

選択中の表の指定行・列のマル付き数字リナンバー v2

Posted on 11月 27, 2021 by Takaaki Naganoya

Keynote書類の現在表示中のスライド(ページ)中の選択中の「表」において、指定の行、指定のヘッダー列以降のセルに対して、(1)丸つき数字の削除 (2)丸つき数字の最小値の計算 を行い、丸つき数字の番号降り直しの開始値をユーザーに確認したうえで、丸つき数字部分のみのリナンバーを行います。

本ScriptはmacOS 12+Keynote v11.2で作成・動作確認を行なってあります。それほどOSおよびKeynoteのバージョンに依存するような処理は行なっていないため、上記以外のOSおよびKeynoteのバージョンの組み合わせでも動くと思います。

–> Watch Demo Movie

初期状態では、

と、丸つき数字のナンバリングが項目の入れ替えなどで正しく並んでいない状態を想定しています。

リナンバー対象の表を選択した状態で本Scriptを実行すると、

リナンバー対象行の問い合わせを行います。一番左の列のデータを抽出して選択させます。この場合には、「名称」を選択します。

次に、リナンバー対象データの列指定を行います。指定列「以降」から「末尾」までをリナンバー処理対象とみなします。この場合には「名称」を選択します。

最後に、丸つき数字のリナンバー開始数値を確認します。

すでに、対象範囲のデータを取り出して丸つき数字の最小値については計算してあるので、その確認のためにダイアログ入力を求めます。推測した数値でよければOKを、修正の必要があれば数値を変更したうえでOKボタンをクリック。

適切に処理が行われれば、丸つき数字部分のリナンバーが完了することでしょう。

AppleScript名:選択中の表の指定行・列のマル付き数字リナンバー v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

set aOffset to 0

–選択中のKeynote書類上の表への参照を取得する
set aTable to returnSelectedTableOnCurrentSlide() of me

if aTable = false then
  display notification "No selected table on Keynote document"
  
return
end if

–行選択
using terms from application "Keynote"
  tell aTable
    tell column 1
      set aList to value of every cell
    end tell
  end tell
end using terms from

set a2List to cleanUp1DList(aList, missing value) of me
set aRes to choose from list a2List with prompt "リナンバー対象の行選択"
set a2Res to search1DList(a2List, first item of aRes) of me

–列選択
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      set bList to value of every cell
    end tell
  end tell
end using terms from

set b2List to cleanUp1DList(bList, missing value) of me
set bRes to choose from list b2List with prompt "リナンバー対象の列選択(ヘッダー末尾列)"
set b2Res to search1DList(b2List, first item of bRes) of me

–リナンバー対象のデータを取得(対象データのみ抽出)
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      set bList to value of cells (b2Res + 1) thru -1
    end tell
  end tell
end using terms from

—データ中に丸つき数字が存在した場合には、最小のものを取得
set aOffset to (getMinimumNumFromNumberWithSign(bList) of me)

–一応、ユーザーに推測した開始値でよいか確認を取る
set dRes to text returned of (display dialog "丸つき数字の開始値:" default answer (aOffset as string))
try
  set aOffset to (dRes as number) – 1
on error
  set aOffset to 0
end try

–Keynoteの表のセルから取得したデータから丸つき数字を除去する
set cList to removeNumberWithSignFromList(bList) of me

–list中の各アイテムの冒頭に順次丸つき数字を追加する
set dList to {}
set aCount to 1

repeat with i in cList
  set j to convNumToNumWithSign(aCount + aOffset) of me
  
set jj to contents of i
  
  
set the end of dList to (j & jj)
  
  
set aCount to aCount + 1
end repeat

–リナンバー対象のセルをリナンバーした丸つき数字+内容で置換
set aCount to 1
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      repeat with i from (b2Res + 1) to ((length of bList) + 1)
        set value of cell i to contents of item aCount of dList
        
set aCount to aCount + 1
      end repeat
    end tell
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum > 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

–1D List上で指定データを検索してヒットしたアイテム番号を返す
on search1DList(aList, aTarg)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set anIndex to anArray’s indexOfObject:aTarg
  
if (anIndex = current application’s NSNotFound) or (anIndex > 9.99999999E+8) then
    return false
  end if
  
return (anIndex as integer) + 1 –convert index base (0 based to 1 based)
end search1DList

–1D listのクリーニング
on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    else
      set the end of bList to ""
    end if
  end repeat
  
return bList
end cleanUp1DList

–text in listから丸つき数字を除去する
on removeNumberWithSignFromList(aList as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
set j2 to removeNumberWithSign(j) of me
    
set the end of bList to j2
  end repeat
  
return bList
end removeNumberWithSignFromList

–文字列から丸つき数字を除去する
on removeNumberWithSign(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeNumberWithSign

–1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める
on getMinimumNumFromNumberWithSign(aList)
  set nList to {}
  
  
repeat with i in aList
    set j to contents of i
    
–与えられたテキストのうち、丸つき数字(白)の
    
set j2 to holdNumberWithSignOnly(j) of me
    
set n2List to characters of j2 –複数の丸つき数字が入っている場合に対処
    
    
repeat with ii in n2List
      set jj to contents of ii
      
set tmpNum to decodeNumFromNumWithSign(jj) of me
      
set the end of nList to tmpNum
    end repeat
    
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:nList
  
set cRes to (anArray’s valueForKeyPath:"@min.self") as integer
  
return cRes
end getMinimumNumFromNumberWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

★Click Here to Open This Script 

Posted in list Number Text | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | 1 Comment

1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める

Posted on 11月 26, 2021 by Takaaki Naganoya

1D List(1D Array)に入っているテキスト要素から、丸つき数字を抽出して数値化し、得られた数値のうち最小のものを求めるAppleScriptです。

こんな、丸つき数字のテキストが入った「表」をKeynote上に作成したときに、

項目を追加したり順番を入れ替えたりすると、セルの中に入れている丸つき数字も変更する必要があります。これが手作業でたいへんめんどくさいので、Scriptで処理できるようにサブルーチンを整備してみました。

やりたいことは割とシンプルなはずなのに、割といろいろ書かないと実現しない処理でもあります。

そして、本Scriptは置換対象のデータから丸つき数字部分を取り出して、最小のものを判別し、リナンバー時の開始値を既存のデータから推測するという処理のために作ったものです。

AppleScript名:1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aList to {"❹Script Editor", "②Xcode", "③ショートカット", "④Script Debugger④", "⑤SD Notary", "⑥UI Browser", "⑦Accessibility Inspector"}
set aSmallestNum to getMinimumNumFromNumberWithSign(aList) of me
–> 2

–1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める
on getMinimumNumFromNumberWithSign(aList)
  set nList to {}
  
  
repeat with i in aList
    set j to contents of i
    
–与えられたテキストのうち、丸つき数字(白)の
    
set j2 to holdNumberWithSignOnly(j) of me
    
set n2List to characters of j2 –複数の丸つき数字が入っている場合に対処
    
    
repeat with ii in n2List
      set jj to contents of ii
      
set tmpNum to decodeNumFromNumWithSign(jj) of me
      
set the end of nList to tmpNum
    end repeat
    
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:nList
  
set cRes to (anArray’s valueForKeyPath:"@min.self") as integer
  
return cRes
end getMinimumNumFromNumberWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
–set numStr6 to "⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇"
  
–set numStr7 to "1︎⃣2︎⃣3︎⃣4︎⃣5︎⃣8︎⃣9︎⃣"
  
–set numStr8 to "⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

文字列から丸つき数字のみ抽出する

Posted on 11月 26, 2021 by Takaaki Naganoya

指定の文字列から丸つき数字の部分だけを抽出するAppleScriptです。

丸つき数字の文字を数値に変換するサブルーチンのために作成したものです。

AppleScript名:文字列から丸つき数字のみ抽出する.scpt
–  Original by: Shane Stanley
–  Created on: 2019/11/04
–  Modified by: Takaaki Naganoya
–  Modified on: 2021/11/26

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

set aRes to holdNumberWithSignOnly("⑦Accessibility Inspector") of me
–> "⑦"

on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

丸つき数字を数値にデコードする v2

Posted on 11月 26, 2021 by Takaaki Naganoya

丸つき数字を数値にデコードするAppleScriptです。よくよく考えるとこれまでに作っていなかったので、作っておきました。

AppleScript名:丸つき数字を数値にデコードする v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

set dRes to decodeNumFromNumWithSign("⑮") of me
–> 15

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
–set numStr6 to "⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇"
  
–set numStr7 to "1︎⃣2︎⃣3︎⃣4︎⃣5︎⃣8︎⃣9︎⃣"
  
–set numStr8 to "⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy 11.0savvy | Leave a comment

丸つき数字の除去と数値からの変換

Posted on 11月 26, 2021 by Takaaki Naganoya

丸つき数字を指定文字列から削除するAppleScriptと、1〜50の範囲の数値を丸つき数字に変換するAppleScriptです。

Keynoteの表に入っている丸つき数字のリナンバーを行うために、必要な処理を書いてみたものです。日本語環境でしかこういう文字は使わないことでしょう。

丸つき数字が文字コード上でところどころ切れているため、単純に調べるのに手間がかかりました。

AppleScript名:数字を丸つき数字に変換.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

repeat with i from -10 to 100 by 1
  set aStr to convNumToNumWithSign(i) of me
  
log {i, aStr}
end repeat

on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum > 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

★Click Here to Open This Script 

AppleScript名:丸つき数字を検出・削除
–  Original by: Shane Stanley
–  Created on: 2019/11/04
–  Modified by: Takaaki Naganoya
–  Modified on: 2021/11/26

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

set aRes to removeNumberWithSign("⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿⓿❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴➀➁➂➃➄➅➆➇➈➉➊➋➌➍➎➏➐➑➒➓") of me
–> "‍‍‍‍‍‍"

on removeNumberWithSign(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeNumberWithSign

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

AS Publisher v18–> v19

Posted on 11月 7, 2021 by Takaaki Naganoya

とくに新しいわけでもなんでもありませんが、「スクリプトエディタでオープン中のScriptをリンクつきのHTMLとして書き出すAppleScript」である「AS Publisher」の機能改修版です。前バージョンまではURLエンコーディング処理にpythonを呼び出して処理していましたが、Cocoaの機能を呼び出すように書き換えました。

# v18に処理のミスがあり、そのミスがOS側で「こういう間違いが多いから通しておこう」と見逃されていたのが厳密に処理されるようになったため、ミスの部分を修正しておきました

AppleScriptのリストをBlogなどに掲載する場合、WordPressやMarkdownの構文自動認識フォーマットを使って掲載されているものを見かけますが、そもそも「return return」(改行コードを返す)といった記述が許可されており、それらがきちんと構文色分けされているAppleScriptにおいて、単なるキーワードによる構文自動認識フォーマットが役立つわけがありません(知らずによく実装したもんですわ>オリジナル版を作った人)。

それ以前に「必要があったので」作ったAppleScriptでもあります。手元に残っている最古のバージョンは2005年の4月1日のタイムスタンプになっていますが、おそらくそれ以前から作っていたものでしょう。

# retMacOSpathList ハンドラで、ファイルパスをやたらとCastしている箇所がありますが、Mac OS X 10.5より前のAppleScriptではファイルパスの文字列のエンコーディングに問題を抱えていた(AppleScript側の文字列とのエンコーディング/ノーマライズの不整合)ので、その対策です。ただし、不必要なぐらい無駄な処理をしているので、参考にはなりません。Mac OS X初期の試行錯誤を強いられていた時代の遺物とでも思ってください

本Scriptおよびその派生版は、macOS標準装備のScript Menuに入れて実行することを想定しています。AppleScriptからスクリプトエディタを操作する場合に、Mac OS X 10.4の時代ではあまり動作が安定せず、もう少し後のバージョンのOSになるまで(10.5ぐらい?)、たいへんスリリングな(クラッシュが頻発する)Scriptingを強いられていました。

いまはそのあたりは安定していますが、ごく初期においては挙動が怪しい部分があったということです(その時期で知識が止まっている人はそういう認識のまま)。

話を戻しましょう。

AppleScriptのプログラムをBlogなどに掲載する際に、自分はこういう道具を作って使用しています。巨大なリストを掲載する場合には記事の文字数が膨大になってしまい、掲載をあきらめたこともありましたが、ほんのごく一部です。

このScriptをもとに、自分が使いやすいように(色指定とか)変更してみてもよいでしょう。

「動けばいい」ぐらいの割り切りで、秘伝のタレに継ぎ足してメンテナンスしてきましたが、HTMLタグで書式制御するよりはCSSで制御したほうがシンプルにできてよさそうな感じもします。

ファイル処理も、とくに「ここまでやる必要ってあったっけ?」という感想を持ってしまいます。なにぶん、2006年ごろに作ったものなので、自分で自分のプログラムを見ても違和感がものすごいですね。「OSの処理に対する不信感」がそこかしこに漂っています、、、、

AppleScript名:AS Publisher v19.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2014/11/09
–  Modified on: 2021/11/07
—
–  Copyright © 2006-2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property quotChar : string id 34

property headerCol : "0000CC" –"0000CC" –ヘッダー部分(濃い色)
property bodyBackCol : "EEFFFF" –"EEFFFF" –Script本文下地(薄い色)
property footerCol : "66FFFF" –"66FFFF" –スクリプトリンク部分

set a to path to me

set pName to "com.apple.ScriptEditor2"

using terms from application "Script Editor"
  tell application id pName
    if (count every document) = 0 then
      display dialog "スクリプトをエディタでオープンした状態で実行してください。" buttons {"OK"} default button 1 with icon 1
      
return
    end if
    
    
set aP to path of front document
    
if aP = "" then
      display dialog "スクリプトを保存してから実行してください。" buttons {"OK"} default button 1 with icon 1
      
return
    end if
    
    
tell front document
      set aInfo to properties
      
set curLang to name of language of aInfo –現在のOSA言語の名称を取得する
    end tell
    
    
–OSA Language名称をもとに色セットを変更する
    
changeColor(curLang) of me
    
    
set c to name of front document
    
    
set aF to retMacOSpathList(aP) of me
    
set htmlFilePath to retHTMLpath(aF) of me
    
    
set contText to contents of front document
    
set encText to makeEncodedScript(contText) of me
    
    
set newLinkText to "applescript://com.apple.scripteditor?action=new&script=" & encText
    
–set insLinkText to "applescript://com.apple.scripteditor?action=insert&script=" & encText
    
–set apndLinkText to "applescript://com.apple.scripteditor?action=append&script=" & encText
    
    
set comText to description of front document
    
    
set textList to every attribute run of front document
    
set textList_ref to a reference to textList
    
    
set colorList to color of every attribute run of front document
    
set colorList_ref to a reference to colorList
    
    
set aTest to contents of item 2 of textList
    
set asciiTest to id of aTest
  end tell
end using terms from

set tabChar to string id 9

–set TIDsList to {{"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"¥"", "&quot;"}}
set TIDsList to {{"\\", "\"}, {"’", "’"}, {"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"  ", "  "}, {string id 13, string id 10}, {string id 13, "<br>"}, {string id 10, "<br>"}, {"\"", "&quot;"}}
–set TIDsList to {{"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"¥"", "&quot;"}}

set TIDsList_ref to a reference to TIDsList

set dataOut to {}
set dataOut_ref to a reference to dataOut

set iCounter to 1
repeat with i in textList_ref
  set j to contents of i
  
  
set curDelim to AppleScript’s text item delimiters
  
repeat with eachItem in TIDsList_ref
    set AppleScript’s text item delimiters to contents of item 1 of eachItem
    
set j to every text item of j
    
set AppleScript’s text item delimiters to contents of item 2 of eachItem
    
set j to j as string
  end repeat
  
set AppleScript’s text item delimiters to curDelim
  
  
set cText to RBG2HTML(item iCounter of colorList_ref) of me
  
  
set the end of dataOut_ref to "<font color=" & cText & ">" & j & "</font>"
  
set iCounter to iCounter + 1
end repeat

set htmlHeader to "<table width=" & quotChar & "100%" & quotChar & " border=" & quotChar & "0" & quotChar & "cellspacing=" & quotChar & "2" & quotChar & " cellpadding=" & quotChar & "2" & quotChar & ">
<tr>
<td bgcolor=\"#" & headerCol & "\"><font color=" & quotChar & "#FFFFFF" & quotChar & ">" & curLang & "名:" & c

if comText is not equal to "" then
  set comText to "<br><font size=" & quotChar & "2" & quotChar & ">【コメント】 " & comText & "</font><br>"
end if

set htmlHeader2 to "</font></td>
</tr>
<tr>
<td bgcolor=\"#" & bodyBackCol & "\"><font size=\"3\">"

set htmlFooter1 to "</font></td>
</tr>
<tr>
<td bgcolor=\"#" & footerCol & "\"><p><font size=\"2\"><a href=\"" & newLinkText & "\">★Click Here to Open This Script</a> </font></p>
</td>
</tr>
</table>
"

set dataText to htmlHeader & comText & htmlHeader2 & (dataOut as text) & htmlFooter1
set dataText to dataText as Unicode text

–set dDir to (path to desktop as text) & "index.html"
–write_to_file(dataText, htmlFilePath, false) of me
–saveInEncoding(dataText, htmlFilePath, "UTF8") of me –UTF8
write_to_file_Safely(dataText, htmlFilePath) of me

tell application id "com.apple.ScriptEditor2"
  display dialog "HTML書き出し完了" buttons {"OK"} default button 1 with icon 1 giving up after 1
  
return
end tell

on retHTMLpath(aF)
  tell application "Finder"
    –set aF to retMacOSpathList(aP) of me
    
set afA to aF as alias
    
set aInfo to info for afA
    
set aExt to name extension of afA
    
    
set aFileName to name of afA
    
set aFileName to aFileName as text
    
set aFileName to aFileName as Unicode text
    
    
set aFileName_rev to reverse of (every character of aFileName) as text
    
set parentF to ((parent of afA) as alias) as text
    
set htmlFileName to name of afA
    
set htmlFileName to htmlFileName as text
    
set htmlFileName to htmlFileName as Unicode text
    
    
    
if aExt is not equal to "" then
      set htmlFileNameText to ((reverse of (items ((length of aExt) + 2) thru -1 of aFileName_rev)) as text) & ".html"
    else
      set htmlFileNameText to (aFileName as text) & ".html"
    end if
    
set htmlFileNameFullPath to parentF & htmlFileNameText
  end tell
  
return htmlFileNameFullPath
end retHTMLpath

on makeEncodedScript(contText)
  (*

  set aList to every paragraph of contText
  set aClass to class of aList
  if aClass = list then
    set aLen to length of aList
  else
    set aLen to 1
  end if
  
  set aaList to {}
  
  set delim to AppleScript’s text item delimiters
  set AppleScript’s text item delimiters to "//piyomaru_replacement_temp//"
  set bList to aList as text
  set AppleScript’s text item delimiters to delim
  
  set aaList to (encodeURL(bList) of me) as Unicode text
  
  set search_string to "//piyomaru_replacement_temp//" as Unicode text
  set replacement_string to "%0A" as Unicode text
set bList to replace_chars(aaList, search_string, replacement_string) of me
*)

  
  
set aaList to (encodeURL(contText) of me) as Unicode text
  
  
return aaList
end makeEncodedScript

–RGB値からHTMLの色指定に変換
on RBG2HTML(RGB_values)
  — NOTE: this sub-routine expects the RBG values to be from 0 to 65536
  
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  
set the the hex_value to ""
  
repeat with i from 1 to the count of the RGB_values
    set this_value to (item i of the RGB_values) div 256
    
if this_value is 256 then set this_value to 255
    
set x to item ((this_value div 16) + 1) of the hex_list
    
set y to item (((this_value / 16 mod 1) * 16) + 1) of the hex_list
    
set the hex_value to (the hex_value & x & y) as string
  end repeat
  
return ("#" & the hex_value) as string
end RBG2HTML

–安全なファイルの書き出し
on write_to_file_Safely(this_data, target_file)
  tell current application
    set finalPath to target_file
    
    
–テンポラリフォルダに一度書き出して、ターゲットのパスに移動  
    
set tmpFol to path to temporary items from system domain
    
set tmpName to do shell script "date +%Y%m%d%H%M%S"
    
set tmpPath to (tmpFol as string) & tmpName
    
    
–テンポラリのパスにファイル書き出し
    
try
      set the target_file to the tmpPath as text
      
set this_data to this_data as Unicode text
      
      
set the open_target_file to open for access file target_file with write permission
      
set eof of the open_target_file to 0 –つねにoverwrite mode(appendではなくて)
      
write this_data to the open_target_file starting at eof as «class utf8»
      
close access the open_target_file
      
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
    
    
–ファイルの移動を行う
    
set tFilePOSIX to quoted form of POSIX path of target_file
    
set fPathPOSIX to quoted form of POSIX path of finalPath
    
set sText to "mv " & tFilePOSIX & " " & fPathPOSIX
    
try
      do shell script sText
    on error error_message
      return error_message
    end try
    
    
return true
  end tell
end write_to_file_Safely

on replace_chars(this_text, search_string, replacement_string)
  set AppleScript’s text item delimiters to the search_string
  
set the item_list to every text item of this_text
  
set AppleScript’s text item delimiters to the replacement_string
  
set this_text to the item_list as string
  
set AppleScript’s text item delimiters to ""
  
return this_text
end replace_chars

on encodeURL(origStr as string)
  set aStr to current application’s NSString’s stringWithString:origStr
  
set encodedStr to aStr’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s alphanumericCharacterSet())
  
return encodedStr as string
end encodeURL

on retMacOSpathList(aFile)
  set aPath to POSIX file aFile
  
set aPath to aPath as alias
  
set aPath to aPath as string
  
set aPath to aPath as Unicode text
  
set aliasFileList to aPath
  
return aliasFileList
end retMacOSpathList

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  tell current application
    try
      set the target_file to the target_file as text
      
set the open_target_file to open for access file target_file with write permission
      
if append_data is false then set eof of the open_target_file to 0
      
write this_data to the open_target_file starting at eof
      
close access the open_target_file
      
return true
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
  end tell
end write_to_file

on changeColor(aLang)
  if aLang = "AppleScript" then
    set headerCol to "0000CC" –"0000CC" –ヘッダー部分(濃い色)
    
set bodyBackCol to "EEFFFF" –"EEFFFF" –Script本文下地(薄い色)
    
set footerCol to "66FFFF" –"66FFFF" –スクリプトリンク部分
    
  else if aLang = "JavaScript" then
    set headerCol to "804000" –"0000CC" –ヘッダー部分(濃い色)
    
set bodyBackCol to "E2D3D3" –"EEFFFF" –Script本文下地(薄い色)
    
set footerCol to "E7AC53" –"66FFFF" –スクリプトリンク部分
  end if
end changeColor

★Click Here to Open This Script 

Posted in OSA Text | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy Script Editor Script Menu | 15 Comments

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 13.6.5 AS系のバグ、一切直らず
  • CotEditorで2つの書類の行単位での差分検出
  • Apple純正マウス、キーボードのバッテリー残量取得
  • macOS 15, Sequoia
  • 初心者がつまづきやすい「log」コマンド
  • ディスプレイをスリープ状態にして処理続行
  • 指定のWordファイルをPDFに書き出す
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • メキシカンハットの描画
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • 2023年に書いた価値あるAppleScript
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • スクリプトエディタで記入した「説明」欄の内容が消えるバグ

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (206) 13.0savvy (163) 14.0savvy (113) 15.0savvy (90) CotEditor (64) Finder (51) iTunes (19) Keynote (115) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (19) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (72) Pages (53) Safari (44) Script Editor (26) 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
  • 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)
  • 未分類

アーカイブ

  • 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