« [FFMPEG]サウンドファイルを左右で別ファイルにする | トップページ | [NSTask]シェルコマンドの実行 »

[trashItemAtURL]フォルダの中身をゴミ箱に入れる

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL


###################################
#####入力フォルダ
###################################
###ダイアログのデフォルト
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
###ダイアログテキスト
set strPromptText to "入力フォルダを選んでください"
try
set listResponse to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list
on error
log "エラーしました"
return
end try
###エリアス
set aliasDirPath to (item 1 of listResponse) as alias
###UNIXパス
set strFilePathText to POSIX path of aliasDirPath as text

###この指定でもOK
###set strFilePathText to "~/Desktop/名称未設定フォルダ"

###String
set ocidFilePath to (refNSString's stringWithString:strFilePathText)
###絶対パスで
set ocidFilePathString to ocidFilePath's stringByStandardizingPath
###NSURL
set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)


###実行
doGo2TrashDirContentsURL(ocidFilePathURL)



################################
####フォルダの中身をゴミ箱へ入れる
################################
on doGo2TrashDirContentsURL(ocidDirUrl)
###ファイルマネージャー初期化
set objFileManager to refMe's NSFileManager's defaultManager()
################################
####渡された値がNSURL以外の場合の処理
try
set strClassName to class of ocidDirUrl as text
####渡された値がテキストだったら
if strClassName is "text" then
set strDirUrl to ocidDirUrl as text
####渡された値がエイリアスだったら
else if strClassName is "alias" then
set strDirPath to POSIX path of ocidDirUrl as text
end if
set ocidFilePath to (refNSString's stringWithString:strDirPath)
set ocidFilePathString to ocidFilePath's stringByStandardizingPath
set ocidDirPathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)
on error
######ocid形式の値だったら
set strClassName to ocidDirUrl's className() as text
###テキストなら
if strClassName contains "NSCFString" then
set ocidFilePathString to ocidDirUrl's stringByStandardizingPath
set ocidDirPathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)
###NSURLなら
else if strClassName is "NSURL" then
set ocidDirPathURL to ocidDirUrl
else
error "NSURLを指定してください" number -9999
return
end if
end try
################################
####渡されたパスが無い場合はエラー
set ocidFilePathString to ocidDirPathURL's |path|()
set boolFolderExists to (objFileManager's fileExistsAtPath:ocidFilePathString isDirectory:true)
if boolFolderExists is false then
log "パス先が実在しません"
####パス先ない場合は作っておく
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)

set listBoolMakeDir to objFileManager's createDirectoryAtURL:(ocidDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
return
end if
################################
####ディレクトリか?の判断 ファイルならエラーで止める
set ocidResultArray to ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference)
if (item 3 of ocidResultArray) is not (missing value) then
doGetErrorData(item 3 of ocidResultArray)
end if
set ocidResultValue to item 2 of ocidResultArray
-->false
set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
if ocidResultValue is ocidFalse then
##error "パス先がファイルですディレクトリを指定してください" number -9999
log "パス先がファイルですディレクトリを指定してください"
return
end if
##################################
##内包リスト
set listResult to objFileManager's contentsOfDirectoryAtURL:ocidDirPathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:0 |error|:(reference)
###エラー発生時
if (item 2 of listResult) is not (missing value) then
doGetErrorData(item 2 of listResult)
end if
###結果
set ocidPathArray to item 1 of listResult
repeat with itemPathArray in ocidPathArray
set ocidPathArrayURL to itemPathArray
##ゴミ箱に入れる
set listResult to (objFileManager's trashItemAtURL:ocidPathArrayURL resultingItemURL:(missing value) |error|:(reference))
###エラー発生時
if (item 2 of listResult) is not (missing value) then
doGetErrorData(item 2 of listResult)
end if

end repeat

end doGo2TrashDirContentsURL







to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

« [FFMPEG]サウンドファイルを左右で別ファイルにする | トップページ | [NSTask]シェルコマンドの実行 »

AppleScript NSFileManager」カテゴリの記事