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

カテゴリー: diff

Numbersで選択中の2列のセルを比較して並べ直して書き戻す v2

Posted on 10月 14 by Takaaki Naganoya

Numbers上でデータを整理しているときに、必要になって組んでみたものです。少し大きいAppleScriptですが、ありもののルーチンをつなぎ合わせて書いたので、とくに画期的な何かを書いたということはありません。

もともと、Numbers上にまとめたデータ(2列分)があって、それらを同じものは同じ行に並べ、削除されたものは空欄、追加されたものは下に列挙するという整理を行なっていました。


▲処理前の状態。2列x処理を行う分の行を選択した状態で本AppleScriptを実行する(スクリプトメニューなどを推奨)


▲処理後の状態。このように整理を行う

この作業をまとめて行うものです。そんなに高速化に配慮しているわけではないので、巨大なデータを扱うのには(このままでは)向いていないかもしれません。ただ、高速化の余地はいろいろあります。

もう、これは分かりきっていた話ですが、Numbers向けに凝ったAppleScriptを書くと、Numbersの機能はほとんど使わずに、AppleScriptの配列やらNSObjectのNSArrayやらを使いまくった「Cocoaの機能のオンパレード」みたいなAppleScriptになります。これは、Numbersに絶望的に何も機能がないためで、データの入出力しか行う余地がありません。

AppleScript名:選択中の2列のセルを比較して並べ直して書き戻す v2
—
–  Created by: Takaaki Naganoya
–  Created on: 2024/10/14
—
–  Copyright © 2024 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set curTable to getCurTable() of me
if curTable = "" then return

set {s1Row, s1Column, e1Row, e1Column} to getSelectionRange(curTable) of me
–> {73, 3, 86, 4}

set colWidth to e1Column – s1Column + 1
–> 2

if colWidth is not equal to 2 then
  display dialog "Numbers上の表の選択幅が2ではないので、処理終了します" buttons {"OK"} default button 1
  
return
end if

set rowHeight to e1Row – s1Row + 1
–> 15

–縦方向に列のセルの値を取得(左側)
set v1List to getValueByRange(curTable, s1Column, s1Row, e1Row) of me

–スイープ(空白セルのmissing valueを除去)
set v1aList to sweepList(v1List, missing value) of me
–> {"com.apple.eloquence.fr-CA.Grandpa", "com.apple.eloquence.fi-FI.Grandpa", " com.apple.eloquence.de-DE.Grandpa", " com.apple.eloquence.pt-BR.Grandpa", "com.apple.eloquence.en-US.Grandpa", "com.apple.eloquence.es-ES.Grandpa", " com.apple.eloquence.en-GB.Grandpa", " com.apple.eloquence.it-IT.Grandpa", " com.apple.eloquence.es-MX.Grandpa"}

–縦方向に列のセルの値を取得(右側)
set v2List to getValueByRange(curTable, e1Column, s1Row, e1Row) of me

–スイープ(空白セルのmissing valueを除去)
set v2aList to sweepList(v2List, missing value) of me
–> {"com.apple.eloquence.de-DE.Grandpa", "com.apple.eloquence.en-GB.Grandpa", "com.apple.eloquence.en-US.Grandpa", "com.apple.eloquence.es-ES.Grandpa", "com.apple.eloquence.es-MX.Grandpa", "com.apple.eloquence.fi-FI.Grandpa", "com.apple.eloquence.fr-CA.Grandpa", "com.apple.eloquence.fr-FR.Grandpa", "com.apple.eloquence.it-IT.Grandpa", "com.apple.eloquence.pt-BR.Grandpa", "com.apple.eloquence.ja-JP.Grandpa", "com.apple.eloquence.ko-KR.Grandpa", "com.apple.eloquence.zh-CN.Grandpa", "com.apple.eloquence.zh-TW.Grandpa"}

set dList to getDiffBetweenLists(v1aList, v2aList) of me
–> {addItems:{"com.apple.eloquence.zh-TW.Grandpa", "com.apple.eloquence.ko-KR.Grandpa", "com.apple.eloquence.ja-JP.Grandpa", "com.apple.eloquence.zh-CN.Grandpa"}, minusItems:{}}

set newList to {}
set minusList to minusItems of dList

repeat with i in v1aList
  set j to contents of i
  
if j is not in minusList then
    if j is in v2aList then
      set the end of newList to j
    else
      set the end of newList to ""
    end if
  else
    set the end of newList to ""
  end if
end repeat

set addList to addItems of dList
set addList to sort1DNumList(addList, true) of me –昇順ソート

set new2List to newList & addList

