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

テキストのキーワード検索(結果をNSRangeのlistで返す)

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:テキストのキーワード検索(結果をNSRangeのlistで返す)
— Created 2017-08-09 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/4771

property NSString : a reference to current application’s NSString
property NSMutableArray : a reference to current application’s NSMutableArray
property NSLiteralSearch : a reference to current application’s NSLiteralSearch

set aStr to "ATGC ACGT ATGC AGTC
ATGC ACGT ATGC AGTC
ATGC ACGT ATGC AGTC
ATGC ACGT ATGC AGTC
"

set aRes to searchWordRanges(aStr, "ATGC") of me as list
–>  {​​​​​{​​​​​​​location:0, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:10, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:20, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:30, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:40, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:50, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:60, ​​​​​​​length:4​​​​​}, ​​​​​{​​​​​​​location:70, ​​​​​​​length:4​​​​​}​​​}

on searchWordRanges(aTargText as string, aSearchStr as string)
  set aStr to NSString’s stringWithString:aTargText
  
set bStr to NSString’s stringWithString:aSearchStr
  
  
set hitArray to NSMutableArray’s alloc()’s init()
  
set cNum to (aStr’s |length|()) as integer
  
  
set aRange to current application’s NSMakeRange(0, cNum)
  
  
repeat
    set detectedRange to aStr’s rangeOfString:bStr options:(NSLiteralSearch) range:aRange
    
if (detectedRange’s location) is equal to (current application’s NSNotFound) then exit repeat
    
    
hitArray’s addObject:detectedRange
    
    
set aNum to (detectedRange’s location) as integer
    
set bNum to (detectedRange’s |length|) as integer
    
    
set aRange to current application’s NSMakeRange(aNum + bNum, cNum – (aNum + bNum))
  end repeat
  
  
return hitArray
end searchWordRanges

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

stringFrom_makingIt のサンプル v2

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:stringFrom_makingIt のサンプル v2
— Created 2015-01-04 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

load framework

set res0 to current application’s SMSForder’s |description|() as text
–> "SMSForder”

–Calculates the MD5 hash of a string
set aStr to "0123456789"
set a to bPlus’s MD5From:aStr
–> "781e5e245d69b566979b86e28d23f2c7"

–Converts straight quote marks into typographer’s quote marks
set bStr to "’a’"
set b to bPlus’s smartQuotedFrom:bStr
–> "‘a’"

–Converts typographer’s quote marks into straight quote marks
set cStr to "‘a’"
set c to bPlus’s unsmartQuotedFrom:cStr
–> "’a’"

–Encodes the five reserved XML characters only
set d1Str to "&\"<>’"
set d1 to bPlus’s encodedXMLFrom:d1Str
–> "&amp;&quot;&lt;&gt;&apos;"

–Decode the five reserved XML characters only
set d2Str to "&amp;&quot;&lt;&gt;&apos;"
set d2 to bPlus’s unencodedForXMLFrom:d2Str
–> "&\"<>’"

