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

タグ: 15.0savvy

デフォルトタイトルと重なっているものと指定サイズのものを抽出

Posted on 5月 26 by Takaaki Naganoya

Keynote書類上でタイトルと(ボックスの領域が)重なるテキスト、および文字サイズが指定サイズと同じテキストを抽出してまとめるAppleScriptを書いてみました。Keynoteで作成した電子書籍の目次データ確認のためのものです。

実行はmacOS 15.5+Keynote v14.4上で行いましたが、とくにバージョン依存している箇所はないので、異なるバージョンであっても使えるはずです。分析対象の書類をKeynoteでオープンした状態で、解析対象のスライド(複数)を選択した状態でスクリプトエディタ上で実行することを前提にしています。

Keynoteのスライド(ページ)からのテキスト抽出は、Cocoaの機能が利用できるようになってからずいぶんと楽になりました。boundsが重なっているものを演算で判別するのも楽勝です。

以下は、執筆中の最新刊「AppleScript+Xcode GUIアプリソース集」の内容にもとづいています(未完成)。

# 同書の作業中、その暫定タイトル名からmacOS 15.5BetaのAppleScript処理系のクラッシュを誘発したといういわくつきのタイトルでもあります(同バグはリリース時に修正されました)

99ソースの部分はまだ添付Xcodeプロジェクトを確認していないので、「だいたいそのぐらいだろう」という暫定値でもあります。

この状態でウィンドウ左側のカバーページ部分だけを選択すると、selectionでカバーページだけを抽出できます。

カバーページに対して、デフォルトタイトルアイテム(ここでは章番号が入る)と、そのエリアに重なっているテキストアイテムを抽出。

さらに、中央に配置している96ポイントのテキストアイテムを抽出。

あとは、Keynote(iWorks Apps)でよくあるテキストアイテムから抽出したテキストに謎コード(iWork共通のオブジェクト内強制改行)が入っていて改行を解除できないという問題にも対処しています。

また、KeynoteなどのiWork Apps共通の問題点である「オブジェクト同士のIDによる識別ができない」点については、propertiesでまとめて属性値を取得し、この属性値でまとめて判定を行っています。少々遅くなりそうな感じでしたが、実際に試してみたら(マシンが速いので?)問題ありませんでした。

1章 さあ、はじめよう!
基礎的な内容のご説明
2章 ウィンドウ
各種部品を格納する基本的な部品
3章 ボタン
クリック操作を受け付けるもっとも基本的な部品
4章 テキストフィールド
文字や数値などの入力を受け付け、表示を行う
5章 テキストビュー
複数行のスタイルつきテキストを扱える入力・表示部品
6章 デートピッカー
カレンダー情報の入力を受け付ける
7章 イメージビュー
画像(色)の表示とドラッグ&ドロップ受け付け
8章 パスコントロール
フォルダやファイルパスのユーザーによる選択や表示
9章 スライダー
数値の入力、表示
10章 プログレスインジケータ
App Storeに出すなら必須の途中経過表示部品
11章 テーブルビュー
表データの表示、入力用ぜひ使いたい部品
12章 アウトラインビュー
階層化データの入力、表示用
13章 ブラウザ
ツリーデータ、フォルダ階層構造の表示・選択
14章 スプリットビューほか
複数のGUI部品をまとめて使いやすく
15章 メニュー
アプリの機能をひととおり呼び出す階層メニュー
16章 ポップオーバー
ボタンなどからオーバーレイ表示する簡易子ウィンドウ
17章 アドレスブックピープルピッカー
住所録データを表示・選択する
18章 AVキットプレイヤービュー
各種ムービーを再生表示
19章 マップキットビュー
場所と倍率と形式を指定して世界地図を表示
20章 OSAキットビュー
スクリプトエディタ部品でAS編集・実行
21章 PDFキットビュー
PDFとサムネイル表示
22章 ウェブキットビュー
Webコンテンツの表示と操作
23章 サービス
各アプリに対して共通で機能を提供する
24章 ドキュメントベースアプリ
「書類」を扱うアプリ形式
25章 外部アプリコントロール
AppleScriptで他のGUIアプリを操作
26章 スクリプタブル化
アプリ自体を外部からAppleScriptで操作可能に
27章 アプリアイコン
Dock上に表示するアプリアイコンを操作する
28章 ダイアログ
文字入力ダイアログを表示して文字入力
29章 アニメーション
GUI部品をアニメーションさせてユーザーの操作を促す
30章 その他
その他のこまごまとした実用的なノウハウ
あとがき、奥付 Author’s notes
ご愛読、ご協力感謝申し上げます。

すべて自分で作ったデータなので、非常に「ゆらぎ」が小さくなっており、そのデータ品質を前提に作っています。

実際にはもっとデータ品質が低いものと思われるため、そうした低品質データに対処するためには、もっといろいろな処理が必要になってくることでしょう。

処理対象データ(ここではKeynote書類)が事前に予想したとおりの構造(default title item使用)や品質(データのばらつきの存在)になっているかは、AppleScriptを何回か走らせてデータの内容を確認するものでもあるため、最初から一発で想定どおりの処理ができるわけではありません。

AppleScript名:デフォルトタイトルと重なっているものと指定サイズのものを抽出.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/05/26
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set targSize to 96.0 –取り出し対象の文字サイズ
set outList to ""

tell application "Keynote"
  tell front document
    set aSel to selection
    
repeat with i in aSel
      set j to contents of i
      
      
tell j
        set tList to every text item
        
        
set dItem to default title item
        
set dProp to properties of dItem
        
set dText to object text of dItem –テキスト
        
        
set dPos to position of default title item
        
set dHeight to height of default title item
        
set dWidth to width of default title item
        
set dList to dPos & {dWidth, dHeight}
        
        
–座標領域が重なっているオブジェクトを抽出
        
set colList to {}
        
repeat with ii in tList
          set jj to contents of ii
          
set tPos to position of jj
          
set tHeight to height of jj
          
set tWidth to width of jj
          
set ttList to tPos & {tHeight, tHeight}
          
set tProp to properties of jj
          
          
if dProp is not equal to tProp then
            set gCol to getCollision(dList, ttList) of me
            
if gCol = true then
              set the end of colList to jj
            end if
          end if
        end repeat
        
        
–指定フォントサイズのテキストアイテムを抽出
        
set sizeList to {}
        
repeat with ii in tList
          set jj to contents of ii
          
set tSize to size of object text of jj
          
if tSize = targSize then
            set the end of sizeList to jj
          end if
        end repeat
        
        
–出力用テキストの整形
        
set t1 to cleanUpKeynoteTextObj(dText) of me
        
set t2 to cleanUpKeynoteTextObj(object text of item 1 of sizeList) of me
        
set t3 to cleanUpKeynoteTextObj(object text of item 1 of colList) of me
        
        
set outStr to t1 & tab & t3 & return & t2
        
set outList to outList & return & outStr
        
      end tell
    end repeat
  end tell
end tell

return outList

–iWorkのtext itemのオブジェクト内改行を削除
on cleanUpKeynoteTextObj(tObj)
  set tObj to repChar(tObj, string id 10, "") of me –LF
  
