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

投稿者: Takaaki Naganoya

Keynoteで選択中のスライドのすべての表のセル内文字色を黒にする

Posted on 5月 27 by Takaaki Naganoya

Keynote v12.0以降で、最前面の書類で選択中のスライド(複数可)にある表のすべての文字色を「黒」に変更するAppleScriptです。

–> Watch Demo Movie

Keynote v12.0で「selection」がまともに機能するようになったので、Keynote用Scriptの実用性がたいへんに高まっています。

# ファイル保存できないとかいうバグが一刻も早く治ることを希望しています。とくに、Pages!

もともと、複数ページにわたって掲載している「表」の中に赤字でマークした箇所があって、そのマークの意図とは別の赤字を入れたかったので、いったんすべて書式をリセットしたかったので書いたものです。

もうちょっと長いScriptを書かなければならないかと思っていたのですが、ことのほかシンプルに書けました。
もっとシンプルに書けるか(everyを使ってスライドごと指定するとか)も試してみたのですが、どうやらこのあたりが限界のようです。

Keynoteのselectionを活かしたScriptでいまいちばん活躍しているのは、Keynoteのテキストアイテム(text item)から内容を取得して、メモリ上でAppleScriptとしてコンパイルし、構文色分けを反映させたスタイル付きテキストを取得して、テキストアイテムに書式を反映させるものです。実に、Keynote上にテキストで書いておいたAppleScriptのリストを構文色分けを反映させた掲載リストに変換できるので便利です。

AppleScript名:Keynoteで選択中のスライドのすべての表のセル内文字色を黒にする.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/05/27
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

tell application "Keynote"
  set aVer to version
  
  
considering numeric strings
    if aVer < 12.0 then return –Keynote v12.0より以前のバージョンでは実行できない
  end considering
  
  
tell front document
    set aaSel to selection
    
set selClass to class of contents of first item of aaSel
    
if selClass is not equal to slide then return –選択中のオブジェクトがslide(ページ)でなければ処理終了
    
    
repeat with i in aaSel
      set j to contents of i
      
      
tell j
        –現在のスライド上のすべての表のすべてのセルの文字色を黒 {0, 0, 0}に
        
set text color of every cell of every table to {0, 0, 0}
      end tell
      
    end repeat
    
  end tell
  
end tell

★Click Here to Open This Script 

(Visited 5 times, 5 visits today)
Posted in 未分類 | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

新発売:AppleScript基礎テクニック集(12)Unix shell commandの利用

Posted on 5月 26 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第12巻、「Unix shell commandの利用」です。PDF 34ページ、サンプルAppleScriptのZipアーカイブを添付。

→ 販売ページ

Unix shell commandの利用といったときに、「AppleScriptからdo shell scriptコマンドでshell commandを呼び出す」話と、「shell commandやshell scriptの中でAppleScriptで書いたプログラムを呼び出す」という、まったく価値観の異なる別の話が含まれています。

本書は、これらのまったく別な話に含まれている概念やノウハウを整理し、わかりやすく具体的にその方法や制限についてご紹介する本です。

目次

■Terminal環境とdo shell script環境の違い

shell scriptをAppleScriptと組み合わせるために
ターミナルに書類のパスを入力する方法
Terminalからプロセスの確認と終了を
Terminalとdo shell sciptの環境変数
Terminal.appをAppleScriptで操作できる①
Terminal.appをAppleScriptで操作できる②

AppleScriptからshell scriptを呼び出す

do shell scriptコマンド
パス形式変換
Terminal.app上と同じ環境変数を使用するには
参考資料:新規インストールしたユーザーの環境変数
パラメータの与え方について
do shell scriptコマンドちょっといいノウハウ
コマンドライン呼び出しプログラムをScriptに同梱

shell scriptからAppleScriptを呼び出す

AppleScript関連のshell command
shell scriptからAppleScriptを呼び出す方法①
shell scriptからAppleScriptを呼び出す方法②
パラメータ指定できるAppleScriptの書き方
AppleScriptライブラリを呼び出せる?
パイプで指定した内容を受信できる?
GUI Scriptingを利用できる?
ホームディレクトリ下のFrameworkを呼べる?

(Visited 12 times, 12 visits today)
Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Pages v12に謎のバグ。書類上に11枚しか画像を配置できない→解決

Posted on 5月 23 by Takaaki Naganoya

Pages v12に謎のバグを見つけました。Pagesでは書類上に任意の画像を配置できるわけですが、11枚目までは配置できるものの、12枚目を配置できません。実際には120枚ほど画像を用意してテストしていたので、12枚目が配置できればそれでOKかと言われれば、ぜんぜん不十分です。

→ OSの再起動を含む、追試を何回か行ってみたところ、その後このままでは画像の貼り込みができなくなっていました。そして、画像ファイル貼り込み前にパスの文字列をaliasにcastしたところ、問題なく120枚貼り込めました。

–> Watch Movie

以前にも、Keynoteに「特定サイズ以上の表を作るとエラーになる」とかいったバグが発生していましたが、この12個目を配置するとエラーになるというのも、内部で何か不可思議なリミッターを設けているように見えます。

スクリプトエディタ、Script Debuggerの両方で発生しています。おそらく、ランタイム環境が何であるかはこの問題に影響を与えていません。

macOS 10.12以降で、日本語環境限定で発生しているバグで有名なものに、「日本語入力Input Method経由でファイル名を入力している最中に、不必要な不可視文字が入力されてしまい、これがファイル処理を妨げる危険性がある」というものがありますが、これらの画像のファイル名をチェックしたところ、そうした危険な不可視文字は混入していませんでした。

# こうした、複数チームの担当製品の間で発生しているバグは、どこが担当してバグを調査するということはないようです。組織の細分化にともない、こうした組織境界面でバグが発生するととたんに無責任になるのがいまのAppleです(組織の構造上の問題です)

ちなみに、GUI経由で画像をPages書類上に配置してみたところ、12枚以上配置できました。Pagesにそのような上限が存在しているわけではないようです。

Pages書類(バンドル書類)内で何かファイル名の重複のようなものが発生したのかと考え、別の画像を(番号をずらして)指定してみましたが、同様の枚数を配置した時点でエラーになりました。

内部で発生している(していた)別のエラーを発生させないように、謎のリミッターをかけていた可能性もありますが、その必然性がよくわかりません。

AppleScript名:指定の画像をPagesに順次貼り込む(12枚で止まってしまう!).scpt
set baseName to "book24_"
set baseFol to (choose folder) as string