–Encodes characters outside ASCII 32-126 in hexadecimal form (&#xHHHH;)
set eStr to "あいうえお"
set e to bPlus’s encodedHexFrom:eStr
–> "&#x3042;&#x3044;&#x3046;&#x3048;&#x304A;"

–Encodes characters outside ASCII 32-126 in decimal form (&#DD;), for use in HTML
set fStr to "あいうえお"
set f to bPlus’s encodedDecimalFrom:fStr
–> "&#12354;&#12356;&#12358;&#12360;&#12362;"

–Decodes characters that appear in decimal form (&#DD;) or hexidecimal form (&#xHHHH;), as used in XML and HTML
set gStr to "&#12354;&#12356;&#12358;&#12360;&#12362;"
set g to bPlus’s decodedDecimalFrom:gStr
–> "あいうえお"

–Deletes any paragraphs that are empty or contain only spaces and/or tabs
set hStr to "a
aaa
a

a
a

"
set h to bPlus’s emptyLineFreeFrom:hStr
–>
(*
"a
aaa
a
a
a"
*)

–Converts runs of more than one space to a single space character, and trims spaces from the beginning and end of paragraphs.
set hStr to " aaaaa bbbb ccccc "
set h to bPlus’s cleanSpacedFrom:hStr
–> "aaaaa bbbb ccccc"

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ICUTransformのサンプル v2

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:ICUTransformのサンプル v2
–Sample Code
— Created 2015-01-06 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set theString to "ながのや, たかあき" –Hiragana
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Hiragana-Latin" inverse:false) as text
–> "naganoya, takaaki"

set theString to "ながのや, たかあき" –Hiragana
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Hiragana-Katakana" inverse:false) as text
–> "ナガノヤ, タカアキ"–Katakana

set theString to "Takaaki, Naganoya"
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Hiragana" inverse:false) as text
–> "たかあき、 ながのや"–Hiragana

set theString to "Takaaki, Naganoya"
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Katakana" inverse:false) as text
–> "タカアキ、 ナガノヤ"–Katakana

set theString to "Shane, Stanley"
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Katakana" inverse:false) as text
–> "シャネ、 スタンレイ"–Katakana…..this seems odd. "シェーン, スタンリー" will be a right spelling

set theString to "Takaaki, Naganoya"
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Halfwidth-Fullwidth" inverse:false) as text
–> "Takaaki, Naganoya"–Double Width Alphabet

set theString to "Naganoya, Takaaki"
set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Hangul" inverse:false) as text
–> "나가노야, 타카아키"–Hangul

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

文字列の長さを求める

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:文字列の長さを求める
— Created 2015-09-02 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to current application’s NSMutableString’s stringWithString:"あいうえお"
set b to a’s |length|()
–>  5

set c to "あいうえお"
set d to length of c
–>  5

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

considering numeric strings

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:considering numeric strings
set aVer to "10.10.1"
set bVer to "10.9.5"

–OS X 10.4で導入されたconsidering numeric strings
considering numeric strings
  if aVer > bVer then
    display dialog "Yosemite is newer than Mavericks"
  else
    display dialog "Whoa!"
  end if
end considering

if aVer > bVer then
  display dialog "Yosemite is newer than Mavericks"
else
  display dialog "Whoa!"
end if

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

NSStringによりバージョン文字列比較

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:NSStringによりバージョン文字列比較
— Created 2015-07-27 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aVer to "10.10.0"
set bVer to "10.10"

set aVerStr to current application’s NSString’s stringWithString:aVer
set bVerStr to current application’s NSString’s stringWithString:bVer

set aRes to aVerStr’s compare:bVer options:(current application’s NSNumericSearch)
–>  1

–1:aVer > bVer, 0:aVer = bVer, -1:bVer > aVer

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

バージョン番号文字列の正確な比較

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:バージョン番号文字列の正確な比較
— Created 2015-07-27 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to "10.10"
set b to "10.10.0"
set aRes to compareVersionNumericStrings(a, b) of me
–>  "A=B"

set a to "10.10"
set b to "10.10.0.1"
set aRes to compareVersionNumericStrings(a, b) of me
–>  "B>A"

set a to "10.10.1"
set b to "10.10.0.1"
set aRes to compareVersionNumericStrings(a, b) of me
–>  "A>B"

set a to "10.10.1"
set b to "10.9.1.1"
set aRes to compareVersionNumericStrings(a, b) of me
–> "A>B"

–バージョン番号文字列の正確な比較
on compareVersionNumericStrings(a, b)
  
  
set aList to parseVersionNumber(a) of me
  
–>  {​​​​​"10", ​​​​​"10"​​​}
  
  
set bList to parseVersionNumber(b) of me
  
–>  {​​​​​"10", ​​​​​"10", ​​​​​"0"​​​}
  
  
set aLen to length of aList
  
set bLen to length of bList
  
  
if aLen = bLen then
    –何もしない
    
set buryTimes to 0
  else if aLen > bLen then
    set buryTimes to (aLen – bLen)
    
set bList to addNullItems(bList, "0", buryTimes) of me
  else if bLen > aLen then
    set buryTimes to (bLen – aLen)
    
set aList to addNullItems(aList, "0", buryTimes) of me
  end if
  
  
set cLen to length of aList –aListもbListも同じ長さなのでこれでいい
  
  
repeat with i from 1 to cLen
    
    
set aItem to contents of item i of aList
    
set bItem to contents of item i of bList
    
    
considering numeric strings
      if aItem > bItem then
        return "A>B"
      else if aItem < bItem then
        return "B>A"
      end if
    end considering
    
  end repeat
  
  
return "A=B"
  
end compareVersionNumericStrings

–バージョン番号文字列からメジャーバージョンを取り出し数値として返す
on parseVersionNumber(a)
  set aStr to current application’s NSString’s stringWithString:a
  
set aRes to (aStr’s componentsSeparatedByString:".")
  
set bRes to aRes’s allObjects()
  
return bRes as list
end parseVersionNumber

–指定リストの末尾に指定個数、埋め草を追加する
on addNullItems(aList, anItem, aTimes)
  
  
copy aList to bList
  
  
repeat aTimes times
    set the end of bList to anItem
  end repeat
  
  
return bList
  
end addNullItems

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

NFKC Casefoldの影響を除外した文字列比較

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:NFKC Casefoldの影響を除外した文字列比較
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set a to "㍑"
set b to "リットル"
log (a = b) –AppleScriptネイティブの文字列比較だとこれらが混同されることに注意
–> true
set c to compStrA_B_(a, b)
–> false

set a to "バビブベボ"
set b to "ハヒフヘホ"
log (a = b)
–> false
set c to compStrA_B_(a, b)
–> false

set a to "あいうえお"
set b to "アイウエオ"
log (a = b) –AppleScriptネイティブの文字列比較だとこれらが混同されることに注意
–> true
set c to compStrA_B_(a, b)
–> false

–文字列比較をASOC(Cocoa)とAppleScriptで実施するテストルーチン
on compStrA:a b:b
  set aStr to current application’s NSString’s stringWithString:a
  
set bStr to current application’s NSString’s stringWithString:b
  
return (aStr’s isEqualToString:bStr) as boolean
end compStrA:b:

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Umlautを無視した文字列比較(NSDiacriticInsensitiveSearch)

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:Umlautを無視した文字列比較(NSDiacriticInsensitiveSearch)
— Created 2015-03-28 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aRes to compareIgnoringDiacriticals("AbC", "ÂbC")
–> true

set aRes to compareIgnoringDiacriticals("Abc", "Ábc")
–> true

set aRes to compareIgnoringDiacriticals("abc", "åbc")
–> true

–Strings Compare with ignoring Umlaut
on compareIgnoringDiacriticals(aText as text, bText as text)
  set aStr to current application’s NSString’s stringWithString:aText
  
return (aStr’s compare:bText options:((current application’s NSCaseInsensitiveSearch) + (current application’s NSDiacriticInsensitiveSearch as integer))) = current application’s NSOrderedSame
end compareIgnoringDiacriticals

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

テキストからリガチャーを削除する v2

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:テキストからリガチャーを削除する v2
— Created 2017-01-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set inputStringWithLigatures to "ꜲꜳÆæꜴꜵꜶꜷꜸꜹꜺꜻꜼꜽffffifflfiflŒœꝎstꜨꜩᵫꝠꝡ" & "LJLjNJNjnjDZDzdzIJij"
set aRes to removeLigaturesFromString(inputStringWithLigatures) of me
–>  "AAaaAEaeAOaoAUauAVavAVavAYayffffifflfiflOEoeOOstTZtzueVYvyLJLjNJNjnjDZDzdzIJij"

on removeLigaturesFromString(inputStringWithLigatures)
  set theString to current application’s NSString’s stringWithString:inputStringWithLigatures
  
  
# Convert what may be done applying transform "ÆæffffifflfiflŒœᵫ" & "LJLjNJNjnjDZDzdzIJij"
  
set inputStringWithLigatures to (theString’s stringByApplyingTransform:"Latin-ASCII" |reverse|:false) as text
  
  
# Treat the remaining ligatures
  
set searchStrings to {"Ꜳ", "ꜳ", "Ꜵ", "ꜵ", "Ꜷ", "ꜷ", "Ꜹ", "ꜹ", "Ꜻ", "ꜻ", "Ꜽ", "ꜽ", "Ꝏ", "Ꜩ", "ꜩ", "Ꝡ", "ꝡ"} — if you find others, add them here
  
set replaceStrings to {"AA", "aa", "AO", "ao", "AU", "au", "AV", "av", "AV", "av", "AY", "ay", "OO", "TZ", "tz", "VY", "vy"} — if you find others, add them here
  
set saveTID to AppleScript’s text item delimiters
  
  
considering case
    set i to 0
    
repeat with lig in searchStrings
      set i to i + 1
      
set AppleScript’s text item delimiters to {lig}
      
set inputStringWithLigatures to text items of inputStringWithLigatures
      
set AppleScript’s text item delimiters to {item i of replaceStrings}
      
set inputStringWithLigatures to inputStringWithLigatures as text
    end repeat
  end considering
  
  
set AppleScript’s text item delimiters to saveTID
  
  
return inputStringWithLigatures
end removeLigaturesFromString

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定キーワードの出現回数のカウント

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定キーワードの出現回数のカウント
set origText to "このテキストはいずれ何らかの指定キーワードが何回出現するかという見地から評価される運命にある。評価されるということは、何がしかの文法的評価体系によって分析されるということである(すんごい、いいかげんな日本語)。"
set aKeyText to "何"
set freqNum to retFrequency(origText, aKeyText) of me

–指定文字列内の指定キーワードの出現回数を取得する
on retFrequency(origText, aKeyText)
  set aRes to parseByDelim(origText, aKeyText) of me
  
return ((count every item of aRes) – 1)
end retFrequency

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

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCで文字を逆順に v2(CJK文字などマルチバイト対応)

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:ASOCで文字を逆順に v2(CJK文字などマルチバイト対応)
— Created 2015-09-01 by Takaaki Naganoya
— Modified 2015-09-01 by Shane Stanley –Consider CJK Characters & Emoji
–http://stackoverflow.com/questions/6720191/reverse-nsstring-text
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aUUID to (current application’s NSUUID’s UUID()’s UUIDString()) as text
–>  "46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A"

set aUUID to "😀😐" & aUUID & "😀😐"
–>  "😀😐46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A😀😐"

set revUUID to reversedStr(aUUID) as text
–>  "😐😀A08A03B3D310-A8AB-9DD4-E3BC-7B71FE64😐😀"

on reversedStr(paramStr as text)
  set aStr to current application’s NSString’s stringWithString:paramStr
  
set strLength to aStr’s |length|()
  
set revStr to current application’s NSMutableString’s stringWithCapacity:strLength
  
set charIndex to strLength – 1
  
repeat while charIndex > -1
    set subStrRange to aStr’s rangeOfComposedCharacterSequenceAtIndex:charIndex
    
revStr’s appendString:(aStr’s substringWithRange:subStrRange)
    
set charIndex to (location of subStrRange) – 1
  end repeat
  
  
return revStr
end reversedStr

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

バージョン番号文字列からメジャーバージョンを取り出し数値として返す v4

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:バージョン番号文字列からメジャーバージョンを取り出し数値として返す v4
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set a to "10.0.1"
set b to retMajorVersionNumber(a) of me
–> 10

set a to "9.10"
set b to retMajorVersionNumber(a) of me
–> 9

–バージョン番号文字列からメジャーバージョンを取り出し数値として返す
on retMajorVersionNumber(a)
  set aStr to current application’s NSString’s stringWithString:a
  
–> "10.0.1" (NSString)
  
  
set aRes to (aStr’s componentsSeparatedByString:".")
  
–> {"10","0","1"} (NSArray)
  
  
set bRes to aRes’s firstObject()
  
–> "10" (NSString)
  
  
set cRes to bRes’s integerValue()
  
–> 10
  
  
return cRes as integer
end retMajorVersionNumber

★Click Here to Open This Script 

Posted in Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダ以下のすべてのファイルのうち指定UTIに該当するものを取得(spotlightで処理)

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定フォルダ以下のすべてのファイルを再帰で取得(spotlightで処理).scpt
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use mdLib : script "Metadata Lib" version "1.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

–set theFolder to choose folder
set theFolder to (path to desktop folder)
set theFiles to mdLib’s searchFolders:{theFolder} searchString:"kMDItemContentType IN[c] %@" searchArgs:{"com.apple.applescript.script", "com.apple.applescript.script-bundle"}

★Click Here to Open This Script 

Posted in file Spotlight System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダ以下のすべてのファイルを再帰で取得 v2

Posted on 2月 15, 2018 by Takaaki Naganoya

指定フォルダ以下のすべてのファイルを再帰で取得するAppleScriptです。

「指定フォルダ以下のファイルを取得する」やりかたは、Spotlightを使うのがベストです。ベストではあるものの、各ユーザーのHDDなりSSDは「破損している」ケースもあって、Spotligtの検索が効かない場合もあります。

また、マウントしたLAN上のファイルサーバーに対してSpotlightが効くかどうかは実際にやってみないと分かりません。OSが異なる可能性がありますし、同じmacOSであっても効くかどうかは実際に試してみるまではっきりしません(開発時に真っ先に確認するポイントでもあります。ファイルサーバーを相手に処理するつもりかどうかの確認です)。

そこで、地道に再帰処理でファイルを取得すること「も」試してみることになるわけですが、ここでFinderを使うと遅くなります。

OS X 10.6以降の64ビット化されたいわゆる「Cocoa Finder」は処理速度が低下しているため、大量のファイル処理をFinder経由で行うことは「悪手」といえます。数百個ぐらいでけっこうな処理速度の低下がみられます。1,000を超えると露骨に速度が下がります。フィルタ参照で条件をつけて抽出するのも速度低下につながります。さらに複数のフィルタ条件を指定するとアホみたいに遅くなります。Cocoa Finderの性能低下はハンパではありません。

たまに、Classic Mac OSの環境しか知らない人が久しぶりに現代の(64ビット化された)macOS上で昔と同じようにFinderに対して膨大なファイルの処理を行わせて「遅い」と文句を言っているのを見かけますが、Finder経由でのファイル処理が現実的ではないという状況を知らなければそういう感想になってしまうかもしれません。

# Classic MacOS〜macOS 10.6ぐらいまではFinder経由で行っても、極端に時間がかかることはありませんでした。それ以降の環境のお話です。また、macOS 10.7以降でも、数個〜数十程度のすくないファイルを処理する場合には、あまり問題になりません

そこで、NSFileManagerを用いてファイル処理を行うことで、「Spotlightほど速くはないものの、Finderで処理するよりははるかに高速」な処理を行えます。

AppleScript名:指定フォルダ以下のすべてのファイルを再帰で取得 v2
— Created 2017-08-04 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

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

–set aFol to POSIX path of (choose folder)
set aFol to POSIX path of (path to desktop folder)

–set aList to retFullPathWithinAFolderWithRecursive(aFol) of me
set bList to retFullPathWithinAFolderWithRecursiveFilterByExt(aFol, "scpt") of me
–set cList to retFilenameWithinAFolderWithRecursiveFilterByExt(aFol, {"scpt", "scptd"}) of me
–set dList to retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString(aFol, "png", "スクリーン") of me
–set eList to retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString(aFol, {"scpt", "scptd"}, "並列") of me

–指定フォルダ以下のすべてのファイルを再帰で取得
on retFilenamesWithinAFolderWithRecursive(aFol)
  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’s lastPathComponent()’s stringByDeletingPathExtension() as string)
  end repeat
  
  
return anArray as list
end retFilenamesWithinAFolderWithRecursive

–指定フォルダ以下のすべてのファイルを再帰で取得
on retFullPathWithinAFolderWithRecursive(aFol)
  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’s lastPathComponent()’s stringByDeletingPathExtension() as string)
  end repeat
  
  
return anArray as list
end retFullPathWithinAFolderWithRecursive

–指定フォルダ以下のすべてのファイルを再帰で取得(拡張子で絞り込み)
on retFilenameWithinAFolderWithRecursiveFilterByExt(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 retFilenameWithinAFolderWithRecursiveFilterByExt

–指定フォルダ以下のすべてのファイルを再帰で取得(拡張子で絞り込み)
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

–指定フォルダ以下のすべてのファイルを再帰で取得(拡張子リストで絞り込み)
on retFullPathWithinAFolderWithRecursiveFilterByExtList(aFol, aExtList)
  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 IN [c]%@" argumentArray:{aExtList}
  
set bArray to anArray’s filteredArrayUsingPredicate:thePred
  
  
return bArray as list
end retFullPathWithinAFolderWithRecursiveFilterByExtList

–指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子で絞り込み)
on retFullPathWithinAFolderWithRecursiveFilterByExtListAndFileNameString(aFol, aExt, aNameString)
  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]%@ && lastPathComponent CONTAINS %@" argumentArray:{aExt, aNameString}
  