set tObj to repChar(tObj, string id 13, "") of me –CR
  
set tObj to repChar(tObj, string id 8232, "") of me –強制改行?
  
return tObj
end cleanUpKeynoteTextObj

on getCollision(aList, bList)
  copy aList to {aXn, aYn, aWn, aHn}
  
copy bList to {bXn, bYn, bWn, bHn}
  
  
set a4Rect to current application’s NSMakeRect(aXn, aYn, aWn, aHn)
  
set b4Rect to current application’s NSMakeRect(bXn, bYn, bWn, bHn)
  
set a4Res to detectRectanglesCollision(a4Rect, b4Rect) of me
  
  
return a4Res
end getCollision

–NSRect同士の衝突判定
on detectRectanglesCollision(aRect, bRect)
  set a1Res to (current application’s NSIntersectionRect(aRect, bRect)) as {record, list}
  
set tmpClass to class of a1Res
  
  
if tmpClass = record then
    –macOS 10.10, 10.11, 10.12
    
return not (a1Res = {origin:{x:0.0, y:0.0}, |size|:{width:0.0, height:0.0}})
  else if tmpClass = list then
    –macOS 10.13 or later
    
return not (a1Res = {{0.0, 0.0}, {0.0, 0.0}})
  end if
end detectRectanglesCollision

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

★Click Here to Open This Script 

Posted in bounds list | Tagged 15.0savvy Keynote | Leave a comment

macOS 15.5beta4で報告済みのバグ何も直らず

Posted on 4月 29 by Takaaki Naganoya

macOS 15.5beta4が配信されましたが、とくに報告ずみのバグは何も直っていません。15.5台では直らないというよりも、バグとして認識していないんじゃないでしょうか?

正式リリースされたようですが、アクセシビリティの音声コマンドが動作していないバグについては、修正されていません。

Posted in Bug news | Tagged 15.0savvy | Leave a comment

【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ

Posted on 4月 21 by Takaaki Naganoya

特定のパターンのファイル名のパスをaliasにcastする処理で処理系まるごとクラッシュするというmacOS 15.5β版(15.5 Beta(24F5053f))のバグ。

もっと以前から存在していたのかもしれませんが、特定のファイル名だとクラッシュを引き起こす前代未聞のバグがすごすぎて、いろいろ調べてみました。

文字依存する箇所はごくわずかで、いろいろ規則性があることがわかってきました。

・15.5 Beta(24F5053f)の日本語ユーザー環境(Primary LanguageをJapaneseにした状態)で発生。英語環境に変更すると発生しない
・ファイルパスをaliasにcastすると即・クラッシュ
・アルファベットとひらがな/カタカナが混在している必要がある???
・拡張子の種別は関係なく発生
・一定の文字数以上の長さが必要
・決定的な問題を引き起こすのは、濁点/半濁点つきのひらがな/カタカナが入っていること
・当初、記号文字やスペースが入っていることが条件かと思っていたが、これらを除去しても発生
・濁点/半濁点つき文字はファイル名の特定文字数以降に登場する必要がある。冒頭に移動させてもクラッシュは発生しない
・同じ処理内容のJXAでもクラッシュが発生する
・これらのクラッシュを誘発するファイル名がフォルダ名についていてもクラッシュは発生しない

現時点でクラッシュを発生させる最低限のファイル名は、

AAXAAXXあああああああああパ1.txt

であることを確認しています。

追記:
macOS 15.5β3(24F5053j)でも継続して発生中です。

Posted in Bug news | Tagged 15.0savvy | Leave a comment

macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ

Posted on 4月 19 by Takaaki Naganoya

きわめて珍しいパターンのバグに遭遇しました。自分の手元のmacOS 15.5Beta2環境(Apple Silicon Mac x 2)では再現率100%です(FB17285323)。再起動しても再現し、日本語環境で発生する一方で英語環境では発生しません。日本語環境であればJXAでも同様の現象の発生を確認しています。

「AppleScript+Xcode GUIアプリ ソースブック.key」

というファイル名のKeynote書類を、macOS 15.5β(日本語ユーザー環境)+Keynote v14.4で編集。このKeynote書類のボツスライドの移動用の「没.key」という書類をもとの書類の同階層に同じスタイルで作成するAppleScriptをスクリプトメニューから実行したところ、これがクラッシュ。

ねんのために、PagesとNumbersでも同様のテストを行なったところ、予想どおり100%クラッシュ。

これまでのmacOSでは遭遇したことのない現象だったので、原因を深掘り。

最終的に、Keynote/Pages/Numbers v14.4の書類からファイルパス(file)を取得し、そのfileをaliasにcastするとクラッシュするということが分かりました。

追記:Pixelmator Pro、CotEditorでも同様のファイル名に対して処理を行うとクラッシュすることが確認されました。

さらに追記:クラッシュを起こすパターンのファイルをchoose fileして、フルパスをstringにcastし、さらにaliasにcastするとアプリ操作に関係なくクラッシュすることが判明

さらに、ファイル名を短くしたり一部を取り出したりすることで、同様のクラッシュが発生するかどうかを調べてみたところ、

AppleScript+Xcode GUIアプリ ソースブ.key
AppleScript+Xcode GUIアプリ ソースブッ.key
AppleScript+Xcode GUIアプリ ソースブック.key
AAAAAAAA+XXXX GUIアプリ ソースブック.key
XXXXXXXX+XXXX XXXアプリ ソースブック.key
XXXXXXXX_XXXX XXXアプリ ソースブック.key
XXXXXXXX-XXXX XXXアプリ ソースブック.key
XXXXXXXX&XXXX XXXアプリ ソースブック.key
XXXXXXXX+XXXX GUIアプリ ソースブック.key
XXXXXXXX=XXXX XXXアプリ ソースブック.key

のパターンで、以下のAppleScriptを実行すると100%クラッシュが発生。スクリプトエディタでもScript Debuggerでもスクリプトメニューでも100%クラッシュ(実行プログラムのクラッシュ)します。

逆に、

AppleScript+Xcode GUIア.key
AppleScript+Xcode GUIアプ.key
AppleScript+Xcode GUIアプリ.key
AppleScript+Xcode GUIアプリ ソース.key
AppleScript+XCODE GUアプリ ソースブック.key
GUIアプリ ソースブックのコピー.key

ではクラッシュが発生しません(なんでや?)。

なんでこの組み合わせでクラッシュが発生するのか、macOS 10.12の時代に発生していた「日本語IMからのファイル名の入力時に余計なUnicode文字が入ることで、ファイル処理できなくなるバグ」の再現かと考え、このファイル名をテキストエディタにコピー&ペーストで表示させたものの、

とくに謎のゴミ文字が入力されているということはないようです。

ちなみに、「AppleScript+Xcode GUIアプリ ソースブック」は企画検討中の電子書籍です。

AppleScript名:Crash Test Script_Keynote.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/19
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

tell application "Keynote"
  tell front document
    set curFile to (file of it)
  end tell
end tell

set curFile to curFile as alias

★Click Here to Open This Script 