set pMax to 10

(*
tell application "Pages"
  –set newDoc to make new document
  
  tell front document
    set dCount to count every page
    
    repeat with i from (dCount + 1) to pMax
      make new page
    end repeat
    
    set newDCount to count every page
  end tell
end tell
*)

repeat with i from 1 to 120
  set aFN to baseFol & baseName & makeFN(i, 4) of me & ".jpg"
  
tell application "Pages"
    tell front document
      tell page i
        set newItem to make new image with properties {file:aFN}
        
tell newItem
          set position of it to {0, 0}
          
set height of it to 843
          
–set position of it to {-1, 0}
          
set locked to true
        end tell
      end tell
    end tell
  end tell
end repeat

on makeFN(aNum, aDigit)
  set aText to "00000000000" & (aNum as text)
  
set aLen to length of aText
  
set aRes to text (aLen – aDigit + 1) thru -1 of aText
  
return aRes
end makeFN

★Click Here to Open This Script 

(Visited 11 times, 1 visits today)
Posted in Bug news | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a comment

iWorkアプリケーションv12に共通のバグ? 新規ファイルの保存ができない

Posted on 5月 23 by Takaaki Naganoya

Apple iWorkアプリケーション(Keynote、Pages、Numbers)の最新バージョンv12.0において、共通のバグがあるのではないか? と見ています。もちろん、見ているだけでなくAppleにバグレポートもしています。

症状は、新規作成した書類を「保存できない」というものです。

以前にも、PDFをexportできないという致命的なバグが発生していましたが、今回のも同様のメカニズムで発生しているものと見ています。つまり、「Appleが自社OSに設定したセキュリティ機能によって、自社アプリであるKeynote、Pages、Numbersが自家中毒を起こしている」という状態です。

自分の勘違いだとよいのですが….

あとは、Numbersのsaveコマンドをよく見てみると、書類フォーマットに「as Numbers」というenumがあるのですが、これはAppleScriptの処理系では「number」の複数形として認識されてしまうので、この予約語自体に無理があります。ここは、「as Numbers format」といった予約語に変えるべきです。

AppleScript名:Keynoteで新規書類作成して指定名称で新規保存.scpt
set dtPath to (path to documents folder) as string
set uuidStr to (do shell script "uuidgen") & ".key"
set savePath to dtPath & uuidStr

tell application "Keynote"
  set nDoc to make new document with properties {document theme:theme id "Application/21_BasicWhite/Standard", width:1024, height:768}
  
save nDoc in file savePath as Keynote
end tell

★Click Here to Open This Script 

AppleScript名:Pagesで新規書類を作成して指定名称で新規保存.scpt
set dtPath to (path to documents folder) as string
set uuidStr to (do shell script "uuidgen") & ".pages"
set savePath to dtPath & uuidStr

tell application "Pages"
  set nDoc to make new document with properties {document template:template id "Application/Blank/ISO"}
  
save nDoc in file savePath as Pages Format
end tell

★Click Here to Open This Script 

AppleScript名:Numbersで新規書類作成して指定名称で新規保存.scpt
set dtPath to (path to documents folder) as string
set uuidStr to (do shell script "uuidgen") & ".numbers"
set savePath to dtPath & uuidStr

tell application "Numbers"
  set nDoc to make new document
  
save nDoc in file savePath as numbers –change "Numbers" word into "numbers format" because "numbers" is alredy registered as "list of number" or "every number"
end tell

★Click Here to Open This Script 

(Visited 8 times, 1 visits today)
Posted in Bug news | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote Numbers Pages | Leave a comment

新発売:AppleScript基礎テクニック集(11)AppleScriptアプレットとドロップレット

Posted on 5月 20 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第11巻、「AppleScriptアプレットとドロップレット」です。PDF 41ページ、サンプルAppleScriptのZipアーカイブを添付。

→ 販売ページ

本シリーズも、着手した頃には「そんなに細切れのテーマで書けるのか?」「そんな作り方で作れるはずがない」といった意見が集まっていたものですが(チーム内の打ち合わせ時には割と批判的です)、実際に作り出したらトンでもなく「書くべきこと」があって、書けば書くほど新たなテーマが見つかるという恐るべき状態。

今回の「アプレット」「ドロップレット」については、あまり振り返ったことがなかったのですが、いざ書いてみると尋常でないほどの情報が溜まっていました。

スクリプトエディタとScript Debuggerという2つの開発環境のちがい、アプレット、ドロップレット、Cocoa-AppleScriptアプレット、Script Debuggerの拡張ドロップレットなどなど。常駐タイプのアプレットに、タイマー割り込み処理のアプレット。アプレットのアイコンのカスタマイズに、アプレット内にAppleScriptライブラリを同梱する方法、Cocoa Frameworkを入れて呼び出す方法など、盛りだくさんな内容をお送りしています。

目次

■アプレット

AppleScriptをアプリケーション形式で書き出し
AppleScriptアプレットの種類
AppleScriptアプレットの作成方法
AppleScriptアプレットのオプション指定
常駐型AppleScriptアプレットの作成方法
常駐型AppleScriptアプレットの記述
アプレットの「初期画面」を指定
「初期画面」に画像をペーストした場合の動作
アプレット/ドロップレットのユーザーインタフェース
資料:スクリプト/アプレットのバンドル構造
資料:アプレットのカスタマイズ①
資料:アプレットのカスタマイズ②
資料:アプレットのカスタマイズ③

■Cocoa-AppleScriptアプレット

Cocoa-AppleScriptアプレットの作成方法
参考資料:Delegateスクリプトの内容確認方法
参考資料:Delegateスクリプトのヘッダー内容
参考資料:Delegateスクリプトのハンドラ

■Script Debuggerのアプレット/拡張アプレット

AppleScriptアプレット(SD)の作成方法
AppleScript拡張アプレット(SD)の作成方法
AppleScript拡張アプレット(SD)の作成方法

■ドロップレット

AppleScriptドロップレットの作成方法
AppleScriptドロップレットの作成方法
ドロップレットによるファイルの受信
macOS 10.12以降の問題への対策ドロップレット
macOS 12.4上でドロップレット処理を実験

(Visited 12 times, 1 visits today)
Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Keynoteで選択中のGroup内オブジェクトを座標でソートし、テキスト抜き出し

Posted on 5月 15 by Takaaki Naganoya

Keynote v12.0以降でオープン中の最前面の書類の、表示中のスライド(ページ)上で選択中のグループから、その内部に収められているオブジェクトからテキストを取り出し、リスト(配列)にまとめるAppleScriptです。