set newLen to length of new2List
if newLen > rowHeight then
  display dialog "新規データが大きいため(" & (rowHeight as string) & "行に対して、データが" & (newLen as string) & "件)データをNumbersに描き戻せません"
  
return –元のデータ範囲よりも、新規データ範囲(行)が多かったら処理を打ち切る(将来アップデート予定。行挿入などで対処)
end if

repeat (rowHeight – newLen + 1) times
  set the end of new2List to ""
end repeat

setValueByRange(curTable, e1Column, s1Row, e1Row, new2List) of me

on setValueByRange(curTable, s1Column, s1Row, e1Row, vList)
  tell application "Numbers"
    tell curTable
      tell column s1Column
        set cList to cells s1Row thru e1Row
      end tell
      
      
set aCount to 1
      
repeat with i in cList
        set aVal to item aCount of vList
        
set value of i to aVal
        
set aCount to aCount + 1
      end repeat
      
    end tell
  end tell
end setValueByRange

on getValueByRange(curTable, s1Column, s1Row, e1Row)
  tell application "Numbers"
    tell curTable
      tell column s1Column
        set vList to value of cells s1Row thru e1Row
        
return vList
      end tell
    end tell
  end tell
end getValueByRange

on getCurTable()
  tell application "Numbers"
    tell front document
      tell active sheet
        try
          set theTable to first table whose class of selection range is range
        on error
          return "" –何も選択されてなかった場合
        end try
        
        
return theTable
      end tell
    end tell
  end tell
end getCurTable

on getSelectionRange(theTable)
  tell application "Numbers"
    tell theTable
      set aSel to properties of selection range
      
set selName to name of aSel
      
set {s1, s2} to parseByDelim(selName, ":") of me
      
      
–始点の情報を取得する
      
set s1Row to (address of row of range s1) as integer
      
set s1Column to (address of column of range s1) as integer
      
      
–終点の情報を取得する
      
set e1Row to (address of row of range s2) as integer
      
set e1Column to (address of column of range s2) as integer
      
      
return {s1Row, s1Column, e1Row, e1Column}
    end tell
  end tell
end getSelectionRange

on parseByDelim(aData, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set dList to text items of aData
  
set AppleScript’s text item delimiters to curDelim
  
return dList
end parseByDelim

on sweepList(aList, sweepItem)
  set bList to {}
  
repeat with i in aList
    if contents of i is not equal to sweepItem then
      set the end of bList to contents of i
    end if
  end repeat
  
return bList
end sweepList

–リストの差分を取得_v2
on getDiffBetweenLists(aArray as list, bArray as list)
  set allSet to current application’s NSMutableSet’s setWithArray:aArray
  
allSet’s addObjectsFromArray:bArray
  
  
–重複する要素のみ抜き出す
  
set duplicateSet to current application’s NSMutableSet’s setWithArray:aArray
  
duplicateSet’s intersectSet:(current application’s NSSet’s setWithArray:bArray)
  
  
–重複部分を削除する
  
allSet’s minusSet:duplicateSet
  
set resArray to (allSet’s allObjects()) as list
  
  
set aSet to current application’s NSMutableSet’s setWithArray:aArray
  
set bSet to current application’s NSMutableSet’s setWithArray:resArray
  
aSet’s intersectSet:bSet –積集合
  
set addRes to aSet’s allObjects() as list
  
  
set cSet to current application’s NSMutableSet’s setWithArray:bArray
  
cSet’s intersectSet:bSet –積集合
  
set minusRes to cSet’s allObjects() as list
  
  
return {addItems:minusRes, minusItems:addRes}
end getDiffBetweenLists

–1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DNumList(theList as list, aBool as boolean)
  tell current application’s NSSet to set theSet to setWithArray_(theList)
  
tell current application’s NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, aBool)
  
set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor}
  
return (sortedList) as list
end sort1DNumList

★Click Here to Open This Script 

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

CotEditorで2つの書類の行単位での差分検出

Posted on 3月 12 by Takaaki Naganoya

CotEditorで2つの書類をオープンしておき、行単位での差分を検出し、新規書類に結果を出力するAppleScriptです。

もともと、macOS搭載のFramework名一覧の差分を検出する資料を作成する必要があり、これを片付けるために作成しました。

本AppleScriptを実行すると……

のように、結果を出力します。それほど巨大なデータを想定して作ってはいないので、ほどほどの規模に抑えて利用してください。

AppleScript名:左右のドキュメントを比較.scpt
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

script spd
  property aList : {}
  
property bList : {}
end script

set (aList of spd) to {}
set (bList of spd) to {}

tell application "CotEditor"
  set dCount to count every document
  
if dCount is not equal to 2 then
    display notification "Error: This script needs only 2 documents"
    
return –Document number Error
  end if
  
  
