AppleScriptから各種GUIアプリを操作すると、各種GUIアプリ上の操作に要する時間がそのまま必要になります。端的なところでいえば、書類のオープン時のアニメーションも毎回行われるわけで、割と無意味なアニメーションに、そこそこの時間が消費されています。
そのUIアニメーションを無効化する処理について調査したところ、割と簡単に情報が見つかりました。
ここに掲載しているサンプルAppleScript(要・Metadata Lib)では、Finder上の最前面のウィンドウで選択中のフォルダ以下にあるPages書類をSpotlightの機能を用いてすべてピックアップし、それらを「UIアニメーションあり」「UIアニメーションなし」の条件でオープン/クローズだけ行うものです。
結論をいうと、M2 MacBook Air上で通常のUIアニメーションつきの処理では68秒かかっていたものが、UIアニメーションを無効化するだけで43秒で処理できました(125個のPages書類で実験)。
これはちょうど、M1 MacからM4 Macに買い替えるぐらいの処理速度の向上が、ただUIアニメーションを無効にするだけで実現できたということを意味します。
1書類あたり0.2秒の処理速度向上が見られたわけですが、これが400個の書類を処理するとなれば80秒も変わってくるわけで、割と洒落にならない速度向上が実現できます。もっと早く調べておけばよかったと思うことしきりです。
AppleScript名:UI アニメーションの無効化による速度向上実験.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/09/22 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions use mdLib : script "Metadata Lib" tell application "Finder" set aSel to (selection as alias list) if aSel = {} then set origPath to choose folder else set origPath to (first item of aSel) end if end tell –SpotlightでPages書類を検出 set aResList to perform search in folders {origPath} predicate string "kMDItemContentType == %@ || kMDItemContentType == %@" search arguments {"com.apple.iwork.pages.sffpages", "com.apple.iwork.pages.pages"} –return aResList –UI Animationを許可 set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() openClosePagesDocs(aResList, true) of me set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to b1Dat – a1Dat –UI Animationを禁止 set a2Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() openClosePagesDocs(aResList, false) of me set b2Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c2Dat to b2Dat – a2Dat return {c1Dat, c2Dat, length of aResList} on openClosePagesDocs(aResList, animationF) if animationF = true then –UI Animationをオンにする try do shell script "defaults delete NSGlobalDomain NSAutomaticWindowAnimationsEnabled" end try else –UI Animationをオフにする do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool NO" end if –Pages書類を順次オープンしてタイトル(?)を取得 set pagesTitleList to {} repeat with i in aResList set aFile to POSIX file i tell application "Pages" open (aFile as alias) end tell tell application "Pages" close front document saving no end tell end repeat if animationF = false then –UI Animationをオンにする try do shell script "defaults delete NSGlobalDomain NSAutomaticWindowAnimationsEnabled" end try end if end openClosePagesDocs |
(Visited 4 times, 4 visits today)