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

月: 2018年2月

TouchBarの検出

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:TouchBarの検出
set aRes to detectTouchBar() of me
–> true/false

on detectTouchBar()
  tell application "System Events"
    set frontApp to first application process whose frontmost is true
    
try
      set touchBar to first UI element of frontApp whose role is "AXFunctionRowTopLevelElement"
    on error errMsg number errNum
      return false
    end try
    
    
set touchBarItems to value of attribute "AXChildren" of touchBar
    
return (touchBarItems is not equal to {})
  end tell
end detectTouchBar

★Click Here to Open This Script 

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

USBに接続されたUSBメモリの情報を取得する v2

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:USBに接続されたUSBメモリの情報を取得する v2

set tmpPath to POSIX path of (path to temporary items from system domain)
set aFileName to (do shell script "/usr/bin/uuidgen")
set outPath to tmpPath & aFileName & ".plist"
do shell script "/usr/sbin/system_profiler -xml SPUSBDataType > " & outPath

tell application "System Events"
  set vRec to value of property list file (outPath as string)
  
set v1Rec to _items of (first item of vRec)
  
  
set dList to {}
  
set sList to {}
  
repeat with i in v1Rec
    set hitF to false
    
try
      set j to _items of i
      
set hitF to true
    end try
    
    
if hitF = true then
      repeat with jj in j
        try
          set jjj to volumes of jj
          
set sNum to d_serial_num of jj
          
set vStr to b_vendor_id of jj
          
          
repeat with ii in jjj
            set the end of dList to {serialNum:sNum, venderName:vStr, dData:contents of ii}
          end repeat
          
        end try
      end repeat
    end if
  end repeat
end tell

dList

–> {{serialNum:"7f12db856195ef", venderName:"0x056e (Elecom Co., Ltd.)", dData:{mount_point:"/Volumes/NO NAME", _name:"NO NAME", writable:"yes", bsd_name:"disk2s1", free_space:"3.75 GB", file_system:"MS-DOS FAT32", |size|:"3.77 GB"}}, {serialNum:"M004101800001", venderName:"0x4146", dData:{mount_point:"/Volumes/ぴよまる", _name:"ぴよまる", writable:"yes", bsd_name:"disk1s9", free_space:"56 MB", file_system:"HFS+", |size|:"123.1 MB"}}}

★Click Here to Open This Script 

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

使用中のMacの製品呼称を取得する v3

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:使用中のMacの製品呼称を取得する v3
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set mRes to retModelInfo() of me
–>  "MacBook Pro with Retina display, Intel Core i7, 15\" (Mid 2012)"

on retModelInfo()
  set pListPath to "/System/Library/PrivateFrameworks/ServerInformation.framework/" & "Versions/A/Resources/English.lproj/SIMachineAttributes.plist"
  
set aRec to retDictFromPlist(pListPath) of me
  
  
set hwName to (do shell script "sysctl -n hw.model")
  
–>  "MacBookPro10,1"
  
  
set aMachineRec to retRecordByLabel(aRec, hwName)
  
  
set aMachineRec2 to contents of first item of aMachineRec
  
return (marketingModel of _LOCALIZABLE_ of aMachineRec2)
end retModelInfo

–Read plist as record
on retDictFromPlist(aPath)
  set thePath to current application’s NSString’s stringWithString:aPath
  
set thePath to thePath’s stringByExpandingTildeInPath()
  
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
  
return theDict as record
end retDictFromPlist

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel(aRecList as list, aPredicate as string)
  –ListからNSArrayへの型変換
  
set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
  
–抽出
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
  
–NSArrayからListに型変換して返す
  
return filteredArray as list
end filterRecListByLabel

–指定レコードの指定ラベルの値を取得する
on retRecordByLabel(aRec as record, aKey as string)
  set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec
  
set aVal to aDic’s valueForKey:aKey
  
return aVal as list
end retRecordByLabel

–指定レコードの指定ラベルの値を取得する
on retRecordByKeyPath(aRec as record, aKey as string)
  set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec
  
set aVal to aDic’s valueForKeyPath:aKey
  
