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

カテゴリー: recursive call

再帰でKeynote書類上のフォントを置換する(グループ対応)

Posted on 11月 16, 2019 by Takaaki Naganoya

macOS 10.15でバンドルされなくなった「ヒラギノ角ゴProN W3」「ヒラギノ角ゴProN W6」がKeynote書類中に指定されていたら、それぞれ「ヒラギノ角ゴシック W3」「ヒラギノ角ゴシック W6」に置換するAppleScriptです。

現在オープン中の最前面のKeynote書類を走査して、全スライド中のオブジェクトの内容を取得し、text item、shape、table中に指定されているフォントを置換するようになっています。また、group化されたこれらのオブジェクトを再帰で取得するようになっています。

ただし、グラフ(chart)中の文字のフォントについてはAppleScriptからアクセスできないため、フォントの置換は行えません。このあたりでミソがついて、完全自動でヒラギノ角ゴProNを置換できるわけではないので、少々残念(KeynoteのAppleScript用語辞書の仕様の問題)な結果に。

また、別にこれらのフォントを置換しなくても実害はないので、置換しても単に気分の問題だけでしょう。

AppleScript名:再帰でKeynote書類上のフォントを置換する(グループ対応)
— Created 2019-11-14 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property allText : {}
property allShape : {}
property allTable : {}

set searchFont to {"HiraKakuProN-W6", "HiraKakuProN-W3"}

tell application "Keynote"
  tell front document
    set sCount to count every slide
    
    
repeat with ii from 1 to sCount
      set current slide to slide ii
      
      
tell current slide
        getEveryTextAndShapes() of me
        
        
–Text Item、Shapeのフォント置換ループ
        
set tList to allText & allShape
        
        
repeat with i in tList
          set aClass to class of i
          
–if aClass is not equal to default title item then
          
set aFont to font of object text of i
          
set aText to (object text of i)
          
if aFont is in searchFont and aText is not equal to "" then
            if aFont ends with "-W6" then
              set font of object text of i to "HiraginoSans-W6"
            else if aFont ends with "-W3" then
              set font of object text of i to "HiraginoSans-W3"
            end if
          end if
          
–end if
        end repeat
        
        
        
–Table内の全セルのフォント置換ループ
        
repeat with iii in allTable
          tell iii
            set cList to every cell
            
            
repeat with i in cList –Loop by cells
              set aFont to font name of i
              
set aText to (value of i)
              
              
if aFont is in searchFont then
                if aFont ends with "-W6" then
                  set font name of i to "HiraginoSans-W6"
                else if aFont ends with "-W3" then
                  set font name of i to "HiraginoSans-W3"
                end if
              end if
              
            end repeat
          end tell
        end repeat
        
        
      end tell
    end repeat
  end tell
end tell

on getEveryTextAndShapes()
  set allText to {}
  
set allShape to {}
  
set allTable to {}
  
  
tell application "Keynote"
    tell front document
      tell current slide
        set gList to every group
        
        
set i1List to every text item
        
set i2List to every shape
        
set i3List to every table
        
        
if i1List is not equal to {} then set allText to allText & i1List
        
if i2List is not equal to {} then set allShape to allShape & i2List
        
if i3List is not equal to {} then set allTable to allTable & i3List
        
        
prepareGroup(gList) of me
        
      end tell
    end tell
  end tell
end getEveryTextAndShapes

–Dig into groups by recursive call
on prepareGroup(gList)
  tell application "Keynote"
    tell front document
      tell current slide
        repeat with i in gList
          tell i
            set g2List to every group
            
            
set i1List to every text item
            
set i2List to every shape
            
set i3List to every table
            
            
if i1List is not equal to {} then set allText to allText & i1List
            
if i2List is not equal to {} then set allShape to allShape & i2List
            
if i3List is not equal to {} then set allTable to allTable & i3List
            
            
prepareGroup(g2List) of me
          end tell
        end repeat
      end tell
    end tell
  end tell
end prepareGroup

★Click Here to Open This Script 

(Visited 108 times, 1 visits today)
Posted in Font recursive call | Tagged 10.15savvy Keynote | Leave a comment

与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3

Posted on 6月 20, 2019 by Takaaki Naganoya

与えられた1次元配列のデータのすべての順列組み合わせパターンを計算し、そのとおりに配列を並べて連結した文字列を、すべての順列組み合わせパターンについて生成して返す(Permutations)AppleScriptです。