AppleScript名:Crash Test Script_PAGES.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/19
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

tell application "Pages"
  tell front document
    set curFile to (file of it)
  end tell
end tell

set curFile to curFile as alias

★Click Here to Open This Script 

AppleScript名:Crash Test Script_Numbers.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/19
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

tell application "Numbers"
  tell front document
    set curFile to (file of it)
  end tell
end tell

set curFile to curFile as alias

★Click Here to Open This Script 

AppleScript名:Crash Test Script_Pixelmator Pro.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/19
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

tell application "Pixelmator Pro"
  tell front document
    set curFile to (file of it)
  end tell
end tell

set curFile to curFile as alias

★Click Here to Open This Script 

AppleScript名:Crash Test Script_CotEditor.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/19
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

tell application "CotEditor"
  tell front document
    set curFile to (file of it)
  end tell
end tell

set curFile to curFile as alias

★Click Here to Open This Script 

AppleScript名:only choose file.scpt
set aFol to (choose file) as string
set anAlias to aFol as alias

★Click Here to Open This Script 

Posted in Bug | Tagged 15.0savvy Keynote | 1 Comment

Skimがv1.7.9にアップデート

Posted on 4月 15 by Takaaki Naganoya

オープンソースのPDFビューワー「Skim」がv1.7.9にアップデートしました。AppleScript用語辞書に変更が加わっていますが、説明文が追加された程度であり機能面での変更は見られません。

Posted in news | Tagged 13.0savvy 14.0savvy 15.0savvy Skim | Leave a comment

«class isot»が復活していた?

Posted on 4月 13 by Takaaki Naganoya

Mac OS X 10.5で、dateオブジェクトをISO日付形式に変換する«class isot»が動作しなくなっていました。

ISO日付テキストからdateオブジェクトへの変換が行えなくなる

そして、もうこの機能はずっと動かなくなったままなのかと思っていましたが……現在動いていることに気づきました。

「AppleScript最新リファレンス」の初版執筆時には使えないことを確認していたので、OS X 10.11ぐらいの時代には動かなかったようです。

その後、手元の古いMacで確認してみたところ、

macOS 10.13:動く
macOS 10.14:動く

と、割と各バージョンで動いていることを確認。最新版のmacOS 15.5βでも動いています。


set testd to ((current date) as «class isot») as string
–> "2025-04-13T22:28:56"

★Click Here to Open This Script 

目下、この機能はPiyomaru Softwareの電子書籍に掲載しているコマンドリファレンスに記載していませんが、記述を復活させるべきか悩ましいところです。

個人的には、Cocoaの機能を用いてISO日付フォーマットとの相互変換を行なっています。

AppleScript名:ISO8601日付文字列を生成 v2.scptd
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set aDate to getDateInternational(2022, 10, 21, 0, 0, 0, "JPT") –―year, month, date, hour, minute, second, time zone abbreviation.
–> date "2022年10月21日 金曜日 0:00:00"

set bStr to retISO8601DateTimeString(aDate, "JPT") as string
–> "2022-10-21T00:00:00+09:00"

–NSDate -> ISO8601 Date & Time String
on retISO8601DateTimeString(targDate, timeZoneAbbreviation)
  set theNSDateFormatter to current application’s NSDateFormatter’s alloc()’s init()
  
theNSDateFormatter’s setDateFormat:"yyyy-MM-dd’T’HH:mm:ssZZZZZ" — Five zeds to get a colon in the time offset (except with GMT).
  
theNSDateFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:(timeZoneAbbreviation))
  
return (theNSDateFormatter’s stringFromDate:targDate) as text
end retISO8601DateTimeString

–Make a GMT Date Object with parameters from a given time zone.
on getDateInternational(aYear, aMonth, aDay, anHour, aMinute, aSecond, timeZoneAbbreviation)
  set theNSCalendar to current application’s NSCalendar’s currentCalendar()
  
theNSCalendar’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:(timeZoneAbbreviation))
  
set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:aDay hour:anHour minute:aMinute |second|:aSecond nanosecond:0
  
return theDate as date
end getDateInternational

★Click Here to Open This Script 

Posted in date | Tagged 15.0savvy | Leave a comment

AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)

Posted on 4月 11 by Takaaki Naganoya

ファイルのドラッグ&ドロップを受け付ける「ドロップレット」の異常動作がmacOS 10.12からずっと続いてきました。AppleScriptドロップレットに対して(Finderから)ファイルをドラッグ&ドロップすると、欠落するものが出てくるという現象です。

昨年、macOS 15.2betaか15.1あたりでこのバグが解消されたように見えました。

(確認中)AppleScript Dropletのバグっぽい動作が解消?

「見えました」というのは、一応現象としては観測できつつも、その動作を意図して実現していないんじゃないか、という懸念があったためです。つまり、Appleの現場なりマネージャー級で意思決定が行われた成果ではなさそうだ、と判定。

この動作がmacOS 15.5βで以前と同様の動作に戻っている(バグ的な動作)ことを観測しています。

on open droppedFiles
set fileCount to count of droppedFiles
display dialog “ドロップされたファイルの数: ” & fileCount buttons {“OK”} default button 1
end open

一応、ドラッグ&ドロップされたファイル/フォルダの受付で取りこぼしが出ないように対策は(Scripter側で工夫して)できているのですが、上記のように一般に知られている単純な受信コードでは対処できません。

ChatGPTをはじめとするLLMでは、上記のような単純なコードを出力することを確認しています。そして、Apple側がOSに不具合を発生させた場合にはLLMが出力するコードでは対処できません。

そして、macOSのヘルプメニューから表示できる「AppleScriptヘルプ」に書かれているドロップレットのコードがまともに動かないというのでは、話になりません。

Posted in Bug Droplet news | Tagged 15.0savvy | Leave a comment

NaturalLanguage.frameworkを用いて日本語テキストの形態素解析を行う

Posted on 4月 10 by Takaaki Naganoya

NSLinguisticTaggerの後継であるNaturalLanguage(NLTokenizer)を用いて、日本語テキストの形態素解析を行なってみました。

この手の頭を使わないプログラミングだとChatGPTに丸投げすれば、(デバッグ作業が必要な)AppleScriptのコードが出てくるので、走らせて直して……で、15分ほどでしょうか。

NLTokenizerの処理結果は、間違ってはいないものの、相変わらず固有名詞を無視しまくるので、実用レベルにあるとはいえません。自分でAppleScriptで書いた簡易日本語パーサー「easyJParse」と処理結果が変わらないので、頑張ってNLTokenizerを使う意義はほとんどないといえます(日本語を処理すると品詞情報を返して来ないので余計に……)。

AppleScript名:NaturalLanguage.frameworkを用いて日本語テキストの形態素解析を行う.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/04/10
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript
use framework "Foundation"
use framework "NaturalLanguage"
use scripting additions

set inputText to current application’s NSMutableString’s stringWithString:"私の名前は長野谷です。"

— NLTokenizerのインスタンスを作成し、単語単位で設定
set tokenizer to current application’s NLTokenizer’s alloc()’s initWithUnit:(current application’s NLTokenUnitWord)
tokenizer’s setString:inputText
tokenizer’s setLanguage:(current application’s NLLanguageJapanese)