set bArray to anArray’s filteredArrayUsingPredicate:thePred
  
  
return bArray as list
end retFullPathWithinAFolderWithRecursiveFilterByExtListAndFileNameString

–指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子リストで絞り込み)
on retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString(aFol, aExtList, aNameString)
  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 IN [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{aExtList, aNameString}
  
set bArray to anArray’s filteredArrayUsingPredicate:thePred
  
  
return bArray as list
end retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString

★Click Here to Open This Script 

Posted in file Spotlight System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定ボリウムをマウント

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定ボリウムをマウント
— Created 2015-12-01 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aPath to current application’s |NSURL|’s URLWithString:"afp://MBA13.local/maro"
set ws to current application’s NSWorkspace’s sharedWorkspace()
ws’s openURL:aPath

★Click Here to Open This Script 

Posted in drive System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

CocoaでDiskSpace(%)を求める

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:CocoaでDiskSpace(%)を求める
— Created 2015-04-01 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aPath to current application’s NSString’s stringWithString:"/"
set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:aPath |error|:(missing value)
set fRes to (fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)) as string

set aDecNum to current application’s NSDecimalNumber’s decimalNumberWithString:fRes
set aFreeNum to aDecNum’s decimalNumberByDividingBy:(current application’s NSDecimalNumber’s decimalNumberWithString:"1000000000") –"G" Bytes for Storage
set bFreeNum to aFreeNum as real
–> 84.058387756348