初版ではありもののサブルーチンを組み合わせて、検証を行うには十分なものでした(v2)。ただし、本来その用途に作成したものではないルーチンを寄せ集めて作ったので、組み合わせ数が増えると処理時間がかかりすぎるきらいがありました。

そこで、こうした処理(Permutations)を英語でどう呼ぶのかを調べ、AppleScriptの実装例を調べたら、灯台下暗し。MacScripter.netにありました。

ただし、そのままではいまひとつ使いにくかったので、きちんと組み合わせた文字列を1次元配列で返すように組んでみたところ、オリジナル版よりは処理時間はかかっていますが、初版からくらべると大幅に処理速度とメモリ使用効率が改善されました。

MacScripter.net掲載サンプル処理結果:{{“A”, “T”, “G”, “C”}….}
本Scriptの処理結果:{“ATGC”, “ATCG”…..}

4要素の順列組み合わせ計算:5倍速
5要素の順列組み合わせ計算:10倍速
6要素の順列組み合わせ計算:30倍速
7要素の順列組み合わせ計算:67倍速
8要素の順列組み合わせ計算:(計測不能)

というように、大規模データになればなるほど高速化の度合いが高くなります。AppleScriptにしては大規模データを扱う演算なので、Cocoaの機能を積極的に利用することで高速化を行なっています。

開発機として用いているMacBook Pro 2012 Retina(Core i7 2.6GHz)と、MacBook Pro 13 2017 (Dual Thunderbolt)で処理時間を計測してみたところ、2012のMBPのほうがわずかに高速でした。

AppleScript名:与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3.scptd
— 2014-10-06 Original By Nigel Garvey@macscripter.net
— 2019-06-19 Modified By Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

script spdPerm
  property permutations : missing value
end script

on run
  –Test by MacBook Pro 2012 Retina Core i7 2.6GHz
  
–set theList to {"A", "T", "G", "C", "1"} –0.05 secs (5 digits) 120 items.
  
–set theList to {"A", "T", "G", "C", "1", "2"} –0.21secs (6 digits) 720 items.
  
set theList to {"A", "T", "G", "C", "1", "2", "3"} –1.44 secs (7 digits) 5,040 items.
  
–set theList to {"A", "T", "G", "C", "1", "2", "3", "4"} –11.54 secs (8 digits) 40,320 items.
  
–set theList to {"A", "T", "G", "C", "1", "2", "3", "4", "5"} –107.27 secs (9 digits) 362,880 items.
  
  
set aRes to permute(theList) of me
  
return length of aRes
end run

on permute(theList as list)
  set theArray to current application’s NSMutableArray’s arrayWithArray:theList
  
set (permutations of spdPerm) to current application’s NSMutableArray’s array()
  
prmt(theArray, 0, (count theList) – 1)
  
  
–Return AppleScript string list
  
set aFinishArray to current application’s NSMutableArray’s new()
  
set anEnum to (permutations of spdPerm)’s objectEnumerator()
  
repeat
    set aValue to anEnum’s nextObject()
    
if aValue = missing value then exit repeat
    
set aStr to aValue’s componentsJoinedByString:""
    (
aFinishArray’s addObject:aStr)
  end repeat
  
  
return aFinishArray as list
end permute

on prmt(theArray, theStart as number, theEnd as number)
  if (theStart = theEnd) then
    (permutations of spdPerm)’s addObject:theArray
  else
    repeat with x from theStart to theEnd
      set theCopy to theArray’s mutableCopy()
      
–swap
      
if (x > theStart) then (theCopy’s exchangeObjectAtIndex:theStart withObjectAtIndex:x)
      
prmt(theCopy, theStart + 1, theEnd)
    end repeat
  end if
end prmt

★Click Here to Open This Script 

(Visited 127 times, 1 visits today)
Posted in list recursive call | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSMutableArray | 2 Comments

PDFのしおり(TOC)の内容を取得する v3

Posted on 4月 11, 2019 by Takaaki Naganoya

指定のTOCつきPDFからTOCをrecord in listで取得するAppleScriptです。各TOC項目のページ数、アウトラインレベル、ラベル(ページ名称)を順次取得して配列に出力します。

先日発売したBlogアーカイブ本のTOCを本Scriptで取得すると、以下のようになります(抜粋)。

