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

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

Microsoftが新OutlookにAppleScript機能を追加表明

Posted on 12月 9, 2021 by Takaaki Naganoya

Outlook: Support for AppleScript in the New Outlook for Mac
Planned Availability: June CY2022 https://t.co/bGOxW8leZG #M365Roadmap #Outlook #NewRoadmapItem

— Microsoft365Roadmap (@M365Roadmap) December 8, 2021

Microsoft 365のロードマップを流布するTwitterアカウント@M365Roadmapが、Mac用のOffice365(ローカルで動かす版のOffice、月単位契約可)の新たなロードマップとして、新バージョンのOutlookに対して、2022年の6月を目処にAppleScript対応機能を追加することを発表しました。

Microsoft Outlook for Macは企業内のExchangeサーバーにアクセスするクライアントで、筆者の手元にある(契約していないので置いてあるだけ)バージョンをアップデートしたところ、バージョン16.55でAppleScript用語辞書もついています。

Microsoftが言うところの「新Outlook」というのは、バージョン17.xxというものなのか、あるいはそれ以上のバージョン番号がついたものなのかは分かりませんが、Outlook v16.xx系を使っていればAppleScriptサポートはあるので(Exchangeに用がないのでOutlookのScriptingしたことないですけど)、それまで16.xx系を使い続けてもよいのでしょう。

また、本件はあくまで計画段階の話なので、確度のほどは不明です。

Posted in news | Tagged 11.0savvy 12.0savvy Outlook | Leave a comment

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

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

Amazon EC2 M1 Macインスタンスが利用可能に

Posted on 12月 3, 2021 by Takaaki Naganoya

Amazon AWSでMacインスタンスが利用可能になって、約1年。当初はMacインスタンスにはMac mini 2018(Intel Mac)が採用されていましたが、M1 Mac miniを採用した「Amazon EC2 M1 Macインスタンス」が本日から利用可能になったと報じられています。

「利用可能なM1 Mac miniのスペックは8CPUコア、8GPUコア、16GBメモリ、16コアApple Neural Engine。これがThunderbolt経由で10GbpsのVPCネットワークと8GB EBSストレージに接続されています。」

とのこと。OSバージョンが書かれていませんが、macOS 11.xか12.xということになるでしょう。

サービス開始当初からM1 Macに言及していた割に提供が1年遅れたことについては、やはり「(AppleScriptや)shell scriptのようなプロセスを決め打ちで遅い高効率コア(IceStorm)で実行してしまう」というmacOS 11.x+M1の問題点(バグといってもいい)がmacOS 12で解消されるのを待っていたというところでしょうか?

ただ、基本単位が24時間で、それを超える分については1秒単位の課金。「瞬間的にMacを大量に用意して大量のデータを処理したい!」という用途にはいまひとつ(費用面で)使いにくいサービスでもあります。

最小単位が1時間とかだったら、いろいろ使ってみようという気になりますが、AppleのEULAをクリアするためにこのようなサービス形態になっているとかいないとか。

あらかじめ起動ディスクイメージを用意しておいて、必要なときに新規インスタンスをこれで起動し、所定の処理を行ったあとでデータをストレージに書き戻すとかいった使い方になることでしょう。

最小単位が24時間というのが、かえすがえすも……。

AppleScriptでこうしたサービスを利用する機会があるのかと言われれば、膨大な量のPDFを処理する必要がある場合などに、一時的にAWS上のインスタンスをかき集めて一気に処理するといったものが考えられるでしょう。

膨大なKeynoteやPagesのデータを処理する必要があって、そのデータ処理を行う……というケースもないことはなさそうですが、別にAWSを利用しなくても処理できそうな気もします(データ量次第です)。

Posted in news | Tagged 11.0savvy 12.0savvy AWS | Leave a comment

FM-1グランプリで仕事効率化部門の部門準賞を獲得!

Posted on 11月 28, 2021 by Takaaki Naganoya

