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

Pagesで選択中のテキストボックスの一番近くにある白い文字のボックスのテキストを取得

Posted on 1月 16 by Takaaki Naganoya

Pagesで作成した書類で、プログラムリストの上に配置したタイトルのテキストを取得するAppleScriptです。

割といきあたりばったりで作ってしまったツールです。

Pagesで電子書籍を作成し、レイアウトしたAppleScriptのプログラムリストのファイル名を求めるために、プログラムリストの上に配置した白い文字で記述したテキストフレーム(Pages上ではShapeオブジェクト)を特定します。

フィルタ参照で相対座標値を表現できるといいのですが、そういうのはできないので、地道に距離計算しています。

「上」「下」という相対的な位置関係を表現するのに、結局数値比較しかできないので、どうしたものかと考えていたのですが、結局この「上」という表現は用いずじまいでした。「一番距離が近いテキストフレーム、文字色は白っぽい」だけで割と正確に特定できたので、いいだろうかというところです。相対位置関係を表記するライブラリなども作っておくといいかもしれません。

予想外の要素が、白いとだけ思っていた文字色が、RGB値では若干ゆらいでいたので、そのあたりの辻褄合わせを地味にやっています。カラードメイン(色名をラフに計算する)系のライブラリを使えば「white」などと雑な表現で指定できたかもしれません。

実際に使っているものは、本Scriptにくわえて選択中のテキストフレームの内容をAppleScriptとしてメモリ上で構文確認とコンパイルを行なって、ここで取得したファイル名でAppleScriptとして保存する処理を行なっています。

AppleScript名:Pagesで選択中のテキストボックスの一番近くにある白い文字のボックスの名前を取得.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/01/16
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript
use scripting additions
use framework "Foundation"
use framework "OSAKit"

property |NSURL| : a reference to current application’s |NSURL|
property OSANull : a reference to current application’s OSANull
property NSString : a reference to current application’s NSString
property OSAScript : a reference to current application’s OSAScript
property OSALanguage : a reference to current application’s OSALanguage
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property OSALanguageInstance : a reference to current application’s OSALanguageInstance
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey

tell application "Pages"
  tell front document
    set aSel to selection
    
if aSel = {} then return
    
set aaSel to first item of aSel
    
    
set targPos to position of aaSel
    
set targCon to object text of aaSel –選択中のtext frame(Pages上ではShape)の本文
    
    
tell current page
      –文字が入っているiWork objectのみが対象
      
set tList to every shape whose object text is not equal to targCon and object text of it is not equal to "" –オブジェクト
      
set pList to position of every shape whose object text is not equal to targCon and object text of it is not equal to "" –座標
      
set cList to color of object text of every shape whose object text is not equal to targCon and object text of it is not equal to "" –文字色
    end tell
    
    
–クレヨンピッカーから指定しても、白色に若干の「ゆらぎ」があるようなので、数値比較で抽出
    
set aRes to findItemNums({65500, 65500, 65500}, cList) of me
    
set aLen to length of aRes
    
if aLen = 0 then
      display dialog "No Hit(error)"
      
    else if aLen = 1 then
      set tmpTarg to first item of aRes
      
set tmpTargTextFrame to item tmpTarg of tList
      
set oRes to object text of tmpTargTextFrame
      
    else
      set p2List to {}
      
repeat with i in aRes
        set j to contents of i
        
set the end of p2List to contents of item j of pList
      end repeat
      
      
set L2ItemNums to retNearestItemByPosition({targPos}, p2List) of me
      
set oRes to object text of (item (first item of L2ItemNums) of tList)
      
    end if
  end tell
end tell

return oRes

–RGBの値がaNumにlistで入ってくる{r, g, b}
–リスト中に入っている指定要素をサーチして、各チャネルの値よりも大きい場合に合致したとみなし、出現アイテム番号を返す(複数対応)
on findItemNums(aNum, aList)
  if aList = {missing value} then return {}
  
if aNum = {missing value} then return {}
  
  
set iCount to 1
  
set hitF to false
  
set hitList to {}
  
copy aNum to {aNum1, aNum2, aNum3}
  
  
repeat with i in aList
    set j to contents of i
    
if j is not equal to missing value then
      copy j to {tmpR, tmpG, tmpB}
      
      
if (tmpR > aNum1) and (tmpG > aNum2) and (tmpB > aNum3) then
        set the end of hitList to iCount
      end if
    end if
    
set iCount to iCount + 1
  end repeat
  
  
return hitList
end findItemNums

on retNearestItemByPosition(L1Pos, L2Pos)
  
  
set resItemNum to {}
  
  
repeat with i in L1Pos
    set j to contents of i
    
set iCount to 1
    
set tDList to {}
    
    
repeat with ii in L2Pos
      set jj to contents of ii
      
      
copy jj to {tmpX1, tmpY1}
      
copy j to {tmpX2, tmpY2}
      
      
if tmpX1 ≥ tmpX2 then
        set xDist to tmpX1 – tmpX2
      else
        set xDist to tmpX2 – tmpX1
      end if
      
      
if tmpY1 ≥ tmpY2 then
        set yDist to tmpY1 – tmpY2
      else
        set yDist to tmpY2 – tmpY1
      end if
      
      
set tArea to xDist * yDist
      
set t2Area to absNum(tArea) of me
      
set the end of tDList to {area:t2Area, itemNum:iCount}
      
set iCount to iCount + 1
    end repeat
    
    
set resList to sortRecListByLabel(tDList, "area", true) of me
    
–> {{itemNum:2, area:100}, {itemNum:3, area:1739}, {itemNum:4, area:3780}, {itemNum:1, area:4554}}
    
    
set tItem to itemNum of first item of resList
    
set the end of resItemNum to tItem
    
  end repeat
  
  
return resItemNum
end retNearestItemByPosition

on arrangeTargItemByItemNumList(L2Pos, L2ItemNums)
  set L3Pos to {}
  
repeat with i in L2ItemNums
    set j to contents of i
    
set the end of L3Pos to item j of L2Pos
  end repeat
  
  
return L3Pos
end arrangeTargItemByItemNumList

on absNum(q)
  if q is less than 0 then set q to –q
  
return q
end absNum

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set sortDesc to current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF
  
set sortedArray to aArray’s sortedArrayUsingDescriptors:{sortDesc}
  
set bList to (sortedArray) as anything
  
return bList
end sortRecListByLabel

★Click Here to Open This Script 

More from my site

  • Pagesで選択中の表を書籍掲載用にセル内容の整理(重複セルをまとめる)Pagesで選択中の表を書籍掲載用にセル内容の整理(重複セルをまとめる)
  • Pagesで選択中のテキストフレーム内のテキストを、指定記号の前まで太らせるPagesで選択中のテキストフレーム内のテキストを、指定記号の前まで太らせる
  • Pages書類の内容を伏せ字に v2Pages書類の内容を伏せ字に v2
  • Pages書類の内容を伏せ字にPages書類の内容を伏せ字に
  • 新刊電子書籍「Pages+AppleScriptで本をつくろう!」を刊行新刊電子書籍「Pages+AppleScriptで本をつくろう!」を刊行
  • iWork Appsでオブジェクトの削除を安全にiWork Appsでオブジェクトの削除を安全に
(Visited 2 times, 1 visits today)
Posted in list Text | Tagged 13.0savvy 14.0savvy 15.0savvy Pages | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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

Google Search

Popular posts

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

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (207) 13.0savvy (177) 14.0savvy (127) 15.0savvy (104) CotEditor (64) Finder (51) iTunes (19) Keynote (115) 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 (74) Pages (54) 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
  • 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年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