{{pageIndex:1, outlineLevel:1, outLabel:"表紙"}, {pageIndex:2, outlineLevel:1, outLabel:"商標について"}, {pageIndex:3, outlineLevel:1, outLabel:"本書をご覧になるために"}, ... {pageIndex:34, outlineLevel:1, outLabel:"2011/1"}, {pageIndex:35, outlineLevel:2, outLabel:"リスト同士のdiffをとる"}, {pageIndex:40, outlineLevel:2, outLabel:"日本の月呼称を返す"}, {pageIndex:42, outlineLevel:2, outLabel:"指定の月を1月分リスト化"},...... {pageIndex:450, outlineLevel:1, outLabel:"奥付"}}

あれ? 「本書について」(2P)という記事が掲載もれしているのを、TOC出力から見つけてしまった、、、、、、、、

AppleScript名:PDFのしおり(TOC)の内容を取得する v3
— Created 2017-01-09 by Takaaki Naganoya
— Modified 2019-04-10 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

property |NSURL| : a reference to current application’s |NSURL|
property PDFDocument : a reference to current application’s PDFDocument
property titleList : {}

set my titleList to {}

set aFile to POSIX path of (choose file of type {"com.adobe.pdf"})

set fileURL to |NSURL|’s fileURLWithPath:aFile
set aPDFdoc to PDFDocument’s alloc()’s initWithURL:fileURL

–TOCの読み込み
set parentOL to aPDFdoc’s outlineRoot() –あらかじめTOCが存在していないとmissing valueになる
if parentOL is equal to missing value then
  display dialog "本PDFにはTOCが添付されていないため、処理を終了します" with title "No TOC Error:"
  
return
end if

getChilds(parentOL) of me
return (my titleList)
–> {{pageIndex:1, outlineLevel:1, outLabel:"表紙"}, {pageIndex:2, outlineLevel:1, outLabel:"商標について"}, {pageIndex:3, outlineLevel:1, outLabel:"本書をご覧になるために"}, {pageIndex:5, outlineLevel:1, outLabel:"まえがき"},…… {pageIndex:33, outlineLevel:1, outLabel:"2011年"}, {pageIndex:34, outlineLevel:1, outLabel:"2011/1"}, {pageIndex:35, outlineLevel:2, outLabel:"リスト同士のdiffをとる"}, {pageIndex:40, outlineLevel:2, outLabel:"日本の月呼称を返す"},…….}

–PDFOutlineを再帰で取得する
on getChilds(parentOL)
  set outLineStr to parentOL’s label()
  
set outLineCount to (parentOL’s numberOfChildren()) as number
  
  
repeat with i from 0 to (outLineCount – 1)
    set anOut to (parentOL’s childAtIndex:i)
    
    
–Label (Page Title)
    
set tmpOut to (anOut’s label()) as string
    
    
–Page Indel Label
    
set tmpInd to (anOut’s destination()’s page()’s label()) as integer
    
    
–Outline Level
    
set outlevel to 0
    
copy anOut to aTmpOut
    
repeat
      set aTmpOut to aTmpOut’s |parent|()
      
if aTmpOut = missing value then exit repeat
      
set outlevel to outlevel + 1
    end repeat
    
    
set the end of my titleList to {pageIndex:tmpInd, outlineLevel:outlevel, outLabel:tmpOut}
    
    
set tmpChild to (anOut’s numberOfChildren()) as integer
    
    
if tmpChild is not equal to 0 then
      getChilds(anOut) of me
    end if
  end repeat
end getChilds

★Click Here to Open This Script 

(Visited 185 times, 1 visits today)
Posted in list PDF Record recursive call | Tagged 10.12savvy 10.13savvy 10.14savvy NSURL PDFDocument | Leave a comment

PDFのしおり(TOC)の内容を取得する v2

Posted on 4月 10, 2019 by Takaaki Naganoya

指定のTOCつきPDFからTOCを1Dリスト(1次元配列)で取得するAppleScriptです。各TOC項目のlabelの文字列を順次取得して1次元配列に出力します。

AppleScript名:PDFのしおり(TOC)の内容を取得する v2
— Created 2017-01-09 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

property |NSURL| : a reference to current application’s |NSURL|
property PDFDocument : a reference to current application’s PDFDocument
property titleList : {}