return aVal
end retRecordByKeyPath

★Click Here to Open This Script 

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

CPUのBrand Nameとバス速度を表示

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:CPUのBrand Nameとバス速度を表示
set aTargLabel to "machdep.cpu.brand_string:"
set aRes to retFilteredSysctrlValue(aTargLabel) of me
–> "Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz"

set bRes to retFilteredSysctrlValue("hw.busfrequency_max") of me
–> " 100000000"

return {aRes, bRes}
–> {"Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz", " 100000000"}

on retFilteredSysctrlValue(aTargLabel)
  set aRes to do shell script "sysctl -a | grep " & aTargLabel
  
set aLen to length of aTargLabel
  
set bRes to text (aLen + 2) thru -1 of aRes
  
return bRes
end retFilteredSysctrlValue

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

使用CPUの状況を取得

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:使用CPUの状況を取得
— Created 2017-12-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aDict to retCpuUsage() of me
–> {userCPU:3.2, sysCPU:12.29, idleCPU:84.49}

on retCpuUsage()
  set cpuUseRes to do shell script "top -l 1 | head -10 | grep ’CPU usage’"
  
set aRes to (parseStrFromTo(cpuUseRes, " ", "%") of me)
  
set userUsage to (last item of (words of item 1 of aRes)) as real
  
set sysUsage to (item 2 of aRes) as real
  
set idleUsage to (item -2 of aRes) as real
  
return {userCPU:userUsage, sysCPU:sysUsage, idleCPU:idleUsage}
end retCpuUsage

on parseStrFromTo(aParamStr, fromStr, toStr)
  set theScanner to current application’s NSScanner’s scannerWithString:aParamStr
  
set anArray to current application’s NSMutableArray’s array()
  
  
repeat until (theScanner’s isAtEnd as boolean)
    — terminate check, return the result (aDict) to caller
    
set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference)
    
    
— skip over separator
    
theScanner’s scanString:fromStr intoString:(missing value)
    
set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference)
    
if theValue is missing value then set theValue to "" –>追加
    
    
— skip over separator
    
theScanner’s scanString:toStr intoString:(missing value)
    
    
anArray’s addObject:theValue
  end repeat
  
  
return anArray as list
end parseStrFromTo

★Click Here to Open This Script 

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

PFSystemKitでMac本体の情報を取得 v1.0

Posted on 2月 20, 2018 by Takaaki Naganoya

–> PFSystemKit.framework

AppleScript名:PFSystemKitでMac本体の情報を取得 v1.0
【Comment】  –https://github.com/perfaram/PFSystemKit
— Created 2016-07-26 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "PFSystemKit"

set systemKit to current application’s PFSystemKit’s investigate()

set a1 to systemKit’s platformReport()’s serial() as string –Hardware Serial
–> "C0XXXXXXXXX2"

set a2 to systemKit’s platformReport()’s model() as string –Hardware Model
–> "MacBookPro10,1�"

set a3 to systemKit’s platformReport()’s family() as integer –Hardware Family
–> 5

set a4 to systemKit’s platformReport()’s uuid() as string –Hardware UUID
–> "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

set a5 to systemKit’s platformReport()’s memorySize() as integer –RAM Size
–> 8

set a6 to systemKit’s platformReport()’s boardID() as string
–> "Mac-XXXXXXXXXXXXXXXX"

set a7 to systemKit’s platformReport()’s romVersion() as string
–> "MBP101.XXX.XXXX.XXX.XXXXXXXXXX"

set a8 to systemKit’s platformReport()’s romReleaseDate() as date
–> date 4017年8月8日火曜日 0:00:00

set a9 to systemKit’s platformReport()’s smcVersion() as string –SMC Version
–> "2.3f36"

set a10 to systemKit’s platformReport()’s sleepCause() as string
–> "5"

set a11 to systemKit’s platformReport()’s shutdownCause() as string
–> "5"

set a12 to systemKit’s platformReport()’s platform() as string
–> "1"

set a13 to systemKit’s platformReport()’s endianness() as string
–> "0"