FileMaker Proの「すごいデータベース」のコンテストであるFM-1グランプリにおいて、Piyomaru Softwareの応募作品「FileMaker PowerPack」が、予選を通過し、決勝において仕事効率化部門の部門準賞を獲得いたしました。

FileMaker PowerPackは、CotEditor PowerPackと同様、Cocoa ScriptingのパワーをFileMaker Proユーザーに体感していただく技術的なデモンストレーションであり、かつ日常的にデータベース用のデータ作成などの作業に従事する人たちが便利と感じる(のではないか)という内容のScriptを収録したScriptサンプルデータベースに仕上がっています。

データベース上にサンプルデータを入れ、その場で実行して結果を確認できるようになっています。

内容はあっさり読めるように書いてあるものが多いので、入門用に見えてしまうかもしれませんが……実際にAppleScriptを「書ける」人間が読むと顔をひきつらせるような内容のものが多々入っています。作った本人としては「これを見て書けるようになるとは思わない」というレベル設定です。超絶レベルではないものの、アクロバティックなものが多いので。

技術的にも度肝を抜くような内容であり、178本のScriptを収録してはいるものの、本作品はあくまでFileMaker Pro上で動作するAppleScriptのコードなわけで、「ド派手で巨大で存在感はあるものの、さすがに大賞はとらないだろう」という味付け。本作品のレビュー記事は、文字数からBlog側で単純計算した読了所要時間が23分という大作です(通常の8倍のボリウム)。

それでも、FM-1グランプリの運営方針である「きちんと内容をチェックする詳細なレビュー」が行われおり、日本国内のFileMaker Proコミュニティのレベルを内外に示したという意味において、よそで真似できない「まっとうな」骨太のコンテストであったことを痛感。運営に携わられたお3方、関連スポンサー企業の方々に敬意を表します。

というわけで、このFileMaker PowerPackの書き換え方法や利用方法などを解説した「FileMaker PowerPack活用ガイド」を近日発刊予定です。技術的なレベルを下げて解説し、久しぶりにAppleScriptにさわるという方向けにレベル設定しています。完全理解というレベルでなくても、必要な書き換えはできるだろうという内容を目指しています。

そして、自分で作った作品の作者自身の解説のはずなのに、自分で書いていてとても大変です。「誰だ、こんなもの作ったのは?」という悪態をついてしまうほどですが、それは自分のことなので自業自得というところです。

Posted in news | Tagged FileMaker Pro | 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

集中モード(Don’t Disturb Mode)に設定する

Posted on 11月 16, 2021 by Takaaki Naganoya

macOS 12.x上で集中モード(Don’t Disturb Mode)に設定するAppleScriptです。

Shortcutsにその機能があるので、AppleScriptから呼ぶことになるでしょう。使っていると「日本語AppleScript」みたいな違和感の塊のようなショートカット.app。「おやすみモードをオフ時までオンにする」とか、無理に日本語にしないで英語にしてあったほうがまだ分かりやすいと思います。

この「集中モード」という機能についても、画面上で「おやすみモード」と表現されていたり「集中モード」と表現されていたり、いまひとつ機能を表す言葉のレベルに戸惑いを感じます。それとも、自分が何か根本的に勘違いしている箇所があるものでしょうか。

インストール+実行ルーチンを呼ぶことで、このショートカット・ワークフローがMac上に存在していなければインストールし、呼び出しを行います。


▲実行前


▲実行後

AppleScript名:Don’t Disturb Shortcuts v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/01
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

set aName to "PIYO_Don’t Disturb" –Shortcut名称
set aURL to "https://www.icloud.com/shortcuts/cf960093816e4c7fb8d23c10e8820790" –共有URL
set sRes to installAndRunShortcuts(aName, aURL) of me

–Install Shortcut and run it
on installAndRunShortcuts(aName, aURL)
  tell application "Shortcuts Events"
    set n1List to name of every shortcut
  end tell
  
  
if n1List does not contain aName then
    display notification "Plese Install my Shortcuts workflow"
    
open location aURL
  end if
  
  
repeat 100 times
    tell application "Shortcuts Events"
      set n2List to name of every shortcut
    end tell
    
    