set my titleList to {}

set aFile to POSIX path of (choose file of type {"com.adobe.pdf"})

set fileURL to |NSURL|’s fileURLWithPath:aFile
set aPDFdoc to PDFDocument’s alloc()’s initWithURL:fileURL

–TOCの読み込み
set parentOL to aPDFdoc’s outlineRoot() –あらかじめTOCが存在していないとmissing valueになる
if parentOL is equal to missing value then
  display dialog "本PDFにはTOCが添付されていないため、処理を終了します" with title "No TOC Error:"
  
return
end if

getChilds(parentOL) of me
return (my titleList)

–再帰処理してみた
on getChilds(parentOL)
  set outLineStr to parentOL’s label()
  
set outLineCount to (parentOL’s numberOfChildren()) as number
  
  
repeat with i from 0 to (outLineCount – 1)
    set anOut to (parentOL’s childAtIndex:i)
    
set tmpOut to (anOut’s label()) as string
    
set the end of my titleList to tmpOut
    
set tmpChild to (anOut’s numberOfChildren()) as integer
    
    
if tmpChild is not equal to 0 then
      getChilds(anOut) of me
    end if
  end repeat
end getChilds

★Click Here to Open This Script 

(Visited 56 times, 1 visits today)
Posted in list PDF recursive call | Tagged 10.12savvy 10.13savvy 10.14savvy NSURL PDFDocument | Leave a comment

Mail.appで選択中のフォルダ以下に存在する全メールの添付ファイルの拡張子のバリエーションを集計

Posted on 11月 25, 2018 by Takaaki Naganoya

Mail.appで選択中のフォルダ(メールボックス)以下に存在する全メールの添付ファイルの拡張子のバリエーションを集計するAppleScriptです。

階層フォルダで管理しているメールボックス(フォルダ)のうち、選択しているメールフォルダ以下のすべてのメッセージに添付されているファイルの拡張子を取得して結果を表示します。

何のために作ったとかいうことはなくて、ただ単に調査のために作ったものです。処理するメールの数にもよりますが、CPU負荷も大きく、時間もそれなりに(数分ぐらい?)かかります。

AppleScript名:Mail.appで選択中のフォルダ以下に存在する全メールの添付ファイルの拡張子のバリエーションを集計
—
–  Created by: Takaaki Naganoya
–  Created on: 2018/11/25
—
–  Copyright © 2018 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

script spd
  property fullSubPath : {}
  
property tmpMesList : {}
  
property outList : {}
end script

property aFullPath : ""
property tmpFullPath : ""
property extList : {}

–変数の初期化
set tmpFullPath to ""
set aFullPath to ""
set extList to {}
set (fullSubPath of spd) to {}
set (tmpMesList of spd) to {}
set (outList of spd) to {}

–選択中のメールボックス(フォルダ)のフルパスを文字列で取得(dir1/dir2/dir3)
set targMBObj to getSelectedOneMailBox() of me
extraxctTextFullPathOfMBObject(targMBObj) of me

copy aFullPath to targMB

if targMB = "" then
  display notification "Mail.app上で選択中のメールボックス(フォルダ)はありません。" –"No Mail.app folder selection" in Japanese
  
return
end if

–指定メールボックス(フォルダ)以下のすべてのメールボックス(フォルダ)を取得する
tell application "Mail"
  tell mailbox targMB
    set aList to name of every mailbox
  end tell
end tell

–指定フォルダ以下のMailboxをサブフォルダまで再帰ですべて取得する
repeat with i in aList
  set j to contents of i
  
set tmpPath to targMB & "/" & j
  
set the end of (fullSubPath of spd) to tmpPath
  
getEverySubMailbox(tmpPath) of me
end repeat

–全サブフォルダからメールを取得し、添付ファイルの確認を行なって、添付ファイルの拡張子を取得して集計
repeat with i in (fullSubPath of spd)
  set tmpBox to contents of i
  
  
tell application "Mail"
    tell mailbox tmpBox
      set mesList to (every message)
    end tell
    
    
–指定フォルダ中のMailでループ
    
repeat with ii in mesList
      set jj to contents of ii
      
      
try
        set attS to mail attachment of jj
      on error
        set attS to {}
      end try
      
      
if attS is not equal to {} then
        
        
–Mailの添付ファイルの拡張子を集計する
        