★Click Here to Open This Script 

Posted in System | Leave a comment

【基礎】アプリケーションの操作は、用語辞書に書いてあるとおり記述しないと動かない

Posted on 2月 19, 2018 by Takaaki Naganoya

「コンピュータは、あなたが思ったとおりには動かないが、操作したとおりに動く」

名言だと思います。同様に、

「プログラムは、あなたが思った/願ったようには動かないが、書いたとおりに動く」

と言い換えることが可能です。さらに、

「AppleScriptは、あなたが願ったようには動かないが、書いたとおりに動く」

とも言い換えられます。とくに、アプリケーションの操作については、各アプリケーションが内蔵している「AppleScript用語辞書」に書いてあるとおりに書くのが鉄則です。それ以外の書き方をして「動いてしまった」としても、その方が不思議なわけで。

自分でも、海外のScripter連中でもそうだと思うのですが、Scriptを書いている最中は、AppleScript用語辞書を数枚ひらきっぱなしです。AppleScriptObjCのプログラムを書いているときには、AppleのReferenceサイトも表示させっぱなしです。さらに難問になってくるとUS AppleのAppleScript Users MLとか、www.macscripter.netとかを検索しまくることになり複数モニタが欠かせません(モニタ3枚使っているといったらShane Stanleyに「ずいぶん枚数多いな!」と驚かれましたが、、、)。

ひと昔前(Classic Mac OSの時代)、AppleScript用語辞書はわざわざ人間(開発者)が書くもので、さらに実際のアプリケーション側の機能とリンクしていない「ただの書き方見本」だったので、「用語辞書には書いていないけれど使える」とかいう「隠し命令」なんてものもありました(初代のEntourageとか)。単なる書きもれ、ケアレスミスでしたが、マニアさんの間では「隠し命令」の存在がちょっと「通」な話題になっていたりしました。

いまのAppleScript用語辞書はXMLファイル(sdefファイル)で、この用語辞書がイコールAppleEventの解釈用の辞書であって、「書き方見本」ではありません。そのため「隠し命令」が存在する余地というのはありません。逆にいえば、用語辞書のとおりに動かなかったら完全なバグなわけです(実装が「不完全」「残念」なために期待したとおりに動かないというKeynote/Numbers/Pagesは例外として)。

たまたま、魔が差してTwitter上で議論になったのですが・・・アプリケーションにファイルをオープンさせる場合には、AppleScript用語辞書をScript Editorでオープンして、コマンドなりオブジェクトなりの使い方を調べることになります。このあたり、Objective-CでCocoaのAPIの使い方をAppleのサイトで調べながら書くのと同じです。AppleScript用語辞書は、アプリケーションバンドル内にあってScript Editorからオープンできます。

で、この「AppleScript用語辞書を見る」ことをしない方がけっこう多いようで・・・逆にこれを見ないでよくプログラムが書けるもんだと感心してしまうんですが、用語辞書を見ないとハマりやすいんですね。というか、自分には用語辞書を読まずにScriptを書くことは不可能です。

# 「仕事でAppleScriptを書いている」と言っていいのは、AppleScript用語辞書をきちんと読んで判断する能力がある(サンプルがなくても自分で試行錯誤して書ける)ことが前提です。用語辞書が読めないScripterはプロではありません

前述のように、アプリケーションの操作は「決められたとおりに書かないと正しく動かない」ものであり、さらにその先に「ファイルをオープンもしないで中を調べたりはできないよ」とか「修正したドキュメントを保存/破棄を指定しないでクローズできないよ」といったアプリケーションの挙動(経験則に基づく)の話になるわけなんですけれども、まずは用語辞書を見ないと分かりません。

アプリケーションで書類をオープンする際には、ごく一部の残念な例外(Adobeのアプリ)をのぞいては、パス情報をaliasにしてopenコマンドに渡す必要があります。

ここで、POSIX pathやらfileやらを渡してもオープンはしないわけです。