if n2List contains aName then
      exit repeat
    end if
    
    
delay 0.5
  end repeat
  
  
if n2List does not contain aName then
    display dialog "I can’t run my Shortcuts because you canceled installing"
    
return false
  end if
  
  
  
tell application "Shortcuts Events"
    run shortcut aName
    
return true
  end tell
end installAndRunShortcuts

★Click Here to Open This Script 

Posted in System | Tagged 12.0savvy Shortcuts Shortcuts Events | Leave a comment

FM-1グランプリにFileMaker PowerPackを出品

Posted on 11月 15, 2021 by Takaaki Naganoya

FileMaker Proの作品コンテスト「FM-1グランプリ」の第2回予選を通過した「FileMaker PowerPack」を、内容拡充し第3回予選に再応募しました。

CotEditorのPowerPackにつづく、FileMaker ProのPowerPack。178本のAppleScriptを収録した、FileMaker Proに組み込んで動かすと便利そうなものを収録したサンプル集です。

Cocoa Scriptingのデモンストレーションを行うフリー配布ソリューションです。

FileMaker ProとAppleScriptなので、相性抜群! 楽勝で作れるに違いない!

……そんなことを考えていた時期が自分にもありました。

しかし、割と前代未聞のトラブルに遭遇。苦労話にはことかかないありさまです。だいたい、内容がなかなか決まらずに企画段階でかなり困っていました。

ここでは、お気楽そうに見える見た目とはまったく異なる技術的な内容について記すこととします。

FM独自のAppleScriptランタイム環境が大変

FileMaker ProのAppleScriptランタイム環境は独特のもので、ランタイム名を取得しても「FileMaker Pro」と独自の名前が返ってきます。Vanilla AppleScriptについては環境の違いにともなう「差異」は感じないところですが、Cocoa Scriptingを行うと「まったくの別物」であることを痛感できます。

FileMaker Pro内蔵のスクリプトステップ「AppleScriptを実行」は、実行したAppleScriptが生成したオブジェクトがメモリ上に常駐したりしません。実行後、即座にメモリ上からパージされるようです。ARC的な「使わなくなって時間がたったから自動で消しとくね!」という挙動とはまるっきり違います。

このことで、OS側から各種Notificationを受信するようなもの(USBメモリがマウントされたらAppleScriptを実行させるなど)は、瞬時にクラッシュ。NSTimerでタイマーを生成してタイマ割り込み処理を行おうとするとクラッシュ!

さらに、標準的なランタイム環境(スクリプトエディタ、Script Menu)で実行したときには発生しないトラブルが山のように発生します。

とくに、CoreImageの機能を呼び出して画像処理を行おうとした場合には、明示的にメインスレッドでの処理を指定する必要があり、これに気づくまではまったく画像フィルタ処理ができない始末。FileMaker PowerPackでは無駄にCoreImageのフィルタ呼び出しプログラムばかり60本ぐらい突っ込んでありますが、このCoreImageの処理で問題のある挙動を示すものがないか全数チェックしたという次第です。

Cocoa Scriptingについては、おそらくメーカー側でもノーチェックの部分なので、調べられる機会に調べておいたほうが得策です。

今回のFileMaker PowerPackでいろいろ余計なノウハウがたまりましたが、osascriptなどのランタイム環境と比べてもずいぶん風変わりなCocoa Scripting環境であることを意識しておく必要があることでしょう。

FileMaker PowerPackには目下178のScriptを入れてあり、FileMaker Pro DB上のサンプルデータをもとにデータ処理を行い、結果がある場合にはDB上に結果を描き戻すようになっています。

今回、さまざまなテストを重ね、「極力FileMaker Pro内蔵のAppleScript実行環境を利用しない」ことが問題解決の近道であることを痛感しました。できるだけ、FileMaker Proの外部に補助アプレットを用意して、そちらでCocoa Scriptingの処理を行うべきです。まして、タイマー処理やNotificationの受信処理などはFileMaker Pro内では行えないため、FM内では最低限の処理にとどめ、処理本体を外部に出すべきでしょう。