▲書類上のオブジェクト(グループ)を選択した状態


▲1冊分のデータがグループにまとめられており、グループ内にオブジェクトを格納

いったんデータを組み上げた「書類」からデータを抜き出すのは、AppleScriptの重要な仕事です。各データ内のフィールドについて座標情報から自動判別するようにしていますが、座標値が想定どおりになっていない場合もあるため、これでもまだ不十分なので、相対的な位置関係(左上、右上、下 などといった)を定義してデータを取り出すようにするとよいでしょう。

……そこまで高度なものを作らなくても、文字列の長さに着目してソートしたら瞬殺でした。

AppleScript名:選択中のGroup内オブジェクトを座標でソートし、テキスト抜き出し.scpt
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

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

script spd
  property allList : {}
  
property aaSel : {}
end script

set (allList of spd) to {}

tell application "Keynote"
  set aVer to version
  
if aVer < "12.0" then return
  
  
set acceptClass to {text item, shape}
  
  
tell front document
    set (aaSel of spd) to selection –need Keynote v12.0 or later
    
    
repeat with i in (aaSel of spd)
      set j to contents of i
      
set aClass to class of j
      
      
if aClass = group then
        tell j
          set gItemList to every iWork item
        end tell
        
        
set posList to {}
        
set aCount to 1
        
        
repeat with ii in gItemList
          set jj to contents of ii
          
set bClass to class of jj
          
          
if bClass is in acceptClass then
            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}
          end if
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, {"positionY", "positionX"}, {true, true}) of me
        
        
–1グループ分のテキストを組み立てる
        
set tmpList to {}
        
repeat with ii in sortedList
          set jj to contents of ii
          
set aStr to myStr of jj
          
set the end of tmpList to aStr
        end repeat
        
set the end of (allList of spd) to tmpList
      end if
    end repeat
  end tell
end tell

return (allList of spd)
–> {{"変数名", "❺", "意外と苦労している人が多い、変数やプロパティ名の命名ルール。とくに、予約語と衝突しない名前について"}, {"条件分岐", "❼", "条件分岐はプログラムに必須。自転車でいえば、カーブで曲がれないとか、水たまりを避けられないぐらい困ります。"}, {"用語辞書", "❹", "読めないと書けない、でも、あの人もこの人も読めない用語辞書。自由自在に読めれば、自由自在に書ける!"}, {"ループ処理", "➓", "プログラムをシンプルに書くための一番大事な構文。書き方によって速度が違ったり、高度な処理を簡潔に書ける!"}, {"アプレット", "⓫", "AppleScriptをアプリケーションとして書き出す、簡易アプリケーション「アプレット」の最新動向!"}, {"間接指定", "❶", "対象としたデータだけでなく、幅広いデータに対応できるよう、プログラムに柔軟性を与える間接指定"}, {"ダイアログ表示", "❾", "簡単に行えるメッセージ表示手段。外部ライブラリの利用でさらなる高機能ダイアログを紹介。"}, {"環境整備", "❽", "macOSに標準で入っているツールを呼び出しやすくしたり設定したりする程度。全体像を掴んでレッツ環境整備。"}, {"フィルタ参照", "❻", "AppleScript独特の概念で、「膨大なデータから正規表現で絞り込む」などの他の処理系と異なる、基礎にして奥義"}, {"tellブロック", "❷", "AppleScriptの7割以上を占める、tellブロックの記述や整理方法を楽に書く秘訣のかずかず"}, {"❸", "ファイルパス", "まさに「基礎」中の「基礎」。「できて当たり前」といえるパス操作。知っているだけで差がつく基礎"}}

–リストに入れたレコードを、指定の属性ラベルの値でソート
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

★Click Here to Open This Script 

(Visited 12 times, 1 visits today)
Posted in list Sort | Tagged 12.0savvy Keynote | Leave a comment

新発売:AppleScript基礎テクニック集(10)ループ処理

Posted on 5月 13 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第10巻、「ループ処理」です。PDF 33ページ、サンプルAppleScriptのZipアーカイブを添付。

→ 販売ページ

ループ処理について、さまざまなrepeat文のバリエーションをまんべんなくすべて知って理解するよりも「間違いなく必要な場所で使える」ことが重要です。

repeat文にもいくつか書き方はあるものの、おおよそ2パターンぐらいでだいたい足ります。

それでも、複数の配列を同時にループしなくてはならないとか、ステップ値を想定の値に合わせるための調整とか、ノウハウらしきものはいろいろあります。

また、参照値をそのまま使うとエラーになる例もあるため、「contents of」でオブジェクトを取り出す必要があるといったささいなノウハウが必要なものでもあります。

目次

■ループによる繰り返し処理

AppleScriptの制御構文
繰り返しループ
repeat文①
repeat文②
repeat文③
repeat文④
repeat文⑤
repeat文⑥
exit repeat
多重ループ+exit repeat

■アプリケーションから取得したオブジェクトでループ

アプリケーションのオブジェクトを処理
Photos上の選択中の写真から情報を取得
Keynoteで選択中のオブジェクトを位置でソート
Numbersで表のセルが連結されていたら分離
targetが重複しているFinder Windowをクローズする
ミュージック.appで選択中のtrackの情報取得
大量のオブジェクト受信に備える

■高度な処理を簡潔に記述

複数の配列の同時ループ
ページ分け
再帰処理

(Visited 18 times, 1 visits today)
Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Safariで表示中のYouTubeムービーのサムネイル画像を取得

Posted on 5月 9 by Takaaki Naganoya

Safariで表示中のYouTubeムービーのサムネール画像を取得、保存、表示するAppleScriptです。

YouTubeのムービーのサムネール画像の取得方法を確認し、動作確認用にダイアログ表示+画像保存の機能を追加したものです。Script Debugger上で動かしている分には、NSImageの内容を結果表示ビューワで自動表示されますが、ない人向けに付けた機能です。

画像自体は、「ピクチャ」フォルダにUUIDつきでPNG形式で保存します。

–> Download Script bundle with Library

掲載リストには、画像表示ライブラリが含まれていないため、そのままでは実行できません。上記のScript Bundleをダウンロードして実行する必要があります。

AppleScript名:Safariで表示中のYouTubeムービーのサムネイル画像を取得.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/05/09
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use imgLib : script "imageDisplayLib"

property NSUUID : a reference to current application’s NSUUID
property |NSURL| : a reference to current application’s |NSURL|
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSURLComponents : a reference to current application’s NSURLComponents
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
property NSMutableDictionary : a reference to current application’s NSMutableDictionary