try
          repeat with iii in attS
            set jjj to contents of iii
            
set aName to name of jjj
            
            
set aExt to getPathExtension(aName) of me
            
            
if (aExt is not in extList) and (aExt is not equal to "") then
              set the end of extList to aExt
            end if
            
          end repeat
        end try
      end if
    end repeat
  end tell
end repeat

choose from list extList with prompt "選択中のメールフォルダ「" & targMB & "」中のメールに添付されているファイルの種別です" with title "添付ファイルの拡張子一覧"

–指定のメールボックス(フォルダ)以下にあるすべてのサブフォルダを(propertyに)追記して返す
on getEverySubMailbox(aPath)
  tell application "Mail"
    tell mailbox aPath
      set aaList to name of every mailbox
      
if aaList is not equal to {} then
        repeat with i in aaList
          set j to contents of i
          
set tmpFullPath to aPath & "/" & j
          
copy tmpFullPath to newPath
          
set the end of (fullSubPath of spd) to newPath
          
getEverySubMailbox(newPath) of me
        end repeat
      end if
    end tell
  end tell
end getEverySubMailbox

–Message Viewerで現在表示中のメールボックスの情報を1つのみ返す
on getSelectedOneMailBox()
  tell application "Mail"
    tell message viewer 1
      set mbList to selected mailboxes
    end tell
  end tell
  
if length of mbList is equal to 0 then
    return ""
  end if
  
  
set aMailBox to contents of (item 1 of mbList)
  
return aMailBox
end getSelectedOneMailBox

–Mail.appのメールボックスオブジェクトを渡すと、テキストのフルパスに変換
on extraxctTextFullPathOfMBObject(aPath)
  tell application "Mail"
    try
      set parentPath to container of aPath
    on error
      return
    end try
    
    
set meName to name of aPath
    
if aFullPath = "" then –1回目のみスラッシュを入れないで処理
      set aFullPath to meName
    else
      –通常処理はこちら
      
set aFullPath to meName & "/" & aFullPath
    end if
    
    
extraxctTextFullPathOfMBObject(parentPath) of me –再帰呼び出し
  end tell
end extraxctTextFullPathOfMBObject

on getPathExtension(aStr as string)
  set pathString to current application’s NSString’s stringWithString:aStr
  
return (pathString’s pathExtension()) as string
end getPathExtension

★Click Here to Open This Script 

(Visited 66 times, 1 visits today)
Posted in file list recursive call | Tagged 10.11savvy 10.12savvy Mail | Leave a comment

指定のコード体系の全パターンのコードを生成 v5

Posted on 2月 21, 2018 by Takaaki Naganoya

AppleScript名:指定のコード体系の全パターンのコードを生成 v5
–v5 外部からプロパティで与えられたルールから、初期値となる「最小値」を自前で計算するように変更
–v4 桁ごとにサブルーチンを設けるのではなく、再帰処理で1つのルーチンを多重呼び出しするように変更
–v3 コードのルールを外部供給する構成にした(処理ロジックとルールの分離が完了)
–v2 各桁の最大値と最小値をプロパティで持たせるテスト
–v1 各桁のインクリメント用のサブルーチンを作成し、ルールを各サブルーチン側でハードコーディングする(正しく動く)

script spd
  property aList : {}
  
property aRuleList : {{1, 2}, {1, 3}, {0, 1}, {1, 4}, {1, 8}} –各桁の{最小値, 最大値}ペアのリスト
  
property aRuleLen : length of aRuleList
end script

set aList of spd to {} –initilaize

set initNum to getMinNum() of me –本ルール下における最小値

set the end of aList of spd to initNum

copy initNum to aNum

repeat
  set aRes to incDigit(aNum, 1) of me
  
  
if aRes = false then
    exit repeat
  end if
  
  
set the end of aList of spd to aRes
  
  
copy aRes to aNum
  
end repeat

–return length of (aList of spd)
return (aList of spd)

–与えられたルール下における最小値をルールリストから求める
on getMinNum()
  –桁数が合っているだけのダミー数字を、適切な桁数作成する(例:11111)
  
set tmpNumStr to ""
  
repeat (aRuleLen of spd) times
    set tmpNumStr to tmpNumStr & "1"
  end repeat
  
  
set tmpNum to tmpNumStr as integer
  
  
–ルールから各桁の最小値を取り出して、各桁に設定する
  