macOS 12のFramework再編の影響も

macOS 12で行われたFramework再編の影響も受けました。PDFKitの機能を利用する場合、macOS 11までであれば、

use framework "QuartzCore"

と記述していましたが、macOS 12では、

use framework "PDFKit"

と記述する必要があります。

macOS 11までのOSと、macOS 12以降のOSにそれぞれ対応するライブラリを書いて、実行時のOSバージョンを検出して呼び出しを切り替える処理をスクリプトエディタ上で動作確認してあったのですが、これをそのままFileMaker Proのランタイムに持っていくとコンパイル(構文確認)をパスしません。

仕方なく、macOS 11までのScriptと、macOS 12以降のScriptで別々のものに分ける必要がありました。

自称・世界最小の簡易形態素解析エンジン「easyJParse」のFM環境への移植

さまざまなランタイム環境で動作する必要のある、Piyomaru Softwareによる世界最小の簡易形態素解析エンジン「easyJParse」を、このFileMaker PowerPackに組み込んでいます。

このeasyJParseは初出時には80行程度、現在でも100行そこそこという超小型の形態素解析プログラムであり、自然言語(主に日本語)によるコマンドを分解するために開発したものです(飛行機の中でも動かすべく、REST APIで外部のサービスを呼び出したりはしていません)。

当初、easyJParseがFileMaker Proランタイム上で動かず、けっこう焦りましたが……いつもの「ソート方向指定」の「ascending」とか「descending」などのキーワードがFMの予約語と衝突していただけでした。

このeasyJParseが動くことで、FileMaker Pro上でTanzaku的な自然言語によるコマンド指定ソリューションが動きまくるはずですが、32KBまでのScriptしかホスティングできないという制限が地道に大変そうであります。

FM-1 GPにFileMaker選手権2021と結果発表が相次ぐ

ウチの検証環境(Intel+M1 Mac mini)でテストした範囲では問題ないことを確認していましたが、どうもM1 MacBook+Retina Display環境では検証できないので、その環境ではいろいろ問題に遭遇していたようです。問題の洗い出しを行えたことは前向きにとらえるべきですが、その環境がないので事前にチェックすることは不可能です。

# M1のRetina Display+MacBook環境が手元にないのでコンテストに応募したわけで…

肝心の総合結果発表は11月27日。大賞を取ることは難しそうですが、予選を通過したため何かいただけそうです。噂によればFileMaker選手権2021も近い日程で結果が発表されるとかしないとか。こちらにも応募はしているため、せめて参加賞ぐらいはいただきたいものです(モバイルバッテリーだったか)。

FileMaker PowerPackについては、その内容を解説し、書き換えるためのガイドを行う書籍を近日発行予定です。さらに、Cocoa Scripting的な立ち入った解説を行うものも計画しています。

これらの付録に、「FM-1グランプリ戦記」をつける予定です。

Posted in news | Tagged 12.0savvy FileMaker Pro | 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

FileMaker Pro Scripting Book with AppleScriptをアップデート

Posted on 11月 4, 2021 by Takaaki Naganoya

FileMaker ProのAppleScriptからの操作を詳細に解説した電子書籍「FileMaker Pro Scripting Book with AppleScript」(日本語版)をアップデートしました。

macOS 12に合わせた内容のアップデート、v1.2までに説明されていなかった内容の補完、および別冊付録リファレンスをまとめています。

すでに購入された方は、購入ページからの再ダウンロードで入手可能です。

→ FileMaker Pro Scripting Book with AppleScript オンラインブックストア(BOOTH)

Posted in Books news PRODUCTS | Tagged 12.0savvy FileMaker Pro | Leave a comment

Shortcuts Eventsでショートカットのインストール+実行

Posted on 11月 1, 2021 by Takaaki Naganoya

