[df]デバイス名取得(リムーバルメディア)
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | 外付けディスク=リムーバブルメディアの名称を取得します |
005 | CD/DVDメディア か USBメモリであって |
006 | 外付けHDDは別です |
007 | |
008 | |
009 | v1 初回作成 |
010 | v1.1 missing value対策を入れた |
011 | com.cocolog-nifty.quicktimer.icefloe *) |
012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
013 | ##自分環境がos12なので2.8にしているだけです |
014 | use AppleScript version "2.8" |
015 | use framework "Foundation" |
016 | use framework "AppKit" |
017 | use scripting additions |
018 | |
019 | property refMe : a reference to current application |
020 | |
021 | set appFileManager to refMe's NSFileManager's defaultManager() |
022 | |
023 | set ocidFalse to (refMe's NSNumber's numberWithBool:false) |
024 | set ocidTrue to (refMe's NSNumber's numberWithBool:true) |
025 | set ocidRemovableName to ("見つかりませんでした") as text |
026 | set ocidInternalName to ("見つかりませんでした") as text |
027 | #################################### |
028 | ####全マウントボリューム取得 |
029 | #################################### |
030 | ##オプション |
031 | set ocidOption to (refMe's NSVolumeEnumerationProduceFileReferenceURLs) |
032 | ##取得キー |
033 | set ocidKeysArray to {(refMe's NSURLNameKey), (refMe's NSURLPathKey), (refMe's NSURLVolumeIsEjectableKey), (refMe's NSURLVolumeIsRemovableKey), (refMe's NSURLVolumeIsInternalKey)} |
034 | ##取得 |
035 | set ocidDiskArray to appFileManager's mountedVolumeURLsIncludingResourceValuesForKeys:(ocidKeysArray) options:(ocidOption) |
036 | ##ボリュームの数だけ繰返し |
037 | repeat with itemDiskArrayURL in ocidDiskArray |
038 | ##リソースキーを取得 |
039 | set listResDict to (itemDiskArrayURL's resourceValuesForKeys:ocidKeysArray |error|:(reference)) |
040 | ##取り出し |
041 | set ocidResourceValuesDict to item 1 of listResDict |
042 | ##内臓判定 |
043 | set boolIsInternal to (ocidResourceValuesDict's objectForKey:(refMe's NSURLVolumeIsInternalKey)) |
044 | ###外付けを取得する |
045 | if boolIsInternal is (ocidFalse) then |
046 | set ocidInternalName to (ocidResourceValuesDict's objectForKey:(refMe's NSURLNameKey)) |
047 | ##リムーバブル判定 -->CD/DVDメディア か USBメモリ |
048 | set boolIsRemovable to (ocidResourceValuesDict's objectForKey:(refMe's NSURLVolumeIsRemovableKey)) |
049 | if boolIsRemovable is (ocidTrue) then |
050 | set ocidRemovableName to (ocidResourceValuesDict's objectForKey:(refMe's NSURLNameKey)) |
051 | end if |
052 | end if |
053 | end repeat |
054 | |
055 | |
056 | #################################### |
057 | ####デバイス名取得 |
058 | #################################### |
059 | ##コマンド整形 |
060 | set strCommandText to ("/bin/df -k") as text |
061 | ##コマンド実行 |
062 | set strDfResponse to (do shell script strCommandText) as text |
063 | ##Stringsに |
064 | set ocdiDfResponse to refMe's NSString's stringWithString:(strDfResponse) |
065 | ##改行指定 |
066 | set ocidCharacterSet to refMe's NSCharacterSet's newlineCharacterSet() |
067 | ##改行でリスト |
068 | set ocidStringArray to ocdiDfResponse's componentsSeparatedByCharactersInSet:(ocidCharacterSet) |
069 | ##リストの数だけ繰返し |
070 | repeat with itemStringArray in ocidStringArray |
071 | set strItemStringArray to itemStringArray as text |
072 | # log strItemStringArray |
073 | if strItemStringArray contains (ocidRemovableName as text) then |
074 | ##スペースを区切り文字としてArrayにして |
075 | set ocidCharacterSet to refMe's NSCharacterSet's whitespaceCharacterSet() |
076 | set ocidDevArray to (itemStringArray's componentsSeparatedByCharactersInSet:(ocidCharacterSet)) |
077 | --->最初のPrint項目がデバイスID |
078 | set ocidFilesystem to (ocidDevArray's firstObject()) |
079 | #/区切りでArrayにして |
080 | set ocidMountPointArray to (itemStringArray's componentsSeparatedByString:(" /")) |
081 | set ocidMountPoint to refMe's NSMutableString's alloc()'s init() |
082 | (ocidMountPoint's appendString:("/")) |
083 | (ocidMountPoint's appendString:(ocidMountPointArray's lastObject())) |
084 | |
085 | end if |
086 | end repeat |
087 | |
088 | |
089 | log ocidInternalName as text |
090 | #リムーバブルドライブの名前 |
091 | log ocidRemovableName as text |
092 | #Filesystem |
093 | log ocidFilesystem as text |
094 | # |
095 | log ocidMountPoint as text |
AppleScriptで生成しました |
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
error number -128
com.cocolog-nifty.quicktimer.icefloe
*)
#
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)
####################################
####全マウントボリューム取得
####################################
##オプション
set ocidOption to (refMe's NSVolumeEnumerationProduceFileReferenceURLs)
##取得キー
set ocidKeysArray to {(refMe's NSURLNameKey), (refMe's NSURLPathKey), (refMe's NSURLVolumeIsEjectableKey), (refMe's NSURLVolumeIsRemovableKey), (refMe's NSURLVolumeIsInternalKey)}
##取得
set ocidDiskArray to appFileManager's mountedVolumeURLsIncludingResourceValuesForKeys:(ocidKeysArray) options:(ocidOption)
##ボリュームの数だけ繰返し
repeat with itemDiskArrayURL in ocidDiskArray
##リソースキーを取得
set listResDict to (itemDiskArrayURL's resourceValuesForKeys:ocidKeysArray |error|:(reference))
##取り出し
set ocidResourceValuesDict to item 1 of listResDict
##内臓判定
set boolIsInternal to (ocidResourceValuesDict's objectForKey:(refMe's NSURLVolumeIsInternalKey))
###外付けを取得する
if boolIsInternal is (ocidFalse) then
set ocidInternalName to (ocidResourceValuesDict's objectForKey:(refMe's NSURLNameKey))
log ocidInternalName as text
##リムーバブル判定 -->CD/DVDメディア か USBメモリ
set boolIsRemovable to (ocidResourceValuesDict's objectForKey:(refMe's NSURLVolumeIsRemovableKey))
if boolIsRemovable is (ocidTrue) then
set ocidRemovableName to (ocidResourceValuesDict's objectForKey:(refMe's NSURLNameKey))
log ocidRemovableName as text
end if
end if
end repeat
####################################
####デバイス名取得
####################################
##コマンド整形
set strCommandText to ("/bin/df -k") as text
##コマンド実行
set strDfResponse to (do shell script strCommandText) as text
##Stringsに
set ocdiDfResponse to refMe's NSString's stringWithString:(strDfResponse)
##改行指定
set ocidCharacterSet to refMe's NSCharacterSet's newlineCharacterSet()
##改行でリスト
set ocidStringArray to ocdiDfResponse's componentsSeparatedByCharactersInSet:(ocidCharacterSet)
##リストの数だけ繰返し
repeat with itemStringArray in ocidStringArray
set strItemStringArray to itemStringArray as text
if strItemStringArray contains (ocidRemovableName as text) then
log strItemStringArray
##スペース指定
set ocidCharacterSet to refMe's NSCharacterSet's whitespaceCharacterSet()
set ocidDevArray to (itemStringArray's componentsSeparatedByCharactersInSet:(ocidCharacterSet))
--->指定ドライブの
log (ocidDevArray's objectAtIndex:0) as text
end if
end repeat
| 固定リンク
「Admin Volumes」カテゴリの記事
- ディスクのイジェクト(2025.02.23)
- [diskutil](ディスクとは違う意味の)ボリュームの所有権とは?(2025.02.16)
- [System Events]ディスクのアンマウント(2025.01.17)
- [hdiutil]ディスクイメージのマウントポイントをユニークな値で作成する(2025.01.16)
- inode番号から対象のファイルを探す(warning: inode (id 9999999): Resource Fork xattr is missing or empty for compressed file)(error: doc-id tree: record exists for doc-id xxxxx, file-id 9999999 but no inode references this doc-id)(2024.10.22)
「Disk」カテゴリの記事
- [diskutil]フォーマット判定(大文字小文字判別)Acrobatでの『印刷出来ません』エラーに対応用(2025.04.30)
- [bluray]VLCのブルーレイ再生ライブラリのインストール(3.0.21対応)(2025.04.19)
- [考え中]DMGディスクイメージ系のインストール(2025.02.10)
- [NSFileManager]ディスク残を求める(2025.01.30)
- [UDSP]SPARSE - スパースディスクイメージを作成する(2024.10.16)
「CD/DVD」カテゴリの記事
- com.apple.CD-DVD-Settings.extensionシステム設定を開く(2023.07.19)
- [com.apple.digihub]com.apple.digihub設定スクリプト(2023.07.19)
- [CD/DVD]基本(2023.05.20)
- ISOイメージを作るDD版.scpt(2023.05.20)
- ISOイメージを作るHDIUTIL版(2023.05.20)