— 解析範囲の取得
set textLength to (inputText’s |length|()) as integer
set theRange to current application’s NSMakeRange(0, textLength)

— トークンを格納するリスト
set tokenList to {}

— トークン範囲を1つずつ取得して、文字列を抽出
set currentIndex to tokenizer’s tokenRangeAtIndex:0

repeat while (currentIndex’s location < (textLength))
  set subStr to (inputText’s substringWithRange:currentIndex)
  
copy (subStr as string) to end of tokenList
  
  
— 次のトークン範囲へ進む
  
set nextIndex to (currentIndex’s location) + (currentIndex’s |length|())
  
if nextIndex ≥ textLength then exit repeat
  
set currentIndex to tokenizer’s tokenRangeAtIndex:nextIndex
end repeat

return tokenList
–> {"私", "の", "名前", "は", "長野", "谷", "です"}

★Click Here to Open This Script 

Posted in Natural Language Processing | Tagged 13.0savvy 14.0savvy 15.0savvy NLTokenizer | Leave a comment

XIBから作るステータスメニューUI

Posted on 4月 8 by Takaaki Naganoya

AppleScriptで作るステータスメニューは、プログラムで動的に作るケースが圧倒的に多いところですが、Xcode上でCocoa Applicationを作成する場合にはXIBで(=XcodeのInterface Builder上で)作ったほうが簡単だし使い勝手がいいので、作成方法を確認しておきました。


▲ステータスメニュー


▲Interface Builder上でメニューをCocoa binding(Xcode 16.3)

–> Download Archive

AppleScript名:AppDelegate.applescript
—
— AppDelegate.applescript
— statusMenuFromXIB
—
— Created by Takaaki Naganoya2 on 2025/03/27.
—
—

script AppDelegate
  property parent : class "NSObject"
  
  
— IBOutlets
  
property theWindow : missing value
  
property theStatMenu : missing value –xib上で作っておいたメニュー。Cocoa Binding
  
  
on applicationWillFinishLaunching:aNotification
    set theIcon to current application’s NSImage’s imageNamed:(current application’s NSImageNameComputer)
    
set statusItem to (current application’s NSStatusBar’s systemStatusBar())’s statusItemWithLength:(current application’s NSVariableStatusItemLength)
    
statusItem’s setTitle:("PIYOMARU")
    
statusItem’s setImage:theIcon
    
statusItem’s setHighlightMode:true
    
statusItem’s setMenu:theStatMenu
    
    
statusItem’s popUpStatusItemMenu:theStatMenu
  end applicationWillFinishLaunching:
  
  
on applicationShouldTerminate:sender
    — Insert code here to do any housekeeping before your application quits
    
return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
end script

★Click Here to Open This Script 

Posted in AppleScript Application on Xcode | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

iWork Appsがバージョン14.4にアップデート

Posted on 4月 6 by Takaaki Naganoya

iWork Apps(Keynote、Pages、Numbers)がバージョン14.4にアップデートしました。各アプリのAppleScript用語辞書に変更はありません。

Pagesで継続して発生している、現在画面上で表示中のページ+2見開きのページ上のオブジェクト情報の取得/操作が行えない現象については、修正されていません。

Apple側はこれをバグとも思っていないようですし、修正するつもりもないのでしょう。この不具合に対処するために、情報取得する対象のページを強制的に表示するよう指示する必要があることでしょう(そんな機能はないので、GUI Scriptingで?)。

Pages書類の各ページ上の表の座標を求めると、正しい値が返らない

Posted in news | Tagged 14.0savvy 15.0savvy Pages | Leave a comment

Dock Menu

Posted on 4月 5 by Takaaki Naganoya

Dock Menuを表示するAppleScriptアプリです。探すと意外と情報がまとまっていないので、掲載しておきます。

Dock Menuについては、スクリプトエディタやスクリプトメニュー、アプレットなどで動作する通常のAppleScriptでは利用できませんが、Xcode上で作成するアプリやCocoa AppleScript Applicationでは利用できます。このあたりは、どのぐらいAppleScriptの実行環境がアプリのイベントをAppleScript側に提供しているかどうかによります。

スクリプトエディタ上で作成するアプレットの一種、Cocoa-AppleScript Appletを用いると、Dock Menuを動的に作ることが可能です(後述)。

–> Download Xcode Project Archive

AppleScript名:AppDelegate.applescript
—
— AppDelegate.applescript
— dock menu
—
— Created by Takaaki Naganoya2 on 2025/04/05.
—
—

script AppDelegate
  property parent : class "NSObject"
  
  
— IBOutlets
  
property theWindow : missing value
  
property dockMenu : missing value
  
  
on applicationWillFinishLaunching:aNotification
    
  end applicationWillFinishLaunching:
  
  
on applicationShouldTerminate:sender
    return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
  
  
–Dock Menu Enabled
  
on applicationDockMenu:(aNotification)
    return dockMenu
  end applicationDockMenu:
  
  
  
on clicked:aSender
    set aTag to (tag of aSender) as integer
    
display dialog (aTag as string)
  end clicked:
end script

★Click Here to Open This Script 

Cocoa-AppleScriptアプレット版はこちらです。macOS 12以降では、Finder上でRosettaを利用して実行するように指定する必要があります。

アプレット本体側のscriptではなく、ランタイム側の「CocoaAppletAppDelegate.scpt」を書き換える必要があります。これを読み返すと、つくづく「いきなりこの内容をScripterに使わせようとしたのは無理があった」と感じます。内容が、macOS 10.7当時のScripterのCocoaへの理解度を考えると難解すぎです。

–> Download Cocoa-AppleScript Applet

AppleScript名:CocoaAppletAppDelegate.scpt
—
— CocoaAppletAppDelegate.applescript
— Cocoa-AppleScript Applet
—
— Copyright 2011 {Your Company}. All rights reserved.
—

— This application delegate emulates the OSA script applet by loading "main.scpt" from the
— "Scripts" folder in the application resources and invoking the traditional run/open/reopen/quit
— handlers in response to Cocoa application delegate methods being called.
—
— This is provided in source form so that you may customize or replace it if your needs go
— beyond the basic applet handlers.
—
— Some of these methods must guard against re-entrancy, because invoking the main.scpt
— handler may end up invoking the event handler inherited from the current application,
— which calls the application delegate’s method again.

script CocoaAppletAppDelegate
  property parent : class "NSObject"
  
property mainScript : missing value — the applet’s main.scpt
  
property didOpenFiles : false — true = the application opened documents during startup
  
property isOpeningFiles : false — re-entrancy guard: true = in the process of opening files
  
property isReopening : false — re-entrancy guard: true = in the process of re-opening
  
property isQuitting : false — re-entrancy guard: true = in the process of quitting
  
  
on applicationWillFinishLaunching:aNotification
    — Insert code here to initialize your application before any files are opened
    
    
— Emulate an OSA Applet: Load the main script from the Scripts resource folder.
    