macOS 12で標準搭載された「ショートカット.app」(Automatorを置き換える簡易ワークフロー構築・実行ツール)を操作して(実際にはGUIなしのShortcuts Events.app)、自分がiCloud上で共有しているショートカット・ワークフローのインストールと実行を行わせるAppleScriptです。

ショートカット・ワークフローはローカルではなくiCloud上に置かれています。それらは「名前」で識別できますが、ユニークなIDが振られているわけではありません。ものすごく不満ですが、一応識別できることにしましょう。

ショートカット・ワークフローは「共有」することが可能で、とくに「リーディングリストで共有」すると、iCloud上の生URLが得られる(SafariのURL欄に表示される)ため、このURL情報を共有すれば「指定のショートカット・ワークフローが存在していない場合にはiCloud上からインストールさせる」という処理が可能になります。

# このあたり、まだ試行錯誤中なので「現時点ではこういう運用をしている」という程度です

今日確認したら、「ファイル」メニューの「共有」に「iCloudリンクをコピー」という項目がありました。

リンクをコピーする機能を入れるなら「編集」メニューではないのかと思わないではないですが、機能的にはこちらがシンプルなのでご利用ください。

お使いの環境に指定のショートカット・ワークフローがインストールされていない場合には、インストールを求めます。ただ、単なる「名称」でしかチェックしていないのと、あらかじめ「同じ名前で内容が違うショートカット」が存在していたら、「インストールずみ」と判定してしまうので、一意に識別できるユニークな識別子がショートカット・ワークフローについていないのはどうかと思います。入手先のiCloud URLでもよいのですが、、、、

# 一応、コンピュータ上で互いを識別するための「id」は持っています

ここで、インストールを行わなかった場合には、それを検出してダイアログ表示します。

AppleScript名:Shortcuts Eventsでショートカットのインストール+実行.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/01
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

set aName to "ASTEST3" –Shortcut名称
set aURL to "https://www.icloud.com/shortcuts/e788482535704922aaa9f617491353b2" –共有URL
set sRes to installAndRunShortcuts(aName, aURL) of me

–Install Shortcut and run it
on installAndRunShortcuts(aName, aURL)
  tell application "Shortcuts Events"
    set n1List to name of every shortcut
  end tell
  
  
if n1List does not contain aName then
    display notification "Plese Install my Shortcuts workflow"
    
open location aURL
  end if
  
  
tell application "Shortcuts Events"
    set n2List to name of every shortcut
  end tell
  
  
if n2List does not contain aName then
    display dialog "I can’t run my Shortcuts because you canceled installing"
    
return false
  end if
  
  
tell application "Shortcuts Events"
    run shortcut aName
    
return true
  end tell
end installAndRunShortcuts

★Click Here to Open This Script 

Posted in Shortcuts Workflow | Tagged 12.0savvy Shortcuts Shortcuts Events | 1 Comment

ショートカットのプロパティで返ってくるアイコンをダイアログ表示

Posted on 10月 29, 2021 by Takaaki Naganoya

macOS 12であらたに搭載されたショートカット.appのショートカット・ワークフローのアイコンを読み取ってダイアログ表示するAppleScriptです。

Shortcuts.appおよびShortcuts Events.app経由で各ショートカット・ワークフローの情報を取得することができます。

tell application "Shortcuts Events"
  set aList to properties of shortcut 1
  
–> name:"ASTEST", class:shortcut, accepts input:false, action count:1, subtitle:"1個のアクション", folder:missing value, color:{62105, 22734, 24226}, computer name:«data TIFF4D4D002A00040008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 …..» id:"5CEF527E-F2E7-42A4-9513-209F098E50E9"}
  
end tell

★Click Here to Open This Script 

ここで、「computer name」と解釈されているのがショートカット・ワークフローのアイコンです。このあたりバグくさいところですね。実際には「icon」という属性ラベルなのですが、、、、

iconデータの内容は最初の文字を見れば丸わかりのTIFF画像です。

手っ取り早く変換したかったので、クリップボード経由でNSImageに変換して、ダイアログに表示してみました。