★Click Here to Open This Script 

Posted in drive System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

CocoaでDiskSpace(Bytes)を求める

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:CocoaでDiskSpace(Bytes)を求める
— Created 2015-04-02 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

usedSpace("/")
–>  8.4346003456E+10

usedSpace2("/")
–>  8.4345450496E+10

usedSpaceString("/")
–>  "84.35 GB"

usedSpaceLongString("/")
–>  "45.77 GB (45,772,857,344 bytes)"–English user environment
–>  "45.77 GB (45,772,857,344 バイト)"–Japanese user environment

tell application "Finder"
  free space of startup disk
end tell
–>  4.5784592712E+10

on usedSpace(volumePath)
  set theNSURL to current application’s class "NSURL"’s fileURLWithPath:volumePath –considering "ASOC in Xcode"
  
set {theResult, theSize} to theNSURL’s getResourceValue:(reference) forKey:(current application’s NSURLVolumeAvailableCapacityKey) |error|:(missing value)
  
  
return theSize as real — integer may be too big for AS
end usedSpace

on usedSpace2(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
  
return (fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)) as real — integer may be too big for AS
end usedSpace2

on usedSpaceString(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
set fRes to fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)
  
  
–Formatting
  
set sizeString to current application’s NSByteCountFormatter’s stringFromByteCount:fRes countStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
  
  
return sizeString as text
end usedSpaceString