repeat with i from 1 to (aRuleLen of spd)
    set aDigNum to item 1 of item i of (aRuleList of spd)
    
set tmpNum to setDigit(tmpNum, i, aDigNum) of me
  end repeat
  
  
return tmpNum
  
end getMinNum

–繰り上がり処理(再帰呼び出しで使用)
on incDigit(aNum, aDigit)
  
  
set {thisMin, thisMax} to item ((aRuleLen of spd) – aDigit + 1) of (aRuleList of spd)
  
  
set aTarget to getDigit(aNum, aDigit) of me
  
  
if aTarget = thisMax then
    
    
if aDigit = (aRuleLen of spd) then
      –オーバーフロー(桁あふれ)エラーを返す
      
return false
    end if
    
    
set bNum to incDigit(aNum, aDigit + 1) of me
    
    
if bNum = false then return false
    
    
set bNum to setDigit(bNum, aDigit, thisMin) of me
    
  else
    
    
set aTarget to aTarget + 1
    
set bNum to setDigit(aNum, aDigit, aTarget) of me
    
  end if
  
  
return bNum
  
end incDigit

–指定数値のうち指定桁の数字を返す
on getDigit(aNum, aDigit)
  
  
set aStr to aNum as string
  
set aLen to length of aStr
  
if aLen < aDigit then
    return false –エラー
  end if
  
  
set tStr to character (aLen – aDigit + 1) of aStr
  
return tStr as integer
  
end getDigit

–指定数値のうち指定桁の数字を返す
on setDigit(aNum, aDigit, newNum)
  
  
set aStr to aNum as string
  
set aLen to length of aStr
  
if aLen < aDigit then
    return false –エラー
  end if
  
  
set aList to characters of aStr
  
  
set item (aLen – aDigit + 1) of aList to (newNum as string)
  
set aaStr to aList as string
  
  
return aaStr as integer
  
end setDigit

★Click Here to Open This Script 

(Visited 39 times, 1 visits today)
Posted in recursive call Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

指定EnumがどのFrameworkに所属しているか検索 v2

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:指定EnumがどのFrameworkに所属しているか検索 v2
— Created 2017-10-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSFileManager : a reference to current application’s NSFileManager
property NSString : a reference to current application’s NSString
property NSPredicate : a reference to current application’s NSPredicate
property NSMutableArray : a reference to current application’s NSMutableArray
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set a1Res to searchEnumFromHeaderFiles("NSUTF8StringEncoding") of me
–>  {​​​​​"DiscRecording.framework", ​​​​​"Foundation.framework", ​​​​​"SpriteKit.framework"​​​}

set a2Res to searchEnumFromHeaderFiles("NSNumberFormatterRoundUp") of me
–>  {​​​​​"Foundation.framework"​​​}

set a3Res to searchEnumFromHeaderFiles("NSParagraphStyleAttributeName") of me
–>  {​​​​​"AppKit.framework"​​​}

on searchEnumFromHeaderFiles(targString)
  set aClass to current application’s NSClassFromString(targString)
  
if aClass is not equal to missing value then return false
  
  
set dPath to POSIX path of (path to application id "com.apple.dt.Xcode")
  
set aFol to dPath & "Contents/Developer/Platforms/MacOSX.platform/" & "Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
  
  
set bList to retFullPathWithinAFolderWithRecursiveFilterByExt(aFol, "h") of me
  
set matchedList to {}
  
  
repeat with i in bList
    set j to contents of i
    
    
set aStr to (NSString’s stringWithContentsOfFile:j encoding:NSUTF8StringEncoding |error|:(missing value))
    
if aStr ≠ missing value then
      set aRange to (aStr’s rangeOfString:targString)
      
      
if aRange’s location() ≠ current application’s NSNotFound and (aRange’s location()) < 9.99999999E+8 then
        set tmpStr to (current application’s NSString’s stringWithString:j)
        
set pathList to tmpStr’s pathComponents()
        
set thePred to (current application’s NSPredicate’s predicateWithFormat:"pathExtension == ’framework’")
        
set aRes to (pathList’s filteredArrayUsingPredicate:thePred)’s firstObject() as text
        
set the end of matchedList to aRes
      end if
      
    end if
  end repeat
  
  
set aArray to current application’s NSArray’s arrayWithArray:matchedList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
return bArray as list
end searchEnumFromHeaderFiles