この手の「ちょっとしたビューをアラートダイアログに入れてデータ表示」を行うのは、すでにノウハウがたまっていて気楽に行えるのが2021年的なAppleScriptの利用状況です。

とりあえず、すべてのショートカット・ワークフローのアイコンを取得することはできるのですが、全部確認するのはかったるいので冒頭の10個だけ表示しています。

–> Watch Demo Movie

AppleScript名:ショートカットのプロパティで返ってくるアイコンをダイアログ表示.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/10/27
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSAlert : a reference to current application’s NSAlert
property NSImage : a reference to current application’s NSImage
property NSImageView : a reference to current application’s NSImageView
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSImageScaleProportionallyUpOrDown : a reference to current application’s NSImageScaleProportionallyUpOrDown

property returnCode : 0

tell application "Shortcuts Events"
  set aCount to count every shortcut
  
if aCount > 10 then
    set aCount to 10 –Too dull to see every icons…. If you want whole icons, remove this if-then block
  end if
end tell

repeat with i from 1 to aCount
  tell application "Shortcuts Events"
    set aProp to properties of shortcut i
    
set aCon to icon of aProp
    
set aName to name of aProp
  end tell
  
  
–クリップボードの内容をNSImageに
  
set the clipboard to aCon
  
set aNSIMage to my getClipboardASImage()
  
  
my dispNSImage(aNSIMage, aName)
end repeat

on dispNSImage(aNSIMage, aTitle)
  set aMessage to "Your NSImage"
  
set paramObj to {myMessage:aTitle, mySubMessage:"This is an image", myImage:aNSIMage}
  
my performSelectorOnMainThread:"displayImageView:" withObject:(paramObj) waitUntilDone:true
  
–my displayImageView:paramObj–for debugging with Script Editor
end dispNSImage

on displayImageView:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set anImage to (myImage of paramObj)
  
  
set imgSize to (anImage’s |size|())
  
set imgWidth to ((imgSize’s width) as real)
  
set imgHeight to ((imgSize’s height) as real)
  
  
set anImageView to NSImageView’s alloc()’s initWithFrame:{origin:{x:0.0, y:0.0}, |size|:{width:imgWidth, height:imgHeight}}
  
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 setIcon:anImage
    
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 displayImageView:

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

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

★Click Here to Open This Script 

Posted in Shortcuts Workflow | Tagged 10.12savvy Shortcuts Shortcuts Events | Leave a comment

AppleScript初期プロダクトマネージャーWilliam Cook氏が死去

Posted on 10月 29, 2021 by Takaaki Naganoya

I'm devastated to hear that William Cook passed away on Wed. Much of 1990s OOP was defined by his seminal papers. When he returned after a decade in industry (AppleScript!) I invited him to @BrownCSDept (where he got his PhD from Peter Wegner) and we became friends. Tragic. pic.twitter.com/SwiPvZZgd2

— ShriramKrishnamurthi (@ShriramKMurthi) October 28, 2021

Posted in news | Leave a comment

Skim v1.6.5+macOS 12.0.1でPDFに埋め込んだapplescript://リンクの動作を確認

Posted on 10月 28, 2021 by Takaaki Naganoya

macOS 10.15で「PDFView上で発生した(カスタムURLスキームの)URLイベントを途中でブチ切る」という変更を、ユーザーに何の断りもなく行ったApple。

本Blog掲載のプログラムリスト末尾に入っている「applescript://」URLリンク。Webブラウザ(HTML)→スクリプトエディタの間では、このURLリンクは有効です。この仕様を引き継いで、AppleScript解説本のPDFでも同様のURLリンクを入れてあったのですが……これがmacOS 10.15で意図的にブロックされました。

URLリンクの長さが規定の文字数を超えるものは途中で切ったり、リンクをそもそも無効にしたりと、AppleはmacOS 10.15.x上でマイナーアップデートのたびに悪のかぎりを尽くしていました。90年代の傍若無人なマイクロソフトを想起させるような、強引すぎるやり方です。