try
      set my mainScript to load script (path to resource "main.scpt" in directory "Scripts")
    on error errMsg number errNum
      — Perhaps this should silently fail if it can’t load the script; that way, a Cocoa applet
      
— can just have Cocoa classes and no main.scpt.
      
display alert "Could not load main.scpt" message errMsg & " (" & errNum & ")" as critical
    end try
  end applicationWillFinishLaunching:
  
  
on applicationDidFinishLaunching:aNotification
    — Insert code here to do startup actions after your application has initialized
    
    
if mainScript is missing value then return
    
    
— Emulate an OSA Applet: Invoke the "run" handler.
    
    
— If we have already opened files during startup, don’t invoke the run handler.
    
if didOpenFiles then return
    
    
try
      tell mainScript to run
    on error errMsg number errNum
      if errNum is not -128 then
        display alert "An error occurred while running" message errMsg & " (" & errNum & ")" as critical
      end if
    end try
    
    
— TODO: Read the applet’s "stay open" flag and quit if it’s false or unspecified.
    
— For now, all Cocoa Applets stay open and require the run handler to explicitly quit,
    
— which is arguably more correct for a Cocoa application, anyway.
    
(* if not shouldStayOpen then
      quit
    end if *)
  end applicationDidFinishLaunching:
  
  
on applicationShouldHandleReopen:sender hasVisibleWindows:flag
    — Insert code here to perform actions in response to a "reopen" event
    
    
if mainScript is missing value then return true
    
    
— Guard against re-entrancy.
    
if not isReopening then
      set isReopening to true
      
      
— Emulate an OSA Applet: Invoke the "reopen" handler. If there isn’t one, let the application object
      
— handle reopen (this is different from an OSA applet, which would do nothing if there is no handler;
      
— this way, the application will perform the usual "create untitled document" behavior by default).
      
try
        tell mainScript to reopen
        
set isReopening to false
        
        
return false
      on error errMsg number errNum
        if errNum is not -128 then
          display alert "An error occurred while reopening" message errMsg & " (" & errNum & ")" as critical
        end if
      end try
      
      
set isReopening to false
    end if
    
    
return true
  end applicationShouldHandleReopen:hasVisibleWindows:
  
  
on |application|:sender openFiles:filenames
    — Insert code here to perform actions in response to an "open documents" event
    
    
— Remember that we opened files, to avoid invoking the "run" handler later.
    
set didOpenFiles to true
    
    
— Guard against re-entrancy.
    
if not isOpeningFiles and mainScript is not missing value then
      set isOpeningFiles to true
      
      
try
        — Convert all the filenames from NSStrings to script strings
        
set theFilenameStrings to {}
        
repeat with eachFile in filenames
          set theFilenameStrings to theFilenameStrings & (eachFile as text)
        end repeat
        
        
tell mainScript to open theFilenameStrings
        
set isOpeningFiles to false
        
        
tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplySuccess)
      on error errMsg number errNum
        if errNum = -128 then
          tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyCancel)
        else
          display alert "An error occurred while opening file(s)" message errMsg & " (" & errNum & ")" as critical
          
tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyFailure)
        end if
      end try
      
      
set isOpeningFiles to false
    else
      tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyFailure)
    end if
  end |application|:openFiles:
  
  
on applicationShouldTerminate:sender
    — Insert code here to do any housekeeping before your application quits
    
    
— Guard against re-entrancy.
    
if not isQuitting and mainScript is not missing value then
      set isQuitting to true
      
      
— Emulate an OSA Applet: Invoke the "quit" handler; if the handler returns, it has fully
      
— handled the quit message and we should not quit, otherwise, it calls "continue quit",
      
— which returns error -10000.
      
try
        tell mainScript to quit
        
set isQuitting to false
        
        
return current application’s NSTerminateCancel
      on error errMsg number errNum
        — -128 means there is no quit handler
        
— -10000 means the handler did "continue quit"
        
if errNum is not -128 and errNum is not -10000 then
          display alert "An error occurred while quitting" message errMsg & " (" & errNum & ")" as critical
        end if
      end try
      
      
set isQuitting to false
    end if
    
    
return current application’s NSTerminateNow
  end applicationShouldTerminate:
  
  
  
–Dock Menu Enabled
  
on applicationDockMenu:(aNotification)
    set aMenu to current application’s NSMenu’s alloc()’s init()
    
set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:"Dock Menuだよ" action:"actionHandler:" keyEquivalent:"")
    (
aMenuItem’s setTarget:me)
    (
aMenu’s addItem:aMenuItem)
    
return aMenu
  end applicationDockMenu:
  
  
on actionHandler:sender
    set aTag to (tag of sender) as string
    
set aTitle to (title of sender) as string
    
    
display dialog (aTitle as string)
  end actionHandler:
end script

★Click Here to Open This Script 

Posted in AppleScript Application on Xcode Cocoa-AppleScript Applet | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

Xcode 16.3で用語辞書にdebugコマンドを追加?

Posted on 4月 4 by Takaaki Naganoya

Xcode 16.3のAppleScript用語辞書を調べていたら、以前のバージョンから変更が加わっていることを見つけました。もっと前のバージョンから変更されていたのかもしれませんが (Xcode 16.2で変更されていました。16.xで変更?)、XcodeのAppleScript用語辞書は機能が壊滅状態なので、まさかアップデートされるとは思ってもみませんでした。

正直なところ、XcodeのAppleScript用語辞書は「これで何をしろと?」という出来で、よく言って「Apple社内のエンジニアが仕事をしているフリ」、正直なところ「何を実装しているのか作成者が目的を完全に見失っている」ような内容です。命令1つ2つを追加したところで、使い物になりません。XcodeのAppleScript用語辞書まわりは、完全にゼロから作り直さないと、何もできない内容です。

XcodeのAppleScript用語辞書こそ、サンプルが掲載されていないと実装側の考えがわからない内容の代表例です。作業者の知能指数を疑うレベルであり、話になりません。

まして、debugコマンドはAppleScriptで作成したアプリケーションでは使えないコマンドなので、GUI側からも使ったことがありません。

Applicationクラスがcreate temporary debugging workspaceコマンドに応答するようになった?

Xcode Application Suiteスイーツ(命令集)にcreate temporary debugging workspaceコマンドが追加された

workspace documentクラスがattachとdebugのコマンドに応答するようになった?

Xcode Scheme Suiteスイーツ(命令集)にattachとdebugコマンドが追加された

AppleScript用語辞書に掲載されているattachとdebugコマンド。本当に動くのか、どう指定するのかさっぱりわからない

Posted in Object control | Tagged 15.0savvy Xcode | Leave a comment

NSIndexSetを作成し、各index要素を取り出す

Posted on 3月 30 by Takaaki Naganoya

NSIndexSetを作成し、そこから各インデックス(数値)を取り出すAppleScriptです。

{3, 4, 5, 6, 7}

から構成されるNSIndexSetを作成して処理すると、

{3, 4, 5, 6, 7}

が返ってきます。

NSTableViewで複数行選択時の行インデックスを取得する際に、NSIndexSetで結果が返ってきたので、ChatGPTに書かせてみました。