tell application "Safari"
  tell front document
    try
      set aURL to URL
    on error
      set aURL to "https://www.youtube.com/watch?v=_fmDtIV9vvI"
    end try
  end tell
end tell

if aURL does not start with "https://www.youtube.com/watch?" then return

set urlDict to parseURLParamsAsDict(aURL) of me
set aParam to urlDict’s valueForKey:"v"
if aParam = missing value then return

set imgURL to "https://i1.ytimg.com/vi/" & (aParam as string) & "/mqdefault.jpg"
set newURL to |NSURL|’s URLWithString:imgURL
set aImg to NSImage’s alloc()’s initWithContentsOfURL:newURL

set imgPath to (POSIX path of (path to pictures folder) & ((aParam as string) & "_") & (NSUUID’s UUID()’s UUIDString()) as string) & ".png"

–Save
saveNSImageAtPathAsPNG(aImg, imgPath) of me

–Display
dispImage(aImg, "YouTube thumbnail") of imgLib

on parseURLParamsAsDict(aURL)
  set components to NSURLComponents’s alloc()’s initWithString:aURL
  
set qList to (components’s query())’s componentsSeparatedByString:"&"
  
  
set paramRec to NSMutableDictionary’s dictionary()
  
  
repeat with i in qList
    set keyAndValues to (i’s componentsSeparatedByString:"=")
    (
paramRec’s setObject:(keyAndValues’s objectAtIndex:1) forKey:(keyAndValues’s objectAtIndex:0))
  end repeat
  
  
return paramRec
end parseURLParamsAsDict

–NSImageを指定パスにPNG形式で保存
on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep
  
  
set pathString to NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
  
set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value))
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
  
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

(Visited 29 times, 1 visits today)
Posted in Image Record Text URL | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy Safari | Leave a comment

新発売:AppleScript基礎テクニック集(9)ダイアログ表示

Posted on 5月 6 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第9巻、「ダイアログ表示」です。PDF 43ページ、サンプルScript、AppleScriptライブラリのZipアーカイブを添付。

→ 販売ページ

ダイアログ表示については、たいへん基礎的な内容でありながらも数多くの機能が盛り込まれています。サードパーティ側でさまざまなダイアログを作れるようになってきて、「みんなが思っているよりも、けっこう便利」な環境ができています。

利用するかしないかは、皆様の判断によると思いますが、実際に利用した方は割と便利で手放せなくなるようです。

目次

ダイアログ表示

標準搭載のさまざまなダイアログ表示機能
メッセージ表示系ダイアログ
メッセージ表示系ダイアログ
ファイル選択ダイアログ
フォルダ選択ダイアログ
ファイル名選択ダイアログ
多項目選択系ダイアログ
色選択ダイアログ
LAN内のリモート資源の選択
ローカルのアプリケーションの選択
リモート・アプリケーションの選択

AppleScriptライブラリを利用したダイアログ表示

AppleScriptライブラリの用語辞書の確認方法
筆者のライブラリにはサンプルScript掲載
iPhoneやiPad、MacとAirDrop通信
チェックボックス選択ダイアログを表示
日付選択ダイアログを表示
場所選択ダイアログを表示
複数ポップアップ選択ダイアログを表示
指定のRTF書類から抽出した書式を選択するダイアログ
指定の場所を異なる拡大倍率で地図表示
表形式のデータをダイアログ表示
ラベルつきテキストフィールドを表示/データ修正
指定色の一覧選択ポップアップメニューを表示
Scriptでカスタマイズ可能な複雑なダイアログ

(Visited 18 times, 1 visits today)
Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

Keynoteのタイトル内の強制改行コードを置換….できない?

Posted on 5月 3 by Takaaki Naganoya

Keynote v12のスライド(ページ)内のタイトル(default title item)内のテキスト(object text)の強制改行を置換(削除)できないという件についての試行錯誤です。

もともと、v12以前のバージョンのKeynoteでは、LF+LFを削除することで、タイトル内のテキストの改行削除は行えていました。

ただ、同様の処理では強制改行を削除し切れないようで….

--> {"AppleSript+NSImageでよく使う

基礎的な処理一覧", "画像ファイル
変換処理", "画像回転処理", "画像ファイルからの

情報取得処理", "画像リサイズ処理", "画像フィルタ処理"}

いまひとつすっきりしません。

Keynoteのタイトル文字列をコピーして、CotEditor(文字コードをAppleScriptと同じUTF16BEに変更)の書類上にペーストすると0Ahと表示するのですが、どうもKeynote上では異なるようで困ります。

AppleScript名:Keynote v12上の選択中のスライドのタイトルをテキスト化(改行削除)(未遂).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/05/03
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set repTargList to {string id 10 & string id 10, string id 10, string id 11, string id 13} –LF,CR,VTab一括置換

tell application "Keynote"
  tell front document
    set aSel to selection
    
set aList to {}
    
    
repeat with i in aSel
      set tmpClass to class of i
      
if tmpClass = slide then
        set tmpStr0 to object text of default title item of i
        
set hexList to retHexDumpedStr(tmpStr0) of me
        
set tmpStr1 to (paragraphs of tmpStr0) –パラグラフごとにlistにしてみた
        
log tmpStr1
        
set tmpStr2 to replaceTextMultiple(tmpStr1 as string, repTargList, "") of me as string
        
set the end of aList to tmpStr2
      end if
    end repeat
  end tell
end tell

return aList

–文字列の前後の改行と空白文字を除去
on cleanUpText(someText)
  set theString to current application’s NSString’s stringWithString:someText
  
set theString to theString’s stringByReplacingOccurrencesOfString:" +" withString:" " options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText}
  
set theWhiteSet to current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()
  
set theString to theString’s stringByTrimmingCharactersInSet:theWhiteSet
  
return theString as text
end cleanUpText