こういうのはとてもよくないことだと思います。PDFの扱いがいいからmacOSを使っている、という部分は大きいわけですが、その基盤となるPDFの扱いをいろいろ無断で変更するというのは、感心しません。

macOS 10.15はβ段階から出来が悪く、「こんなのに付き合っていられるか!」と10.14を継続使用することを決め、10.15をガン無視することにしました。

macOS 10.15は「リリースしてはいけない」レベルのOSだと感じました。このあたり、ダメなものにストップをかけられないのは、Appleという会社の構造的な問題なんでしょう(イエスマンばっかりやがな)。

macOS 10.15を無視したことは個人の生産性を損なわなかったという意味では「いいこと」でしたが、macOS 10.5で発生していたさまざまな問題を自分が認識しなかった(そういうレベルまで付き合う気がしなかった)ために、問題の発覚が遅れたという副作用もありました(まさか、Cocoa Scriptingの実行速度が大幅に低下していたとは!>macOS 10.15)。

macOS 10.15におけるPDF埋め込みリンクについては、macOS標準添付のPreview.appであろうがサードパーティの(オープンソースの)Skim PDF Viewerだろうが、自分でXcode上で作ったお気軽PDFブラウザだろうが、PDFViewを使っている限りはこの制限が発生していました。

真剣にサードパーティのPDF関連Frameworkの利用も検討しましたが、価格面でなかなか折り合いが合わなかったのと、PDFViewに相当するクラスが提供されていなかったのでダメでした。

本件についてはAppleにレポートしていましたが、Apple側でガン無視状態(いろいろリクエストしても無視されるのがデフォルト状態)。サードパーティのPDFビューワまで影響が出るため、macOS 10.15を使わないという消極的な対策しかできない状況でした。

ちょうどこの時期に執筆していたMusic.appのScripting本が完全に頓挫するなど、少なからず打撃を受けた出来事だったのです(各種既刊本のアップデート計画も白紙撤回していました)。

その後、Piyomaru SoftwareのScripting本については「本誌掲載Scriptは別途Zipアーカイブを添付」という原始時代のやり方にたちかえることになります。

そんな中、ふとmacOS 12.0.1+Skim v1.6.5で冗談半分に「URLリンクが復活してたりして」と試してみたら……………「!!!!!』

なんと、Skim上では「applescript://」URLリンクが正常に動作して、内容が途切れることなくスクリプトエディタに転送されていました。さすがに、追加試験でPreview.app上で実行してみたらリンクをまるごとブロックされましたが、Skimについては大丈夫なようです。

こっそり禁止して、こっそり修正ということなんでしょうか。現状がこのまま維持されるのか、それとも将来にまた仕様が変更されるのか不明ですが、コソコソ仕様を変えられるのはとても不愉快です。

OSのRelease Notesをちゃんと出さないことは、不満です。

Posted in news PDF | Tagged 12.0savvy | Leave a comment

AppleScriptからショートカット実行&ショートカット内でAppleScriptを実行

Posted on 10月 27, 2021 by Takaaki Naganoya

macOS 12で新規搭載された「ショートカット.app」、このアプリケーション上で記述するワークフローを「ショートカット」、ショートカットを構成する各コマンドを「アクション」と呼びます。

ショートカット.appは日本語ローカライズされた名称であって、実際には「Shortcuts.app」という名前です。

AppleScript用のショートカット実行GUIなしアプリケーション「Shortcuts Events.app」が別途(ショートカット.appのバンドル内に)存在しており、こちらもAppleScript用語辞書を備えています。

Shortcuts.appに対してコマンドを実行すると、同アプリケーションが起動します。Shortcuts Events.appに対してコマンドを実行すると、Shortcuts.appは起動しません。

両者とも辞書内容は一緒であり、バージョン番号と名前以外に差異はありません。

AppleScript名:ショートカットのプロパティを取得.scpt
tell application "Shortcuts"
  properties
  
–> {frontmost:false, class:application, name:"Shortcuts", version:"5.0"}
end tell

★Click Here to Open This Script 

AppleScript名:Shortcuts Eventsのプロパティを取得.scpt
tell application "Shortcuts Events"
  properties
  