tell window 1
    set w1Bounds to bounds
    
copy w1Bounds to {w1X1, w1Y1, w1X2, w1Y2}
    
set w1Doc to document of it
  end tell
  
  
tell window 2
    set w2Bounds to bounds
    
copy w2Bounds to {w2X1, w2Y1, w2X2, w2Y2}
    
set w2Doc to document of it
  end tell
  
  
if w1X1 > w2X1 then
    set {w1, w2} to {2, 1}
  else
    set {w1, w2} to {1, 2}
  end if
  
  
tell document w1 –左ウィンドウ
    set (aList of spd) to paragraphs of contents
  end tell
  
  
tell document w2 –右ウィンドウ
    set (bList of spd) to paragraphs of contents
  end tell
  
end tell

–ゴミ掃除
set (aList of spd) to cleanUp1DList((aList of spd), {"
"
}) of me

set (bList of spd) to cleanUp1DList((bList of spd), {"
"
}) of me

–差分計算
set (cList of spd) to getDiffBetweenLists((aList of spd), (bList of spd)) of me

–結果出力テキスト組み立て
set aRes to listToTextUsingDelim(addItems of (cList of spd), "")
set bRes to listToTextUsingDelim(minusItems of (cList of spd), "")

–結果の新規書類への出力
set d3 to makeNewCotEditorDoc("左→右で追加された項目" & return & return & aRes) of me
set d4 to makeNewCotEditorDoc("左→右で削除された項目" & return & return & bRes) of me

on getDiffBetweenLists(aArray as list, bArray as list)
  set allSet to current application’s NSMutableSet’s setWithArray:aArray
  
allSet’s addObjectsFromArray:bArray
  
  
–重複する要素のみ抜き出す
  
set duplicateSet to current application’s NSMutableSet’s setWithArray:aArray
  
duplicateSet’s intersectSet:(current application’s NSSet’s setWithArray:bArray)
  
  
–重複部分を削除する
  
allSet’s minusSet:duplicateSet
  
set resArray to (allSet’s allObjects()) as list
  
  
set aSet to current application’s NSMutableSet’s setWithArray:aArray
  
set bSet to current application’s NSMutableSet’s setWithArray:resArray
  
aSet’s intersectSet:bSet –積集合
  
set addRes to aSet’s allObjects() as list
  
  
set cSet to current application’s NSMutableSet’s setWithArray:bArray
  
cSet’s intersectSet:bSet –積集合
  
set minusRes to cSet’s allObjects() as list
  
  
return {addItems:minusRes, minusItems:addRes}
end getDiffBetweenLists

on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    end if
  end repeat
  
return bList
end cleanUp1DList

–デリミタ文字を指定してリストを文字列化
on listToTextUsingDelim(aList, aDelim)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set outText to every text item of aList
  
set outText to outText as string
  
set AppleScript’s text item delimiters to curDelim
  
return outText
end listToTextUsingDelim

–指定テキストでCotEditorの新規書類を作成
on makeNewCotEditorDoc(aCon)
  tell application "CotEditor"
    activate
    
set newDoc to make new document
    
tell newDoc
      set contents to aCon
    end tell
  end tell
end makeNewCotEditorDoc

★Click Here to Open This Script 

Posted in diff list Text | Tagged 13.0savvy 14.0savvy CotEditor | Leave a comment

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

Google Search

Popular posts

  • macOS 13.6.5 AS系のバグ、一切直らず
  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • CotEditorで2つの書類の行単位での差分検出
  • Apple純正マウス、キーボードのバッテリー残量取得
  • macOS 15, Sequoia
  • Cocoa-AppleScript Appletランタイムが動かない?
  • ディスプレイをスリープ状態にして処理続行
  • macOS 14の変更がmacOS 13にも反映
  • Finder上で選択中のPDFのページ数を加算
  • 初心者がつまづきやすい「log」コマンド
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • 当分、macOS 14へのアップデートを見送ります
  • macOS 14、英語環境で12時間表記文字と時刻の間に不可視スペースを入れる仕様に
  • macOS 13 TTS環境の変化について
  • メキシカンハットの描画
  • 新刊発売 AppleScript最新リファレンス v2.8対応
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • macOS 14, Sonoma 9月27日にリリース
  • 2023年に書いた価値あるAppleScript

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (586) 10.15savvy (436) 11.0savvy (280) 12.0savvy (200) 13.0savvy (118) 14.0savvy (66) 15.0savvy (33) CotEditor (62) Finder (49) iTunes (19) Keynote (108) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (64) Pages (50) Safari (44) Script Editor (23) 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
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • diff
  • drive
  • 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)
  • 未分類

アーカイブ

  • 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