–指定文字列からCRLFを除去
on cleanUpCRLF(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set bString to aString’s stringByReplacingOccurrencesOfString:(string id 10) withString:"" –remove LF
  
set cString to bString’s stringByReplacingOccurrencesOfString:(string id 13) withString:"" –remove CR
  
set dString to cString as string
  
return dString
end cleanUpCRLF

–文字置換ルーチン
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

–任意のデータから特定の文字列を複数パターン一括置換
on replaceTextMultiple(origData as string, origTexts as list, repText as string)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to origTexts
  
set origData to text items of origData
  
set AppleScript’s text item delimiters to {repText}
  
set origData to origData as text
  
set AppleScript’s text item delimiters to curDelim
  
return origData
end replaceTextMultiple

on hexDumpString(aStr as string)
  set theNSString to current application’s NSString’s stringWithString:aStr
  
set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set theString to (theNSData’s |debugDescription|()’s uppercaseString())
  
  
–Remove "<" ">" characters in head and tail
  
set tLength to (theString’s |length|()) – 2
  
set aRange to current application’s NSMakeRange(1, tLength)
  
set theString2 to theString’s substringWithRange:aRange
  
  
–Replace Space Characters
  
set aString to current application’s NSString’s stringWithString:theString2
  
set bString to aString’s stringByReplacingOccurrencesOfString:" " withString:""
  
  
set aResList to splitString(bString, 2)
  
–> {​​​​​"E3", ​​​​​"81", ​​​​​"82", ​​​​​"E3", ​​​​​"81", ​​​​​"84", ​​​​​"E3", ​​​​​"81", ​​​​​"86", ​​​​​"E3", ​​​​​"81", ​​​​​"88", ​​​​​"E3", ​​​​​"81", ​​​​​"8A"​​​}
  
  
return aResList
end hexDumpString

–Split NSString in specified aNum characters
on splitString(aText, aNum)
  set aStr to current application’s NSString’s stringWithString:aText
  
if aStr’s |length|() ≤ aNum then return aText
  
  
set anArray to current application’s NSMutableArray’s new()
  
set mStr to current application’s NSMutableString’s stringWithString:aStr
  
  
set aRange to current application’s NSMakeRange(0, aNum)
  
  
repeat while (mStr’s |length|()) > 0
    if (mStr’s |length|()) < aNum then
      anArray’s addObject:(current application’s NSString’s stringWithString:mStr)
      
mStr’s deleteCharactersInRange:(current application’s NSMakeRange(0, mStr’s |length|()))
    else
      anArray’s addObject:(mStr’s substringWithRange:aRange)
      
mStr’s deleteCharactersInRange:aRange
    end if
  end repeat
  
  
return (current application’s NSArray’s arrayWithArray:anArray) as list
end splitString

–与えられたデータ内容をhexdumpして返す
on retHexDumpedStr(aStr)
  set aRes to do shell script "echo " & quoted form of aStr & " | hexdump -v "
  
return aRes
end retHexDumpedStr

★Click Here to Open This Script 

(Visited 12 times, 1 visits today)
Posted in Object control Text | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

お蔵入りした本:処理高速化

Posted on 4月 29 by Takaaki Naganoya

実際に出てきた本もあれば、出すのをストップした本もあります。

これ(↑)がそうで、作っているうちに「これ、基礎じゃなくね?」「なんか、上級者向けじゃね?」という話になって、ほとんど出来上がっていたのにストップしました。

内容は…..いい感じですし、内容も詰まっているので「基礎レベルではない」といった説明ができれば、割とそのまま(実例のコードを掲載しつつ)出してもいいとは思っています。

こんなかんじで。

こういうかんじでも。

(Visited 24 times, 1 visits today)
Posted in Books | Leave a comment

新発売:AppleScript基礎テクニック集(8)環境整備

Posted on 4月 28 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第8巻、「環境整備」です。PDF 42ページ、スクリプトエディタ上にAppleScriptのスニペットを入力する開発支援用Script集「Piyomaru Script Assistant v2.0」つき。

→ 販売ページ

AppleScriptの開発に必要なツールはmacOSに最初から入っています。整備も何も、追加で揃える必要がありそうなのは、Late Night SoftwareのScript Debuggerのお試し版ぐらいのものです。情報として伝えるなら100文字以内で終わってしまいそうなお話です。

それでも「難しい」と感じる方がいるとすれば、それはノウハウが分散していて、どれを使うとかどれをどう設定しておくといった話がまとめられていないから、ということなんでしょう。ここに、AppleScriptの開発環境がどういうものか、詳細にご紹介いたします。

環境整備〜超基礎編

自分でマシンの管理を行えることが必要
AppleScriptの開発環境〜安定動作の平均的な環境
キーボード/マウスの選択
プリンタは?
ネット接続
複数台あったほうが便利
iPhoneと共通のiCloud設定
Apple Developper Account

環境整備〜構築編

スクリプトエディタ:編集、実行、書き出し
アクティビティモニタ:プロセス監視、終了
Script Debugger:統合開発環境
作業手順をまとめておく:Keynote
各種AppleScriptライブラリ
Piyomaru Script Assistant
各種AppleScript対応アプリケーション

環境整備〜設定編

スクリプトエディタ:構文フォーマット設定
スクリプトエディタ:ライブラリ設定
スクリプトメニュー
Finderの拡張子設定を使用環境で統一
システム環境設定
システム環境設定

環境整備〜メンテナンス編

定期的な再起動
定期的なディスクメンテナンス
異常プロセスの監視

(Visited 16 times, 1 visits today)
Posted in Books news | Tagged 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(7)条件分岐(if文)

Posted on 4月 26 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第7巻、「条件分岐(if文)」です。PDF 34ページ、掲載Scriptアーカイブつき。

→ 販売ページ

if文が書けないとプログラムを書けないも同然。自転車でいえば、カーブで曲がれ ないとか、水たまりを避けられない走り方しかできない状態です。if文で華麗なコー ナリングを決められたら、初心者レベルは超えられたことでしょう。

if文とは?

条件分岐ってなに?
if文による条件分岐
if文による条件分岐
if文による条件分岐
if文で気をつけるべき点

比較演算とは?

比較演算ってなに?(数値)〜同一比較
比較演算ってなに?(数値)〜大小比較
参考資料:比較演算の演算子
文字の大小比較演算
特別な文字比較を行えるオプション
特別な文字比較のサンプル
複数の条件をリストにして、is inで照合
文字を含む、始まる、終わる演算
論理の反転
参考資料:is in演算子のはたらき
参考資料:「計算機」で16→10進数変換

より高度な条件分岐

UnicodeのNormalizeを強制して文字比較
アプリケーションのオブジェクト同士の照合
異なるアプリケーションのオブジェクト同士の照合

(Visited 19 times, 1 visits today)
Posted in Books news | Tagged 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(6)フィルタ参照

Posted on 4月 24 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第6巻、「フィルタ参照」です。PDF 41ページ、掲載Scriptアーカイブつき。

→ 販売ページ

「フィルタ参照」はAppleScript独特の概念であり、「膨大なデータから正規表現で絞り込む」といった他の環境でありがちな方法とは異なります。

データのしぼりこみができないものと誤解されたり、説明しておく必要性をとても感じるものです。本書が好評を得られるかといった話は度外視してでも、書かないとダメという存在です。

フィルタ参照

条件に合うものを抽出するフィルタ参照①
条件に合うものを抽出するフィルタ参照②
条件に合うものを抽出するフィルタ参照③
フィルタ参照に使われる予約語
フィルタ参照の書き方
フィルタ参照の書き方
フィルタ参照の書き方
オブジェクトの各属性を指定して抽出
とくに指定しにくい属性値
フィルタ参照の傾向と対策
フィルタ参照の注意点

より高度なフィルタ参照

複数条件でしぼりこむ
開始日、終了日を指定
まとめて設定(無条件フィルタリング)
任意の項目をランダムで抽出

ひかえめなフィルタ参照

直接指定できない場合に、間接指定的に使う①
直接指定できない場合に、間接指定的に使う②

アプリケーションのフィルタ参照例紹介

メール(Mail)
リマインダー(Reminders)
メモ(Notes)
連絡先(Contacts)
ミュージック(Music)
Keynote
Pages
Pages:ページ中の指定サイズの文字のテキスト
Numbers

(Visited 25 times, 2 visits today)
Posted in Books news | Leave a comment

PFiddlesoft UI Browserが製品終了に

Posted on 4月 23 by Takaaki Naganoya

AppleScriptの環境を構成する大きな要素部品である「PFiddlesoft UI Browser」が、開発者のBill Cheesemanのリタイアに伴い、製品終了になることが2022/4/17に表明されました。

GUI Scriptingの登場と同時に、この技術を完全にカバーし、生産性を格段に向上させるツールとしてUI Browserが登場しました。これを持っているかどうかでGUI ScriptingによるScript記述は天と地ほども差が出ます。

AppleScriptを書くのであれば、まず必携といっても過言ではないというツールであったわけです。

Script DebuggerとUI Browserを柱として、AppleScript系の開発環境が構築されてきたわけで、たいへんに重要なパーツであったことは誰にも否定できないことでしょう。

Bill Cheesemanの動向は定期的にウォッチしていたのですが、「引退した法律家からプログラマに転身」「いいかげん、けっこう高齢」といった認識は持っていたものの、「PreFab Software」から改名した「PFiddlesoft」というユニット名を使っていたあたりで「誰かに引き継ぎを考えているんだろう」と(勝手に)思っていました。

ただ、C++でゴリゴリにmacOSの深いところを叩きまくるようなアクセシビリティ系のプログラムを組むことはストレスが大きかったことでしょう。Cocoaの上からAppleScriptでOSのサービスを叩いていても「またバグが」「勝手な仕様変更が」などと苛立たしいことこの上ないわけで、もっと深い部分で叩いていたら、よりストレスは大きなものとなっていたことでしょう。

正直、このあたりの技術について同等の理解と技術力を有している人間といわれると、世界でも6人ぐらいしかいないことでしょう。Shane、Mark、Sal、Has、あとはAppleの現役エンジニア(世間せまっ!)。

その割にユーザーサポートの負担なども重圧となっていたことでしょうし、Billが続けられないという英断をしたことについては尊重すべきだと考えます。

とはいえ、その大きすぎる「穴」をどうやって埋めていくかというテーマを抱えてしまっているわけです。LateNight Softwareが継承する、とかいった話は(技術的な)可能性としてありそうではあるものの、(マーケティング的かつ費用的な意味では)ちょっとわかりません。

UI Browserの代替技術、代替製品という意味では「Piyomaru Dynamic Menu Clicker」があります。メニューについていえば、「このアプリケーションのこのメニューのこの項目」と指定すると、それを強制的にクリックするような仕組みをすでに作れています。

ただ、Webブラウザ上のコンテンツをGUI Scriptingを用いて操作するような用途では、ちょっと代替手段を持っていません。

UI Browserの基礎的な部品を各種Frameworkとして提供しているものの、ちょっとAppleScriptから気軽に叩いて機能を呼び出すというのは難しそうだと思われました。

UI Browser自体、販売終了するということなので、必要な人はすぐに購入すべきでしょう。そして、Webサイトも閉じる予定とのことなので、必要な資料などをPFiddlesoftのWebサイトからダウンロードしてバックアップしておくべきでしょう。

(Visited 29 times, 1 visits today)
Posted in GUI Scripting news | Tagged 12.0savvy | Leave a comment

新発売:AppleScript基礎テクニック集(5)〜変数、プロパティ名

Posted on 4月 20 by Takaaki Naganoya

電子書籍の新刊を発売しました。新シリーズ「AppleScript基礎テクニック集」の第5巻、「変数、プロパティ名」です。PDF 31ページ、掲載Scriptアーカイブつき。

→ 販売ページ

変数名の決め方……自分がそこで困ったり悩んだり苦しんだりしたことはないのですが、いろいろ聞いてみると意外なほど困っている様子。え、そこなの????

長い変数名、短い変数名、プロパティとグローバル変数はどこが違うの? といった聞きたいけど聞く相手がいないといった「素朴な疑問」をなるべくあぶり出して、明確になるようまとめてみました。PDF 31ページ、掲載ScriptのZipアーカイブを添付。

変数/プロパティとは?

変数ってなに?
変数に入れられるものは?
ローカル変数とグローバル変数
変数の型ってあるの?
変数の型の確認方法は?
propertyと変数の違い(1/2)
propertyと変数の違い(2/2)
暗黙のrunハンドラ
暗黙のrunハンドラ内の変数
propertyの有効範囲=スコープ
propertyの有効範囲を超える
暗黙のrunハンドラ内の変数と各ローカル変数との衝突?

変数/プロパティの名前の決め方

変数名の命名ルール
「予約語」と衝突しない命名を
「予約語」と衝突しない命名を
アプリケーションの予約語
AppleScriptの単語が何として認識されているか
かんたんな変数名/プロパティ名の付け方
変数/プロパティの運用の要点

(Visited 21 times, 1 visits today)
Posted in Books news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

macOS 12.4beta1で「GUIなしツールから起動したAppleScriptでUTIを取得できない」バグ?

Posted on 4月 18 by Takaaki Naganoya

スクリプトメニューやService Stationなど、「メニューやウィンドウを持たないGUIなしツール」がたくさんmacOS環境にありますが、macOS 12.4上ではこれらのAppleScript起動ツールから起動したAppleScriptから、UniformtypeIdentifiers.frameworkを用いたUTIの取得をブロックされているようです。

スクリプトメニューで実行を確認した(途中までで実行がブロックされてしまった)AppleScriptを、スクリプトエディタ上で実行したところ、問題なく実行されます。ちなみに、内容は選択した画像ファイルをPixelmator Proで超解像処理してクリップボードに設定する(コピーする)という「おかわいらしい」内容のものです。

Service Stationは、コンテクストメニューからのAppleScript実行を可能にするAppleScript実行ツールですが、これについてもしかるべきハンドラを書いて同じAppleScriptを実行させるようにしてみたものの、UTIの取得をブロックされるようです。

→ 追試で、Terminal.app上からosascript経由で実行してみたところ、UniformtypeIdentifiers.frameworkの呼び出しに失敗することが判明

明示的にMain threadで実行しないとダメ???>UniformtypeIdentifiers.framework

AppleScript名:🟧選択中の画像を超解像💝処理してコピー.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/03/25
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

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

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

set acceptUTI to "public.image"

set aFile to choose file
(*
tell application "Finder"
  set aFile to first item of (selection as alias list)
end tell
*)

–set aUTI to getUTIFromFile(aFile) of me –選択ファイルからUTIを
–display dialog aUTI
–if aUTI is equal to missing value then return

–set uRes to filterUTIList({aUTI}, acceptUTI) of me –選択ファイルのUTIが、受付可能UTIに含まれるかどうかチェック
–if uRes is equal to {} then return –選択したファイルが画像ではなかった

–掃除
tell application "Pixelmator Pro" to close every document without saving

–取得した画像を超解像処理してコピー
tell application "Pixelmator Pro"
  try
    open aFile
  on error
    close every document without saving
    
return
  end try
  
  
tell front document
    with timeout of 3000 seconds
      super resolution
    end timeout
  end tell
end tell

tell application "Pixelmator Pro"
  tell front document
    select all
    
copy
    
close without saving
  end tell
end tell

on getUTIFromFile(aFile)
  set aPath to POSIX path of aFile
  
  
set aWS to current application’s NSWorkspace’s sharedWorkspace()
  
set pRes to (aWS’s isFilePackageAtPath:aPath) as boolean
  
if pRes = false then
    set superType to (current application’s UTTypeData)
  else
    set superType to (current application’s UTTypePackage)
  end if
  
  
set pathString to current application’s NSString’s stringWithString:aPath
  
set aExt to (pathString’s pathExtension()) as string
  
  
set aUTType to current application’s UTType’s typeWithFilenameExtension:aExt conformingToType:(superType)
  
  
set aUTIstr to aUTType’s identifier() as string
  
return aUTIstr
end getUTIFromFile

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

★Click Here to Open This Script 

(Visited 23 times, 1 visits today)
Posted in Bug news UTI | Tagged 12.0savvy Pixelmator Pro Service Station | Leave a comment

Keynoteで選択中のtext itemとshapeをもとにtext itemを敷き詰める

Posted on 4月 15 by Takaaki Naganoya

Keynote v12で正常化した「selection」を用いて、選択中のshapeオブジェクトの範囲に、選択中のtext itemを指定数敷き詰めるAppleScriptです。

–> Watch Demo Movie

Keynote書類上に、敷き詰める領域を示すshapeオブジェクトを1つ、敷き詰める内容を示すtext itemオブジェクトを1つ配置し、これら2つを選択して本Scriptを実行。

選択状態で実行。

指定の最小文字サイズから最大文字サイズ(Keynote書類上で選択したtext item中の文字サイズ)までの間で乱数選択しつつ、指定個数を指定エリア中に作成します。

AppleScript名:選択中のtext itemとshapeをもとにtext itemを敷き詰める.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/04/15
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

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

property textItem : missing value
property shapeItem : missing value

property objMax : 200 –生成するオブジェクト(text item)の個数
property fontSizeMin : 12 –生成するオブジェクト内のobject textのフォントサイズ(これ以下のサイズは生成しない)

tell application "Keynote"
  set aVer to (version as real)
  
if aVer < 12 then return
  
  
tell front document
    –書類上で選択中のオブジェクトを種別に抽出する
    
set aaSel to selection
    
if length of aaSel is not equal to 2 then error "You have to select one text item and one shape."
    
    
repeat with i in aaSel
      set j to contents of i
      
set tmpClass to class of j
      
      
if tmpClass = text item and textItem = missing value then
        set textItem to j
      else if tmpClass = shape and shapeItem = missing value then
        set shapeItem to j
      end if
    end repeat
    
    
if {textItem, shapeItem} contains missing value then error "Selected objects are not suitable"
    
    
    
–選択されたshapeオブジェクトから情報を取得
    
set aPos to (position of shapeItem)
    
set minX to item 1 of aPos
    
set maxX to minX + (width of shapeItem)
    
set minY to item 2 of aPos
    
set maxY to minY + (height of shapeItem)
    
    
–選択されたtext itemオブジェクトから情報を取得
    
set maxFSize to size of object text of textItem
    
set oText to object text of textItem
    
set tWidth to width of textItem
    
set tHeight to height of textItem
    
    
    
–shapeオブジェクトで指定された領域に、text itemを生成
    
repeat objMax times
      –座標を乱数で指定
      
set randomX to random number from minX to (maxX – tWidth)
      
set randomY to random number from minY to (maxY – tHeight)
      
–文字サイズを乱数で指定
      
set randomCSize to random number from fontSizeMin to maxFSize
      
      
tell current slide
        set tmpT to make new text item with properties {object text:oText, position:{randomX, randomY}, width:tWidth, height:tHeight} at the end
        
ignoring application responses
          set size of every character of object text of tmpT to randomCSize
        end ignoring
      end tell
      
    end repeat
  end tell
end tell

★Click Here to Open This Script 

(Visited 15 times, 1 visits today)
Posted in list | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

SF Symbolsを名称で指定してPNG画像化

Posted on 4月 15 by Takaaki Naganoya

SF Symbolsに収録されている文字を、名称で指定してデスクトップにPNG画像で書き出すAppleScriptです。

同フォントは、アプリ上のアイコン素材3,300個以上をフォント化し、再利用しやすくすることで、アイコンで何かの意図を示すことを規格化したいという、現代における象形文字の再発明みたいな存在です。

文化や人種を超えた、非言語コミュニケーションを促進するための基盤としてAppleが整備を行なっているものなんでしょう。文化を縦断したインフラみたいな。

アイコン素材を自分で作るのはつらいので、正直助かっています。Appleが配布しているSF Symbols 3アプリケーションを使って画面上でいろいろ探して、文字を指定できます。

文字を指定するのはできているのですが、フォントのファミリーとかマルチカラーとかの指定の方法がよくわかりません。

AppleScript名:SF Symbolsを名称で指定してPNG画像化 v1a.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/04/14
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.8" — Monterey (12) or later
use framework "Foundation"
use framework "AppKit"
use framework "QuartzCore"
use scripting additions

set aName to "paperplane.circle.fill"
set anImage to current application’s NSImage’s imageWithSystemSymbolName:(aName) accessibilityDescription:(missing value)
set resImg to resizeNSImage(anImage, 50.0) of me

set aDesktopPath to current application’s NSString’s stringWithString:(POSIX path of (path to desktop))
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingPathExtension:"png")