本Script内でNSIndexSet内の各インデックス数値に対してオフセット加算ができるようにしているのは、このNSTableViewで1行目が0と返ってくる仕様のためで、AppleScriptと合わせるために+1のオフセット加算を行う必要があったためこのようになっています。

NSTableViewで複数行を選択すると、結果がNSIndexSetで返ってくる

初回から動くコードは返してこなかったものの、何回かやりとりしている間に、書き直せばなんとかなりそうだったので、引き継いで手で書いてみました。CocoaのAPI呼び出しはプログラミングではなくパズルみたいなものなので、ChatGPTに向いているといえば向いているのですが、AppleScriptObjCがBlocks構文を呼べないとかいった前提条件が認識されていないようなので、「手で描き直した方が速い」ということに。

NSNotFoundについては、Apple側も真面目に実装している風ではない(これを返すことが徹底されているわけではない、と)ので、あてにできないと感じています。

AppleScript名:NSIndexSetを作成し、各index要素を取り出す.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/03/30
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

— サンプルのNSIndexSetを作成(3, 4, 5, 6, 7 を含む)
set indexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(3, 5))
set aList to getEachIndexFromIndexSet(indexSet, 0) of me
–> {3, 4, 5, 6, 7}

— インデックスを取得する処理
on getEachIndexFromIndexSet(indexSet, offsetNum)
  set indexList to {}
  
set currentIndex to indexSet’s firstIndex() — 最初のインデックスを取得
  
  
repeat while currentIndex ≠ (current application’s NSNotFound)
    — 取得したインデックスをAppleScriptのリストに追加
    
if currentIndex > 9.99999999E+8 then exit repeat
    
    
set end of indexList to ((currentIndex as number) + offsetNum)
    
    
— 次のインデックスを取得
    
set currentIndex to indexSet’s indexGreaterThanIndex:currentIndex
  end repeat
  
  
return indexList
end getEachIndexFromIndexSet

★Click Here to Open This Script 

AppleScript名:NSIndexSetを作成し、各index要素を取り出す(書き換え後).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/03/30
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

— サンプルのNSIndexSetを作成(3, 4, 5, 6, 7 を含む)
set indexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(3, 5))
set aList to getEachIndexFromIndexSet(indexSet, 0) of me
–> {3, 4, 5, 6, 7}

— インデックスを取得する処理
on getEachIndexFromIndexSet(indexSet, offsetNum)
  set indexList to {}
  
set currentIndex to indexSet’s firstIndex() — 最初のインデックスを取得
  
  
— 取得したインデックスをAppleScriptのリストに追加  
  
repeat while currentIndex ≤ 9.99999999E+8
    set end of indexList to ((currentIndex as number) + offsetNum)
    
    
— 次のインデックスを取得
    
set currentIndex to indexSet’s indexGreaterThanIndex:currentIndex
  end repeat
  
  
return indexList
end getEachIndexFromIndexSet

★Click Here to Open This Script 

Posted in list | Tagged 13.0savvy 14.0savvy 15.0savvy NSIndexSet | Leave a comment

Keynoteの現在のスライド上で選択中のテキストをもとに、後続の記事トビラページのタイトルに内容を設定 v2a

Posted on 3月 23 by Takaaki Naganoya

Keynote書類の現在表示中のスライド上で選択中のテキストアイテム(ボックス)の内容をもとに、後続の記事トビラページのタイトルを設定するAppleScriptです。Keynote 14.3+macOS 15.4betaで動作確認していますが、とくにバージョン依存した書き方などは行なっていません。

また、本来はiWork汎用オブジェクトの座標によるソート機能をライブラリとして独立させ、バンドル形式のScript書類に入れてあったのですが、Blog掲載のためにフラットなScriptに書き換えています。

本来、Keynoteのようにスライドのインデント(レベル変更)を行って階層構造を形成できるアプリでは、それぞれのスライド(ページ)のレベルを取得したり変更できることが望ましいのですが、Keynoteの機能セットの範囲内ではAppleScriptからそのような操作は行えません。そこで、各スライドのベースレイアウトに何を用いているかによって擬似的にレベルを判定しています。

KeynoteのAppleScript対応機能はiWork Apps中では屈指の対応度を誇っていますが、レイアウトした画像の内容データにアクセスできないのと、各スライドのレベルの取得/変更が行えない点がものすごく残念です。

電子書籍「Cocoa Scripting Course」の作成用に、以前にも作ったことがあるかもしれませんが、ふたたび作ってしまいました。ちょっと大きめの書き捨てScriptです。


▲Keynote書類の章トビラ上で章の記事内容を示すテキストボックスを選択した状態で本AppleScriptを実行。複数のボックスを選択してあっても、座標値でソートして順番を決定


▲各記事のトビラページに、章トビラから取得したテキストを設定。以下、繰り返し


▲章トビラ上のテキストをすべて記事トビラのタイトルに設定

AppleScript名:現在のスライド上で選択中のテキストをもとに、後続の記事トビラページのタイトルに内容を設定 v2a.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/03/23
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
–use iwoSortLib : script "iWorkObjSortLib"

tell application "Keynote"
  tell front document
    –章扉の上にあるテキストアイテム(各記事タイトルが入っているものと想定、複数可能)を取得
    
set aSelList to selection
    
set bSelList to {}
    
set sCount to count every slide
    
    
    
–画面上で選択しておいたオブジェクトのうち、text itemのみを抽出(念のため)
    
repeat with i in aSelList
      set j to contents of i
      
set aClass to class of j
      
if aClass is text item then
        set the end of bSelList to j
      end if
    end repeat
    
    
set cSelList to sortIWorkObjectsByPositionAndRetObjRef(bSelList, {"positionX", "positionY"}, {true, true}) of iWorkObjSort
    
    
    
–章トビラ上のテキストアイテム(複数可)をループしつつ、中のテキストを行ごとに分解してリスト化
    
–(連結前にあらかじめX座標をもとにソートしておいたほうがいい??)
    
set textList to {}
    
repeat with i in cSelList
      set tmpCon to object text of i
      
set tmpList to paragraphs of tmpCon
      
set textList to textList & tmpList
    end repeat
    
–return textList
    
    
–章トビラのページの情報を取得
    
tell current slide
      set curSlideNum to slide number
      
set curSlideLayout to name of base layout
    end tell
    
    
–章トビラの次のページの情報を取得(ここが必ず記事トビラであるという前提のもとに処理)
    
tell slide (curSlideNum + 1)
      set nextSlideLayout to name of base layout
    end tell
    
    
set targSlideList to {}
    
repeat with i from (curSlideNum + 1) to sCount
      tell slide i
        set tSTheme to name of base layout
        
if tSTheme = curSlideLayout then
          –章トビラを検出したら処理終了
          
exit repeat
        else if tSTheme = nextSlideLayout then
          –扉+1ページのスライド(記事カバー)を検出したら記録
          
set the end of targSlideList to i
        end if
      end tell
    end repeat
    
–return targSlideList
    
    
set iCount to 1
    