–> {frontmost:false, class:application, name:"Shortcuts Events", version:"1.0"}
end tell

★Click Here to Open This Script 

ショートカット.appを起動するとこのようなウィンドウが表示されます。

あるいは、このようなウィンドウです。

ショートカット一覧が並んだ表示状態で実行することもできますし、ダブルクリックして個別ウィンドウ表示した状態で実行することも可能です。

また、メニューから実行することも可能です。

環境設定では、以下のような設定が行えます。「詳細」に「スクリプトの実行を許可」の項目があり、デフォルトではオフになっています。当然、ここはオンにします。他の項目についても適宜、自己責任でオンにすることになります。

ショートカット(ワークフロー)はローカルに保存されません。iCloud上に保存されます。

ショートカット(ワークフロー)の共有はコンテクストメニューから行います。このさい、iCloud上のURLが直接表示されないため、少々面倒な感じです。

「リーディングリスト」で共有すると、WebブラウザのURL欄に詳細なURLが表示されるため、これを用いるとよさそうです。

https://www.icloud.com/shortcuts/e788482535704922aaa9f617491353b2

新規ショートカットの作成には、メニューから「ファイル」>「新規ショートカット」(Command-n)を実行するか、ショートカット.appのウィンドウでツールバーの「+」をクリックします(すべてのショートカット、My Shortcutなど選択中のフォルダによって表示/非表示状態が変わります)。

初期状態はこんな(↑)感じです。アクションを選択してダブルクリックするか、ビュー上にドラッグ&ドロップするとアクションが置かれます。

最初なので、ダイアログでも出しておきましょう。

ツールバー上の「▶︎」ボタンをクリックするか、Command-rでショートカット(ワークフロー)を実行します。

ショートカットは名前で識別することになります。ツールバー上の名称を変更すると名前を付けられます。

AppleScript側からはこの名前「AS TEST2」を指定して呼び出すことになります。

AppleScript名:ショートカット.appでショートカット実行.scpt
tell application "Shortcuts"
  run shortcut "AS TEST2"
end tell

★Click Here to Open This Script 

AppleScript名:Shortcuts Eventsでショートカット実行.scpt
tell application "Shortcuts Events"
  run shortcut "AS TEST2"
end tell

★Click Here to Open This Script 

現在のところ、Shortcuts.appならびにShortcuts Events.appのrunコマンドでパラメータを指定すると実行時にエラーになるので、パラメータをAS側からショートカット側に指定できていません。クリップボード経由で指定するとかいってアクロバット技を使えばできないことはありませんが、パラメータを指定できないのは明らかな「バグ」です。

あとは、他のプログラムとの相互運用性を高めるためには、ショートカット側の実行結果をAS側に返してくれることが望ましいです。オブジェクトによっては、sdefを通じてやりとりできないCocoa Object(CLLocationなど)もありますが、結果は得られるべきでしょう。

ショートカット.appについては、ヘルプにけっこうな内容の説明が書かれているので、まずはそちらを読むとよいでしょう。ただ、日本語訳がよくないのか「お前は何を言ってるんだ?」という表現が散見され、説明文というよりも広告みたいな内容の箇所もあります。

Posted in news | Tagged 12.0savvy Shortcuts Shortcuts Events | 12 Comments

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 26, Tahoe
  • macOS 15でも変化したText to Speech環境
  • KagiのWebブラウザ、Orion
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • macOS 15 リモートApple Eventsにバグ?
  • NSObjectのクラス名を取得 v2.1
  • 2024年に書いた価値あるAppleScript
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AVSpeechSynthesizerで読み上げテスト
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • Numbersで選択中の2列のセルを比較して並べ直して書き戻す v2
  • Apple、macOS標準搭載アプリ「写真」のバージョン表記を間違える
  • Script DebuggerがmacOS 15.x上で起動せず→起動

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (156) CotEditor (66) Finder (52) Keynote (119) 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 (56) Pixelmator Pro (20) 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
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • 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年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 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