set fRes to saveNSImageAtPathAsPNG(resImg, savePath) of me

on resizeNSImage(aSourceImg, aScale)
  set aSize to aSourceImg’s |size|()
  
set newWidth to (width of aSize) * aScale
  
set newheight to (height of aSize) * aScale
  
  
set targFrame to current application’s NSMakeRect(0, 0, newWidth, newheight)
  
set targImage to current application’s NSImage’s alloc()’s initWithSize:{newWidth, newheight}
  
  
set aKey to {current application’s NSImageHintInterpolation}
  
set aObj to current application’s NSNumber’s numberWithInt:(current application’s NSImageInterpolationLow)
  
set hintRec to current application’s NSDictionary’s dictionaryWithObject:aObj forKey:aKey
  
  
targImage’s lockFocus()
  
aSourceImg’s drawInRect:targFrame fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeCopy) fraction:1.0 respectFlipped:true hints:hintRec
  
targImage’s unlockFocus()
  
  
return targImage
end resizeNSImage

–NSImageを指定パスにPNG形式で保存
on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep
  
set pathString to current application’s NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value))
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

(Visited 35 times, 1 visits today)
Posted in Font Image | Tagged 11.0savvy 12.0savvy SF Symbols | Leave a comment

Pixelmator Pro v2.4.1で新機能追加+AppleScriptコマンド追加