on usedSpaceLongString(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
set fRes to fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)
  
  
–Formatting
  
set theFormatter to current application’s NSByteCountFormatter’s alloc()’s init()
  
theFormatter’s setCountStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
  
theFormatter’s setIncludesActualByteCount:true
  
set sizeString to theFormatter’s stringFromByteCount:fRes
  
  
return sizeString as text
end usedSpaceLongString

★Click Here to Open This Script 

Posted in drive System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダが所属しているDiskの空き容量をGバイト単位で返す

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定フォルダが所属しているDiskの空き容量をGバイト単位で返す
— Created 2015-04-02 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aFol to choose folder
set aFolPOSIX to POSIX path of aFol

set fRes to freeStorageSpaceG(aFolPOSIX)

–指定フォルダが所属しているDiskの空き容量をGバイト単位で返す
on freeStorageSpaceG(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
set fileFree to (fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize))
  
  
set sizeString to current application’s NSByteCountFormatter’s stringFromByteCount:fileFree countStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
  
  
return sizeString as text
end freeStorageSpaceG

★Click Here to Open This Script 

Posted in drive System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定ボリウム(サーバーボリウム)の空き容量を調べる

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定ボリウム(サーバーボリウム)の空き容量を調べる
— Created 2015-05-14 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set serverName to "maro"
set aRes to getVolumeFreeSpace(serverName) of me