repeat with i in targSlideList
      try
        set tmpT to contents of item iCount of textList
        
tell slide i
          set object text of default title item of it to tmpT
        end tell
        
set iCount to iCount + 1
      on error
        return
      end try
    end repeat
  end tell
end tell

–ライブラリとしてバンドル形式のAppleScript書類に組み込んでいたものをBlog掲載用に展開した
script iWorkObjSort
  property parent : AppleScript
  
use AppleScript
  
use framework "Foundation"
  
use framework "AppKit"
  
use scripting additions
  
  
script spd
    property aaSel : {}
  end script
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Obj情報はitem noだけ)
  
on sortIWorkObjectsByPosition(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
      end tell
    end tell
    
    
return sortedList
  end sortIWorkObjectsByPosition
  
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Objだけ返す)
  
on sortIWorkObjectsByPositionIncludingObjRef(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
        
–データを返す配列にiWork Object への参照を含める
        
set sCount to 1
        
repeat with i from 1 to (length of aaSel)
          set tmpID to objID of contents of item i of sortedList
          
set tmpObj to contents of item tmpID of aaSel
          
set (item tmpID of sortedList) to (item tmpID of sortedList) & {objRef:tmpObj}
        end repeat
      end tell
    end tell
    
    
return sortedList
  end sortIWorkObjectsByPositionIncludingObjRef
  
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Obj入りのリストを返す)
  