Posted on 4月 14 by Takaaki Naganoya

いまひとつ、「こういう表現を行いたいんだけど、どう操作していいのかわからない」感のあるPixelmator Pro。毎回、メニューを漁っては「これ、どう作るんだろうーなー」という手探り感があります。

Photoshopで育って、どっぷりPhotoshopで画像処理を理解した人がPixelmator Proを理解するのに、敷居の高さを感じます。これ、絶対に「こういう完成物を作りたい」という人向けに「こういう手順で操作すればいい」というマニュアル的なものが必要です。

さて、Pixelmator Proの新バージョンv2.4.1が登場。機械学習を利用した人物写真の自動切り抜き機能が目玉のようです。



Student with laptop photo created by freepic.diller – www.freepik.com

おお!まつげの箇所だけちょっと切れてしまったけど、これはいい感じ!



Lookstudio – jp.freepik.com によって作成された people 写真

上半身はほぼ完璧! こーれーはすごい。

いろんなスナップ写真を切り抜かせてみましたが、人物の全身が写っているとヒット率が高いようです。つまり、おかしな箇所を切り抜かれる危険性が低くなるということです。白いTシャツが抜ける危険性が高いようで、、、

Pixelmator 2.4.1 AppleScript用語辞書変更内容

export formatに「Motion」(Motion project.)が追加された
コマンド「select subject」追加。
コマンド「smart refine selection」追加。
コマンド「remove background」追加。
エレメント「color adjustments layer」「effects layer」追加。