–指定フォルダ以下のすべてのファイルを再帰で取得(拡張子で絞り込み)
on retFullPathWithinAFolderWithRecursiveFilterByExt(aFol, aExt)
  set anArray to NSMutableArray’s array()
  
set aPath to NSString’s stringWithString:aFol
  
set dirEnum to NSFileManager’s defaultManager()’s enumeratorAtPath:aPath
  
  
repeat
    set aName to (dirEnum’s nextObject())
    
if aName = missing value then exit repeat
    
set aFullPath to aPath’s stringByAppendingPathComponent:aName
    
anArray’s addObject:aFullPath
  end repeat
  
  
set thePred to NSPredicate’s predicateWithFormat:"pathExtension == [c]%@" argumentArray:{aExt}
  
set bArray to anArray’s filteredArrayUsingPredicate:thePred
  
  
return bArray as list
end retFullPathWithinAFolderWithRecursiveFilterByExt

★Click Here to Open This Script 

(Visited 31 times, 3 visits today)
Posted in recursive call 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定リストの次元を取得する v3

Posted on 2月 7, 2018 by Takaaki Naganoya

任意のリスト(配列)変数の次元を取得するAppleScriptです。

手軽に多次元配列を宣言してアクセスできるAppleScriptですが、作成した配列変数の次元を確認する方法は用意されていません。

そこで、指定リスト(配列)の次元数を取得するものを書いてみました。

日常的に2次元配列とか3次元配列は使いますが、4次元配列まではあまり使ったことがありません。実用上は3次元配列ぐらいでしょうか。

たまに2次元配列が使えない言語とか、実用上は2次元配列ぐらいまでしか直接宣言できない言語があって驚かされます。

AppleScript名:指定リストの次元を取得する v3
use AppleScript version "2.4"
use scripting additions

set aList to {{1, 2, 3}, {4, 5, 6}} –2D List
set aDim to getDimension given tArray:aList
–> 2

set bList to {{{1, 2}, {2, 3}, {3, 4}}, {{1, 2}, {2, 3}, {3, 4}}, {{1, 2}, {2, 3}, {3, 4}}} –3D List
set bDim to getDimension given tArray:bList
–> 3

set cList to {1, 2, 3, 4, 5, 6} –1D List
set cDim to getDimension given tArray:cList
–> 1

set dList to {{{{1, 2}, {2, 3}}, {{1, 2}, {2, 3}}, {{1, 2}, {2, 3}}}} –4D List
set dDim to getDimension given tArray:dList
–> 4

–指定Listの次元を再帰で取得する
on getDimension given tArray:aList as list : {}, curDim:aNum as integer : 1
  
  
set anItem to contents of first item of aList
  
set aClass to class of anItem
  
  
if aClass = list then
    set aNum to aNum + 1
    
set aRes to getDimension given tArray:anItem, curDim:aNum
  else
    return aNum
  end if
  
end getDimension

★Click Here to Open This Script 

(Visited 43 times, 3 visits today)
Posted in list recursive call | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

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

Google Search

Popular posts

  • AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 13, Ventura(継続更新)
  • ドラッグ&ドロップ機能の未来?
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • PFiddlesoft UI Browserが製品終了に
  • macOS 12.3 beta 5、ASの障害が解消される(?)
  • SF Symbolsを名称で指定してPNG画像化
  • 新刊発売:AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 12.3上でFinder上で選択中のファイルをそのままオープンできない件
  • Pixelmator Pro v2.4.1で新機能追加+AppleScriptコマンド追加
  • Safariで表示中のYouTubeムービーのサムネイル画像を取得
  • macOS 12のスクリプトエディタで、Context Menu機能にバグ
  • 人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 12.5(21G72)がリリースされた!
  • Pages v12に謎のバグ。書類上に11枚しか画像を配置できない→解決
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • macOS 13 TTS Voice環境に変更
  • NSCharacterSetの使い方を間違えた

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1391) 10.14savvy (586) 10.15savvy (434) 11.0savvy (274) 12.0savvy (174) 13.0savvy (34) CotEditor (60) Finder (47) iTunes (19) Keynote (97) 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 (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (118) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWorkspace (20) Numbers (55) Pages (36) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • 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
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • 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)
  • 未分類

アーカイブ

  • 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