on sortIWorkObjectsByPositionAndRetObjRef(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
        
–データを返す配列にiWork Object への参照を含める
        
set sCount to 1
        
set retList to {}
        
repeat with i from 1 to (length of aaSel)
          set tmpID to objID of contents of item i of sortedList
          
set tmpObj to contents of item tmpID of aaSel
          
set the end of retList to tmpObj
        end repeat
      end tell
    end tell
    
    
return retList
  end sortIWorkObjectsByPositionAndRetObjRef
  
  
  
–リストに入れたレコードを、指定の属性ラベルの値でソート
  
on sortRecListByLabel(aRecList as list, aLabelStr as list, ascendF as list)
    set aArray to current application’s NSArray’s arrayWithArray:aRecList
    
    
set aCount to length of aLabelStr
    
set sortDescArray to current application’s NSMutableArray’s new()
    
repeat with i from 1 to aCount
      set aLabel to (item i of aLabelStr)
      
set aKey to (item i of ascendF)
      
set sortDesc to (current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabel ascending:aKey)
      (
sortDescArray’s addObject:sortDesc)
    end repeat
    
    
return (aArray’s sortedArrayUsingDescriptors:sortDescArray) as list
  end sortRecListByLabel
  
end script

★Click Here to Open This Script 

Posted in list Sort | Tagged 13.0savvy 14.0savvy 15.0savvy Keynote | Leave a comment

Numbersで選択範囲のdateの年を+1する

Posted on 3月 10 by Takaaki Naganoya

Numbersの書類上で選択中のセル内の日付形式データがあったら、yearを+1(インクリメント)するAppleScriptです。macOS 15.4beta+Numbers 14.3で動作確認していますが、OSバージョンおよびNumbersのバージョンに依存する部分はありません。

実行前

実行後

ものすごくつまらなくて、ものすごく用途が狭いScriptですが、実際に書いてみたら意外と手間取る感じでした。

当然、+1するだけではなくー1する機能も作ってあり、呼び出し部分を差し替えればー1するようになります。macOS標準搭載のスクリプトメニューに入れて実行することを想定しています。

AppleScript名:選択範囲のdateの年を+1する.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/03/10
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application "Numbers"
  tell front document
    tell active sheet
      try
        set theTable to first table whose class of selection range is range
        
set cellList to cell of selection range of theTable
        
set valList to value of cell of selection range of theTable
        
set aLen to length of cellList
      on error
        display notification "Numbers: There is no selection"
        
return
      end try
      
      
set bValList to {}
      
repeat with i in valList
        set j to contents of i
        
set aClass to class of j
        
        
if aClass = date then
          set j to incremenetYearOf(j) of me
        end if
        
        
set the end of bValList to j
      end repeat
      
      
      
repeat with i from 1 to aLen
        set aVal to contents of item i of bValList
        
set aCell to contents of item i of cellList
        
        
ignoring application responses
          set value of aCell to aVal
        end ignoring
      end repeat
      
    end tell
  end tell
end tell

on incremenetYearOf(aDate)
  set {bYear, bMonth, bDate} to {year of aDate, month of aDate, day of aDate}
  
set bYear to bYear + 1
  
set {year of aDate, month of aDate, day of aDate} to {bYear, bMonth, bDate}
  
return aDate
end incremenetYearOf

on decremenetYearOf(aDate)
  set {bYear, bMonth, bDate} to {year of aDate, month of aDate, day of aDate}
  
set bYear to bYear – 1
  
set {year of aDate, month of aDate, day of aDate} to {bYear, bMonth, bDate}
  
return aDate
end decremenetYearOf

★Click Here to Open This Script 

Posted in Calendar list | Tagged 13.0savvy 14.0savvy 15.0savvy Numbers | Leave a comment

CotEditor v5.1.1でprint機能に微修正

Posted on 3月 5 by Takaaki Naganoya

CotEditorのメンテナーの@1024jp氏の談話によると、

https://x.com/1024jp/status/1896829943587508664

CotEditor v 5.1.0 → 5.1.1 でAppleScript経由のprint機能がうまく動作していなかったのを動作するように修正したとのこと。

ここのところ、macOS側の機能の不全により、AppleScript経由でのprint機能の実行がうまく動作していないケースが発生しており、それに対処したとのこと。

Posted in Object control | Tagged 14.0savvy 15.0savvy CotEditor | Leave a comment

CotEditor v5.1.0でdocumentのcontentsにeditable属性を追加

Posted on 3月 3 by Takaaki Naganoya

CotEditorがバージョン5.1.0にアップデートしました(翌日、5.1.1にアップデートしましたが、AS的には差はありません)。

アップデート内容は多岐に渡っていますが、AppleScript的には、documentのcontentsにeditable(boolean)属性が追加されました。

実際に、AppleScriptからこの属性値を操作して、想定どおりの挙動を行うかどうかをチェックしてみました。こうしたテストは開発者側でも行なっているかどうか怪しいところなので(とくにAppleのノーチェックぶりが目に余る)、大事な作業です。

想定どおりの動作を行なっているので、確認OKです!

AppleScript名:v51.0 document editable test.scpt
tell application "CotEditor"
  tell front document
    set editable of it to true
    
set edF to editable
    
–> true
    
set contents of it to "ABC"
    
    
    
set editable of it to false
    
set edF to editable
    
–> false
    
set contents of it to "CDE"
  end tell
end tell

★Click Here to Open This Script 

Posted in Object control | Tagged 14.0savvy 15.0savvy CotEditor | Leave a comment

画像をAppleScriptでアスキーアート化

Posted on 2月 26 by Takaaki Naganoya

指定の画像をアスキーアート化して、ダイアログ表示するAppleScriptです。

–> Download Script archive with Library

面倒だったのでChatGPTに書かせてみましたが、Retina Display対応や細かい箇所のエラーを取りきれず、途中から自分で書き換えました。

画像のアスキーアート化Scriptは、できることはわかっていつつも、これほど役立たずに時間の無駄なプログラムも珍しいところ。こういうのはChatGPTに書かせるのがお似合いです。

それにしても、けっこうこまめにコメント入れるもんですね。正しい内容かどうかは定かではありませんが……>ChatGPT

AppleScript名:画像をAppleScriptでアスキーアート化.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/02/26
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
use radioLib : script "displayTextView"

property retinaF : missing value

— ファイル選択ダイアログで画像を選ぶ
set imagePath to POSIX path of (choose file with prompt "画像を選択してください:")

— ASCIIアートを生成
set asciiText to convertImageToASCII(imagePath)
display text view asciiText main message "ASCII ART" sub message "Sub Message" with properties {font name:"Courier", size:12, width:620, height:620, color:{255, 255, 255}}

on convertImageToASCII(imagePath)
  set asciiChars to "@%#*+=-:. " — 濃い→薄い順の文字列
  
  
— Retina用のスケールファクターを取得
  
set retinaF to (current application’s NSScreen’s mainScreen()’s backingScaleFactor()) as real
  
  
— 画像を読み込む
  
set imageURL to current application’s NSURL’s fileURLWithPath:imagePath
  
set imageData to current application’s NSImage’s alloc()’s initWithContentsOfURL:imageURL
  
  
— 論理サイズ(points単位)を取得("size"は予約語なのでエスケープ)
  
set imageSize to imageData’s |size|()
  
set {w_pt, h_pt} to {imageSize’s |width|, imageSize’s |height|}
  
  
— リサイズ後の幅(文字数)を指定し、論理サイズに基づいてスケール計算
  
set newWidth to 80
  
set scaleFactor to newWidth / w_pt
  
set newHeight to round (h_pt * scaleFactor * 0.55) — 文字の縦横比調整
  
  
— ピクセル単位のサイズに変換(Retina対応)
  
set pixelWidth to newWidth * retinaF
  
set pixelHeight to newHeight * retinaF
  
set pixelSize to current application’s NSMakeSize(pixelWidth, pixelHeight)
  
  
— 新しいピクセルサイズでNSImageを作成し、元画像を描画
  
set resizedImage to current application’s NSImage’s alloc()’s initWithSize:pixelSize
  
resizedImage’s lockFocus()
  
current application’s NSGraphicsContext’s currentContext()’s setImageInterpolation:(current application’s NSImageInterpolationHigh)
  
imageData’s drawInRect:{{0, 0}, pixelSize} fromRect:{{0, 0}, imageSize} operation:(current application’s NSCompositingOperationCopy) fraction:1.0
  
resizedImage’s unlockFocus()
  
  
— リサイズ後の画像からNSBitmapImageRepを取得
  
set resizedRep to current application’s NSBitmapImageRep’s imageRepWithData:(resizedImage’s TIFFRepresentation())
  
  
— 各ピクセルをASCII文字に変換(Retina対応のため、retinaFごとにサンプリング)
  
set asciiArt to ""
  
set stepX to retinaF as integer
  
set stepY to retinaF as integer
  
  
repeat with y from 0 to (pixelHeight – 1) by stepY
    repeat with x from 0 to (pixelWidth – 1) by stepX
      set pixelColor to (resizedRep’s colorAtX:(x * retinaF) y:(y * retinaF))
      
      
set r to pixelColor’s redComponent()
      
set g to pixelColor’s greenComponent()
      
set b to pixelColor’s blueComponent()
      
set brightness to (r + g + b) / 3
      
      
set charIndex to round (brightness * ((count of asciiChars) – 1))
      
set asciiArt to asciiArt & (character (charIndex + 1) of asciiChars)
    end repeat
    
set asciiArt to asciiArt & linefeed
  end repeat
  
  
return asciiArt
end convertImageToASCII

★Click Here to Open This Script 

Posted in dialog file Image Text | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

XcodeのWorkspace Documentをクローズ後、再オープン

Posted on 2月 21 by Takaaki Naganoya

XcodeでオープンしているWorkspace Document(.xcodeproj)のパスを取得してクローズし、再度オープンするAppleScriptです。macOS 15.3上のXcode 16.2で動作をテストしました。

Xcodeでオープン中の.applescriptファイルの構文確認を行うために、いろいろ試行錯誤してみたものの、Xcodeでオープン中にはファイルへの書き込み権限を取得できない雰囲気が濃厚。Xcodeのエディタで表示中のコンテンツ(.applescript)の本文に文字列を突っ込んでみても、Xcodeに蹴られます。

# AppleScriptからの制御が封じられているので、まっとうな手口が使えません

これに対策するために、Xcode Projectをいったんクローズして、構文確認を行うとよさそうだったので、試作品を作ってみたものです。

AppleScript名:XcodeのWorkspace Documentをクローズ後、再オープン.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/02/21
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

tell application "Xcode"
  set xcVer to version
  
  
tell workspace document 1
    set xWSprojPath to path
    
close
  end tell
end tell

–delay 1

tell application "Xcode"
  open ((POSIX file xWSprojPath) as alias)
end tell

★Click Here to Open This Script 

Posted in file File path Object control | Tagged 13.0savvy 14.0savvy 15.0savvy Xcode | Leave a comment

AppleScript Explored 最新OS&Xcode対応 日本語版?

Posted on 2月 18 by Takaaki Naganoya

個人的な資料として、Shane Stanleyの電子書籍「AppleScript Explored」を日本語訳して使っておりました。部分的にmacOS最新版+Xcode最新版にスクリーンキャプチャを入れ替えて。


▲あくまで自分用の私的な資料として作成した日本語版


▲自分用だったので、装飾のたぐいはほとんどなし


▲いつもの感じでレイアウトを作るとこんな感じ?

最初の版から、購入して読んでいましたが……実にいい本です。「もうちょっと画面キャプチャが多いとわかりやすいのに」とは思っていましたが、自分には書けないタイプの本です。

# そのフィードバックをもとに、画面キャプチャ主体の本「AppleScript+XcodeでつくるMacアプリ Xcode 14対応」を出したわけで

冗談半分でShaneに「日本語訳版を出してもいい?」と聞いてみたところ、快諾。えっ? いいの?!(^ー^;;;

いまXcodeで作っているAppleScriptのプロジェクトがあるのですが、こうした資料が充実していないと本当に辛いので、日本語版を出せることは個人的に素直に嬉しいです。

ただ、本書がOS X 10.8ぐらいの時代に書かれたという時代背景があり、そのあたりの状況認識を大幅に変更する必要はあると思います。当時はShaneがAppleScriptObjC Explorerを作っていたので、そうしたツールの存在を前提として書かれた部分もあるので、macOS 15時代+Xcode 16環境に合わせてアップデートする必要もあることでしょう。

Posted in AppleScript Application on Xcode Books news | Tagged 15.0savvy Xcode | Leave a comment

Post navigation

  • Older posts

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

Google Search

Popular posts

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

Tags

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

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年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