AppleScript用語辞書には「openコマンドにはaliasを渡してね」と書いてあるので、alias以外を渡すのはアウトです(aliasのlistはOKな場合も)。それ以外の形式のパス情報を渡して、たまたま間違って動いていたとしても、たまたまです。それ以上でも、それ以下でもありません。

最近は、AppleScript用語辞書にHTMLコンテンツを入れることができるようになり、一部のアプリケーションでは用語辞書内にサンプルScriptを掲載しだして、「サンプルをそのままコピペで動く」いい時代になってきたはずなんですが、これまた残念なことに「Apple社内の連中が書くScriptが絶望的に読みにくい」ために(theとかresultとか使いまくる&1行を長く記述して初心者にわかりにくい)、サンプルを読むと逆に理解しづらくなるという事態が(ーー;;

もういっそのこと、アプリケーションバンドル内に、典型的な利用法を記述したAppleScript Librariesを内蔵してしまって、Scriptから呼び出せるようにすべきではないかとも考える次第です。

余談:

途中から(OS X 10.8あたり?)挙動が変わってしまって困っていた、Mail.appのmove命令。前は複数のmessageをlistに入れて一気にmoveできていたのが、1つのmessageしかmoveできないように変わり、処理速度を稼げなくなっていました(複数一度にmoveできたほうが速い)。

いましらべたら、

複数のmessageを示すobject(s)の表記がありますね。でも、「Move an object to new location」とも書いてあり・・・微妙な。

Posted in sdef | Leave a comment

指定ファイルのFinder Tagを取得

Posted on 2月 19, 2018 by Takaaki Naganoya

AppleScript名:指定ファイルのFinder Tagを取得
— Created 2014-12-21 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to choose file
set aResList to getFinderTagList(a) of me
–> {"ブルー", "レッド"}

on getFinderTagList(anAlias)
  –aliasをNSURLに変換  
  
set aPOSIX to POSIX path of anAlias
  
set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIX
  
  
–指定URLから指定の属性(NSURLTagNamesKey)を取得
  
try
    set aTargAttr to {current application’s NSURLTagNamesKey}
    
set {attsNSDictionary, theError} to aURL’s resourceValuesForKeys:aTargAttr |error|:(reference)
    
if attsNSDictionary is missing value then
      error (theError’s localizedDescription() as text)
    end if
    
    
–NSURLTagNamesKeyのリストを返す
    
set aList to NSURLTagNamesKey of (attsNSDictionary as list of string or string)
    
return aList as list
    
  on error
    return {}
  end try
end getFinderTagList

★Click Here to Open This Script 

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

FBEncryptorで文字列の暗号化、復号化

Posted on 2月 19, 2018 by Takaaki Naganoya

–> FBEncryptorKit.framework

AppleScript名:FBEncryptorで文字列の暗号化、復号化
— Created 2016-02-16 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "FBEncryptorKit" –https://github.com/dev5tec/FBEncryptor

set aStr to "ABCDEFあいうえお
かきくけこGHIJKLMN"

set aKey to "piyomaru"

–Encryption
set aEnc to (current application’s FBEncryptorAES’s encryptBase64String:aStr keyString:aKey separateLines:true) as string
–>  "N0/E5FB97DY+qOFtfKK9CCsAMKznyej94Ons1lC90V/9vMJIaBw5R+mbaxaTm711"

–Decription
set aDec to (current application’s FBEncryptorAES’s decryptBase64String:aEnc keyString:aKey) as string
(*
"ABCDEFあいうえお
かきくけこGHIJKLMN"
*)

★Click Here to Open This Script 

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

ファイルのMD5、SHA1、SHA512のハッシュ値を求める

Posted on 2月 19, 2018 by Takaaki Naganoya

–> md5Lib.framework

AppleScript名:ファイルのMD5、SHA1、SHA512のハッシュ値を求める
— Created 2016-02-11 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "md5Lib" –https://github.com/JoeKun/FileMD5Hash

set aPath to POSIX path of (choose file)

set a to (current application’s FileHash’s md5HashOfFileAtPath:aPath) as string
–>  "329e854b9993405414c66faac0e80b86"

set b to (current application’s FileHash’s sha1HashOfFileAtPath:aPath) as string
–>  "50847286df61f304d142c6a0351e39029f010fc2"

set c to (current application’s FileHash’s sha512HashOfFileAtPath:aPath) as string
–>  "5132a7b477652db414521b36……..1a6ff240e861752c"

★Click Here to Open This Script 

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

NSStringからSHA-3のハッシュ値を求める

Posted on 2月 19, 2018 by Takaaki Naganoya

–> SHA3Kit.framework

AppleScript名:NSStringからSHA-3のハッシュ値を求める
— Created 2017-08-09 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "SHA3Kit" –https://github.com/jaeggerr/NSString-SHA3

set origData to (current application’s NSString’s stringWithString:"hello")
set aHash1 to (origData’s sha3:256) as string
–> "1C8AFF950685C2ED4BC3174F3472287B56D9517B9C948127319A09A7A36DEAC8"

set aHash2 to (origData’s sha3:224) as string

set aHash3 to (origData’s sha3:384) as string

set aHash4 to (origData’s sha3:512) as string

★Click Here to Open This Script 

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

NSDataからMD5値を計算する

Posted on 2月 19, 2018 by Takaaki Naganoya

–> md5FromDataKit.framework

AppleScript名:NSDataからMD5値を計算する
— Created 2016-02-11 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "md5FromDataKit" –https://github.com/siuying/NSData-MD5

set aStr to "ぴよまるソフトウェア"
set aNSStr to current application’s NSString’s stringWithString:aStr
set aData to aNSStr’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set aMD5Hex to (current application’s NSData’s MD5HexDigest:aData) as string
–>  "2d0b4e205f274f20b17dc8ca4870f1db"

set aMD5 to (current application’s NSData’s MD5Digest:aData)’s |description|() as string
–>  <2d0b4e20 5f274f20 b17dc8ca 4870f1db>

★Click Here to Open This Script 

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

ZipZap frameworkを使ってZipアーカイブ内の情報を取得

Posted on 2月 19, 2018 by Takaaki Naganoya

–> ZipZap.framework

AppleScript名:ZipZap frameworkを使ってZipアーカイブ内の情報を取得
— Created 2015-10-11 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "ZipZap" –https://github.com/pixelglow/zipzap

set aPath to POSIX path of (choose file of type {"public.zip-archive"})
set oldArchive to current application’s ZZArchive’s archiveWithURL:(current application’s |NSURL|’s fileURLWithPath:aPath) |error|:(missing value)
–>  (ZZArchive) <ZZArchive: 0x7fe59aa932d0>

set entryCount to oldArchive’s entries()’s |count|()
–>  180 –this zip archive includes 180 file or folders inside

set firstArchEntry to oldArchive’s entries()’s firstObject()
–>  (ZZOldArchiveEntry) <ZZOldArchiveEntry: 0x7fe593fa4dc0>

set aList to oldArchive’s entries() as list

repeat with i in aList
  set uSize to i’s uncompressedSize() as real
  
set cSize to i’s compressedSize() as real
  
set comF to i’s compressed() as boolean
  
set encF to i’s encrypted() as boolean
  
set modD to i’s lastModified() as date
  
set chkSum to i’s crc32() as text –The CRC32 code of the entry file: 0 for new entries.
  
set aFileMode to i’s fileMode() as text — The UNIX file mode for the entry: 0 for new or non-UNIX entries. This includes the file type bits.
  
set aFileName to i’s fileName() as text
  
  
log {aFileName, aFileMode, chkSum, modD, encF, comF, cSize, uSize}
  
–> 21:28:01.817 (* {"ZXingObjC.framework/", "16877d", "0", date "2015年10月10日土曜日 10:07:16", false, false, 0.0, 0.0} *)
  
–> 21:28:01.818 (* {"ZXingObjC.framework/Headers", "41453d", "2.393740531E+9", date "2015年10月10日土曜日 10:07:10", false, false, 24.0, 24.0} *)
  
–> 21:28:01.820 (* {"ZXingObjC.framework/Versions/", "16877d", "0", date "2015年10月10日土曜日 10:07:10", false, false, 0.0, 0.0} *)
  
–> 21:28:01.821 (* {"ZXingObjC.framework/Versions/3.1.0/", "16877d", "0", date "2015年10月10日土曜日 10:07:16", false, false, 0.0, 0.0} *)
  
–> 21:28:01.822 (* {"ZXingObjC.framework/Versions/3.1.0/Headers/", "16877d", "0", date "2015年10月10日土曜日 10:07:16", false, false, 0.0, 0.0} *)
  
  
–set aData to (i’s newDataWithError:false)–Uncompressed raw data
  
–log aData
end repeat

★Click Here to Open This Script 

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

指定フォルダをtarでまとめる

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:指定フォルダをtarでまとめる
set a to choose folder
set b to tarFolder(a) of me

–指定フォルダをtarでまとめる
on tarFolder(a)
  set anAlias to a as alias
  
  
tell application "Finder"
    set aParent to parent of anAlias
    
set aFolName to name of anAlias
  end tell
  
  
set aParent to aParent as alias
  
  
set preCMD to "cd " & quoted form of POSIX path of aParent
  
set tarCMD to "tar cvf " & quoted form of (aFolName & ".tar") & " " & quoted form of (aFolName & "/")
  
  
try
    set aRes to do shell script (preCMD & " && " & tarCMD)
  on error
    return false
  end try
  
  
  
tell application "Finder"
    set tarAlias to ((aParent as string) & aFolName & ".tar") as alias
    
set tarEx to exists of tarAlias
  end tell
  
  
if tarEx = true then
    return tarAlias
  else
    return false
  end if
end tarFolder

★Click Here to Open This Script 

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

指定のtarアーカイブを展開する

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:指定のtarアーカイブを展開する
set a to choose file with prompt ".tarファイルを指定してください"
set b to extractTar(a) of me

–指定tarアーカイブを展開する
on extractTar(a)
  set anAlias to a as alias
  
  
tell application "Finder"
    set aParent to parent of anAlias
    
set aFileName to name of anAlias
  end tell
  
  
set aParent to aParent as alias
  
  
set preCMD to "cd " & quoted form of POSIX path of aParent
  
set tarCMD to "tar xf " & quoted form of (aFileName)
  
  
try
    set aRes to do shell script (preCMD & " && " & tarCMD)
  on error
    return false
  end try
  
  
set newName to text 1 thru -5 of aFileName –展開後の、".tar"を除去した名前
  
set extractFol to (aParent as string) & newName & ":"
  
  
try
    return (extractFol as alias)
  on error
    return false
  end try
  
end extractTar

★Click Here to Open This Script 

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

テキストをhexdump v4

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:テキストをhexdump v4
— Created 2015-01-24 by Shane Stanley
— Modified 2015-01-26 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to "あいうえお"

set theNSString to current application’s NSString’s stringWithString:aStr
set aList to hexDumpString(theNSString) of me

on hexDumpString(theNSString)
  set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set theString to (theNSData’s |description|()’s uppercaseString())
  
  
–Remove "<" ">" characters in head and tail
  
set tLength to (theString’s |length|()) – 2
  
set aRange to current application’s NSMakeRange(1, tLength)
  
set theString2 to theString’s substringWithRange:aRange
  
  
–Replace Space Characters
  
set aString to current application’s NSString’s stringWithString:theString2
  
set bString to aString’s stringByReplacingOccurrencesOfString:" " withString:""
  
  
set aResList to splitString(bString, 2)
  
–> {​​​​​"E3", ​​​​​"81", ​​​​​"82", ​​​​​"E3", ​​​​​"81", ​​​​​"84", ​​​​​"E3", ​​​​​"81", ​​​​​"86", ​​​​​"E3", ​​​​​"81", ​​​​​"88", ​​​​​"E3", ​​​​​"81", ​​​​​"8A"​​​}
  
  
return aResList
end hexDumpString

–Split NSString in specified aNum characters
on splitString(aText, aNum)
  set aStr to current application’s NSString’s stringWithString:aText
  
if aStr’s |length|() ≤ aNum then return aText
  
  
set anArray to current application’s NSMutableArray’s new()
  
set mStr to current application’s NSMutableString’s stringWithString:aStr
  
  
set aRange to current application’s NSMakeRange(0, aNum)
  
  
repeat while (mStr’s |length|()) > 0
    if (mStr’s |length|()) < aNum then
      anArray’s addObject:(current application’s NSString’s stringWithString:mStr)
      
mStr’s deleteCharactersInRange:(current application’s NSMakeRange(0, mStr’s |length|()))
    else
      anArray’s addObject:(mStr’s substringWithRange:aRange)
      
mStr’s deleteCharactersInRange:aRange
    end if
  end repeat
  
  
return (current application’s NSArray’s arrayWithArray:anArray) as list
end splitString

★Click Here to Open This Script 

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

POSIX pathからAliasへの戻し方(解説つき)

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:POSIX pathからAliasへの戻し方(解説つき)
set a to choose folder
log {"alias", a}
–> (*alias, alias Cherry:Users:me:Desktop:*)

set aP to POSIX path of a
log {"POSIX path", aP}
–> (*POSIX path, /Users/me/Desktop/*)–shell commandに渡すときにはパスにquoted form ofを付ける必要アリ。Cocoaに渡すときにはquoted form ofは不要

set aF to POSIX file aP
log {"file", aF}
–> (*file, file Cherry:Users:me:Desktop:*)

set anAlias to aF as alias
log {"alias", anAlias}
–> (*alias, alias Cherry:Users:me:Desktop:*)

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ファイルパスの階層とリストとの相互変換

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:ファイルパスの階層とリストとの相互変換
— Created 2016-11-07 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aStr to current application’s NSString’s stringWithString:"/Users/maro/Desktop/aTEST.scpt"
set aList to aStr’s pathComponents() as list
–>  {​​​​​"/", ​​​​​"Users", ​​​​​"maro", ​​​​​"Desktop", ​​​​​"aTEST.scpt"​​​}

set bList to {"/", "Users", "maro", "Desktop", "aTEST.scpt"}
set bStr to (current application’s NSString’s pathWithComponents:bList) as string
–>  "/Users/maro/Desktop/aTEST.scpt"

★Click Here to Open This Script 

Posted in File path list | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定のパスのファイル名をUUIDにして拡張子を付け替える

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:指定のパスのファイル名をUUIDにして拡張子を付け替える
— Created 2017-09-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSUUID : a reference to current application’s NSUUID

set aFile to POSIX path of (choose file of type {"public.html"})
set aRes to changeFileNameWithUUID(aFile, "pdf") of me

on changeFileNameWithUUID(aFile, newExt)
  set thePath to NSString’s stringWithString:aFile
  
set path1 to thePath’s stringByDeletingLastPathComponent()
  
set theName to NSUUID’s UUID()’s UUIDString()
  
set path2 to (path1’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:newExt
  
return path2
end changeFileNameWithUUID

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

POSIX pathからファイル名と親フォルダを抽出

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:POSIX pathからファイル名と親フォルダを抽出
— Created 2016-05-25 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set targPath to "/20160524/IMG_1198.JPG"

set aPath to current application’s NSString’s stringWithString:targPath
set fileName to (aPath’s lastPathComponent()) as string
–>  "IMG_1198.JPG"
set parentFol to (aPath’s stringByDeletingLastPathComponent()) as string
–>  "/20160524"

★Click Here to Open This Script 

Posted in File path | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • UI Browserがgithub上でソース公開され、オープンソースに
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • iWork 12.2がリリースされた
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • ChatGPTでchatに対する応答文を取得
  • macOS 13でNSNotFoundバグふたたび
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)
  • AppleScriptの数値変数で指数表示にならない最大値、最小値
  • バカスタム App選手権 入賞!
  • Dockアイコンにプログレスバーを追加 v3
  • macOS 14, Sonoma

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (187) 13.0savvy (61) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) 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 (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) 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
  • 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)
  • 未分類

アーカイブ

  • 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