on getVolumeFreeSpace(serverName)
  set anNSURL to (current application’s |NSURL|’s fileURLWithPath:"/Volumes")’s URLByAppendingPathComponent:serverName
  
set theResult to (anNSURL’s checkResourceIsReachableAndReturnError:(missing value)) as boolean
  
if not theResult then
    — not mounted, so handle error
    
return false
  end if
  
set {theResult, theSpare} to anNSURL’s getResourceValue:(reference) forKey:(current application’s NSURLVolumeAvailableCapacityKey) |error|:(missing value)
  
if theResult as boolean then
    set spareString to (current application’s NSByteCountFormatter’s stringFromByteCount:theSpare countStyle:(current application’s NSByteCountFormatterCountStyleFile)) as text
  else
    — couldn’t get the value, so handle error
    
error
  end if
end getVolumeFreeSpace

★Click Here to Open This Script 

Posted in drive System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • Apple Creator Studioに含まれるKeynote/Pages/Numbersは新バージョン?
  • 最初期に書かれた書籍「AppleScript道入門」の序文をいま振り返る
  • 簡易カラーチャート画像の出力 v2.1
  • 指定フォルダ内から指定拡張子のファイルを取得し、エイリアスだったらオリジナルのパスをPOSIX pathで取得
  • macOS 15.7.2 スクリプトメニューから実行できなくなった地図系ライブラリ?
  • ASCII ART QR Encoder
  • MacBook Air/MacBook Proのディスプレイ開閉角度を取得
  • 「AppleScriptセキュリティ実践ガイド」を発売
  • 各種GUIアプリ書類のオープン速度を向上するためにUIアニメーションの一時停止を
  • Keynote->Keynote Creator Studio(以下同様)
  • Finder上で選択中のHEIC画像から位置情報を削除_v5
  • 翻訳テスト
  • FSFindFolder failed with error=-43
  • YouTubeムービー再生実験
  • 2025年に書いた価値あるAppleScript
  • 関数計算ライブラリで一部のユーザーから再現しない問題が報告される件
  • ワークスペース切り替えv2.1
  • YouTubeムービー再生実験 v2c
  • macOS 26.xのSystem Events/Screen Saver preferencesにバグ
  • macOS 26.1が登場。Finderのバグが直らない?

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (440) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (173) 26.0savvy (53) CotEditor (68) Finder (53) Keynote (123) 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 (80) Pages (58) Safari (45) Script Editor (28) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • Apple Inteligence
  • 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
  • date
  • 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
  • Newt On Project
  • NFC
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spaces
  • Spellchecker
  • Spotlight
  • SVG
  • Swift
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2026年10月
  • 2026年9月
  • 2026年8月
  • 2026年7月
  • 2026年6月
  • 2026年5月
  • 2026年3月
  • 2026年2月
  • 2026年1月
  • 2025年12月
  • 2025年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 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