クラス「image layer」がコマンド「remove background」と「decontaminate colors」に応答するようになった。
クラス「group laye」に要素「color adjustments layer」「effects layer」を追加
クラス「color adjustments layer」「effects layer」を追加

コマンド「draw elliptical selection」の同義語(draw oval selection)を廃止
コマンド「resize image」の同義語(adjust size、image size)を廃止
コマンド「resize canvas」の同義語(adjust canvas、canvas size)を廃止

冒頭の人物自動切り抜きを行うAppleScriptを書いてみると、こんな感じです。Pixelmator ProのAppleScript用語辞書にもサンプルScriptが掲載されています。

AppleScript名:人物自動切り抜き.scpt
tell application "Pixelmator Pro"
  tell front document
    remove background –v2.4.1 or later
  end tell
end tell

★Click Here to Open This Script 

(Visited 26 times, 1 visits today)
Posted in Image | Tagged 10.15savvy 11.0savvy 12.0savvy Pixelmator Pro | Leave a comment

Post navigation

  • Older posts

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

Google Search

Popular posts

  • ebook:アーケードゲーム「戦場の絆」僕らの15年戦争
  • macOS 12が正式リリースされる
  • macOS 12 beta5でAppleScript処理系の大幅なスピードアップ。M1もIntelも
  • AppleScriptからショートカット実行&ショートカット内でAppleScriptを実行
  • Xcode 13.2 〜 13.3は有害(正式版でもまだ有害)
  • M1 Macの各種温度センサーから値を取得する
  • Intel MacとApple Silicon Macの速度差〜画像処理
  • AS Publisher v18
  • 2021年に書いた価値あるAppleScript
  • 新刊 elgato STREAM DECK 徹底活用Mac+STREAM DECKで時短+作業効率化!! 刊行
  • macOS 11.5+Keynote 11.1でGUI Scriptingに障害?
  • CotEditorで選択範囲の行頭にある数字をリナンバーする v1
  • Stream Deck Softwareがバージョン5.1.2にバージョンアップ
  • 「AppleScript最新リファレンス」に追加ダウンロードコンテンツ
  • AppleScriptによるWebブラウザ自動操縦ガイド
  • Shortcuts Eventsでショートカットのインストール+実行
  • マウスの右クリックメニューをカスタマイズするService Station
  • Amazon EC2 M1 Macインスタンスが利用可能に
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • macOS 12.3 beta 5、ASの障害が解消される(?)

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1389) 10.14savvy (565) 10.15savvy (400) 11.0savvy (235) 12.0savvy (106) CotEditor (54) Finder (46) iTunes (19) Keynote (83) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (21) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (42) NSJSONSerialization (21) NSMutableArray (61) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWindow (17) NSWorkspace (20) Numbers (51) Pages (32) Safari (38) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (22) WKWebViewConfiguration (22)

カテゴリー

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

アーカイブ

  • 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