AppleScript Trash

[メンテナンス] Containers 配下の全てのCachesをゴミ箱に入れる


【スクリプトエディタで開く】|

#!/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 framework "AppKit"
use scripting additions

property refMe : a reference to current application


set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirURL to ocidURLsArray's firstObject()
set ocidContainersDirPathURL to ocidLibraryDirURL's URLByAppendingPathComponent:("Containers")

set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
set ocidKeyArray to refMe's NSArray's arrayWithArray:({(refMe's NSURLPathKey), (refMe's NSURLIsDirectoryKey), (refMe's NSURLIsSymbolicLinkKey)})
set listSubPathResult to (appFileManager's contentsOfDirectoryAtURL:(ocidContainersDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference))
set ocidSubPathURLArray to item 1 of listSubPathResult
repeat with itemSubPathURL in ocidSubPathURLArray
  set ocidCachesDirPathURL to (itemSubPathURL's URLByAppendingPathComponent:("Data/Library/Caches"))
  ###【1】シンボリックリンクか?判定
  set litsBoolIsSymLink to (ocidCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error|:(reference))
  if (item 2 of litsBoolIsSymLink) = (refMe's NSNumber's numberWithBool:true) then
    log "シンボリックリンクなので処理しない"
  else if (item 2 of litsBoolIsSymLink) = (refMe's NSNumber's numberWithBool:false) then
    ###【2】ディレクトリか?判定
    set litsBoolIsDir to (ocidCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
    if (item 2 of litsBoolIsDir) = (refMe's NSNumber's numberWithBool:false) then
      log "フォルダでは無いなら処理しない"
    else
      ###【3】ディレクトリの中のコンテンツの取得
      set listCachesDirPathResult to (appFileManager's contentsOfDirectoryAtURL:(ocidCachesDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference))
      set ocidCachesDirPathResultArray to item 1 of listCachesDirPathResult
      ###【3】で取得したコンテンツの結果があるならゴミ箱に入れる
      if ocidCachesDirPathResultArray ≠ (missing value) then
repeat with itemCachesDirPathURL in ocidCachesDirPathResultArray
##log itemachesDirPath's |path|() as text
##ゴミ箱に入れる
set listResult to (appFileManager's trashItemAtURL:(itemCachesDirPathURL) resultingItemURL:(itemCachesDirPathURL) |error|:(reference))
end repeat
      else
log "3のディレクトリの中身が空なので処理するものがない"
      end if
    end if
  end if
end repeat

|

[Finder 設定]ゴミ箱を空にする時の警告の有無


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application


###警告を出さないようにする
tell application "Finder"
  set boolWarns to (trash's warns before emptying) as boolean
  if boolWarns is true then
    set trash's warns before emptying to false
  end if
  set boolWarns to (trash's warns before emptying) as boolean
end tell


###警告出すようにする
tell application "Finder"
  set boolWarns to (trash's warns before emptying) as boolean
  if boolWarns is false then
    set trash's warns before emptying to true
  end if
  set boolWarns to (trash's warns before emptying) as boolean
end tell



######警告出すようにする
set strFilePath to "~/Library/Preferences/com.apple.finder.plist"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath)
### 読み込み
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL)
### 値確認
set boolValue to ocidPlistDict's valueForKey:("WarnOnEmptyTrash")
if boolValue = (refMe's NSNumber's numberWithBool:false) then
ocidPlistDict's setValue:(refMe's NSNumber's numberWithBool:true) forKey:("WarnOnEmptyTrash")
  ####保存
  set boolDone to ocidPlistDict's writeToURL:(ocidFilePathURL) atomically:true
end if

###警告を出さないようにする
set strFilePath to "~/Library/Preferences/com.apple.finder.plist"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath)
### 読み込み
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL)
### 値確認
set boolValue to ocidPlistDict's valueForKey:("WarnOnEmptyTrash")
if boolValue = (refMe's NSNumber's numberWithBool:true) then
ocidPlistDict's setValue:(refMe's NSNumber's numberWithBool:false) forKey:("WarnOnEmptyTrash")
  ####保存
  set boolDone to ocidPlistDict's writeToURL:(ocidFilePathURL) atomically:true
end if




###警告を出さないようにする
set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:WarnOnEmptyTrash\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
set strBoolWarns to (do shell script strCommandText) as text
if strBoolWarns is "true" then
  ##値の変更 BOOL
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:WarnOnEmptyTrash bool false\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
  ##確認
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:WarnOnEmptyTrash\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
  ##保存
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
end if


###警告出すようにする
set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:WarnOnEmptyTrash\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
set strBoolWarns to (do shell script strCommandText) as text
if strBoolWarns is "false" then
  ##値の変更 BOOL
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:WarnOnEmptyTrash bool true\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
  ##確認
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Print:WarnOnEmptyTrash\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
  ##保存
  set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"$HOME/Library/Preferences/com.apple.finder.plist\"") as text
  do shell script strCommandText
end if

###警告出すようにする
set strCommandText to ("/usr/bin/defaults read com.apple.finder WarnOnEmptyTrash") as text
set strBoolWarns to (do shell script strCommandText) as text
if strBoolWarns is "0" then
  set boolWarns to false as boolean
  set strCommandText to ("/usr/bin/defaults write com.apple.finder WarnOnEmptyTrash -boolean true") as text
  do shell script strCommandText
end if
set strCommandText to ("/usr/bin/defaults read com.apple.finder WarnOnEmptyTrash") as text
set strBoolWarns to (do shell script strCommandText) as text

###警告を出さないようにする
set strCommandText to ("/usr/bin/defaults read com.apple.finder WarnOnEmptyTrash") as text
set strBoolWarns to (do shell script strCommandText) as text
if strBoolWarns is "1" then
  set boolWarns to false as boolean
  set strCommandText to ("/usr/bin/defaults write com.apple.finder WarnOnEmptyTrash -boolean false") as text
  do shell script strCommandText
end if
set strCommandText to ("/usr/bin/defaults read com.apple.finder WarnOnEmptyTrash") as text
set strBoolWarns to (do shell script strCommandText) as text



|

[bash]全てのローカルアカウントのゴミ箱を空にする 少し修正(警告出ないようにした)


【スクリプトエディタで開く】|

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
# 要管理者権限
########################################
###管理者インストールしているか?チェック
USER_WHOAMI=$(/usr/bin/whoami)
/bin/echo "実行したユーザーは:$USER_WHOAMI"
if [ "$USER_WHOAMI" != "root" ]; then
  /bin/echo "このスクリプトを実行するには管理者権限が必要です。"
  /bin/echo "sudo で実行してください"
  ### path to me
  SCRIPT_PATH="${BASH_SOURCE[0]}"
  /bin/echo "/usr/bin/sudo \"$SCRIPT_PATH\""
  /bin/echo "↑を実行してください"
  exit 1
else
  ###実行しているユーザー名
  SUDO_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
  /bin/echo "実行ユーザー:" "$SUDO_USER"
fi

###コンソールユーザーにのみ処理する
CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
/bin/echo "CONSOLE_USER:$CONSOLE_USER"
if [ -z "$CONSOLE_USER" ]; then
  /bin/echo "コンソールユーザーが無い=電源入れてログインウィンドウ状態"
else
  /bin/echo "コンソールユーザーに対しての処理"
  ###エラーよけにゴミ箱に削除する物を作っておく
  /usr/bin/sudo -u "$CONSOLE_USER" /usr/bin/touch "/Users/$CONSOLE_USER/.Trash/ForEmptyTrash.tmp"
  ##この1秒がないとゴミ箱がすでに空の場合にエラーが出る
  /bin/sleep 1
  ###ゴミ箱を空に 警告無し
  /usr/bin/sudo -u "$CONSOLE_USER" /usr/bin/osascript -e "tell application id \"com.apple.Finder\" to empty the trash without warns before emptying"
fi

########################################
###ローカルのユーザーアカウントを取得
TEXT_RESULT=$(/usr/bin/dscl localhost -list /Local/Default/Users PrimaryGroupID | /usr/bin/awk '$2 == 20 { print $1 }')
###リストにする
read -d '\\n' -r -a LIST_USER <<<"$TEXT_RESULT"
###リスト内の項目数
NUM_CNT=${#LIST_USER[@]}
/bin/echo "ユーザー数:" "$NUM_CNT"
########################################
##リストの内容を順番に処理
for ITEM_LIST in "${LIST_USER[@]}"; do
  /bin/echo "LIST_USER:ユーザー名:" "${ITEM_LIST}"
  if [ "$SUDO_USER" == "root" ]; then
    ###ユーザーチェンジして
    /usr/bin/su - "${ITEM_LIST}"
  fi
  ###ゴミ箱パス
  TRASH_PATH="/Users/${ITEM_LIST}/.Trash"
  TRASH_CNT=$(/usr/bin/sudo -u "${ITEM_LIST}" /bin/ls "$TRASH_PATH" | wc -l)
  /bin/echo "削除前ゴミ箱の内包数:" "$TRASH_CNT"
  ##ゴミ箱の中を空にする
  /usr/bin/sudo -u "${ITEM_LIST}" /usr/bin/find "/Users/${ITEM_LIST}/.Trash" -mindepth 1 -delete
  TRASH_CNT=$(/usr/bin/sudo -u "${ITEM_LIST}" /bin/ls "$TRASH_PATH" | wc -l)
  /bin/echo "削除後ゴミ箱の内包数:" "$TRASH_CNT"
  ##ルートに戻す
  if [ "$SUDO_USER" == "root" ]; then
    ###ユーザーチェンジして
    /usr/bin/su - root
  fi

done

/bin/echo "処理終了しました"

exit 0


|

[bash]path to rash folder from user domain


【スクリプトエディタで開く】|

#!/bin/bash

ITEM_USER="user_name"

RASH_DIR=$(/usr/bin/sudo -u "${ITEM_USER}" /usr/bin/mktemp -d "/Users/${ITEM_USER}/.Trash/XXXXXXXX")
/bin/echo "$RASH_DIR"
## path to rash folder from user domain
exit 0


|

[bash]path to temporary items from user domain


【スクリプトエディタで開く】|

#!/bin/bash

ITEM_USER="user_name"

TMP_DIR=$(/usr/bin/sudo -u "$ITEM_USER" /usr/bin/mktemp -d)
/bin/echo "$TMP_DIR"
TMP_DIR_T=$(/usr/bin/dirname "$TMP_DIR")
TMP_FILE_NAME=$(/usr/bin/basename "$TMP_DIR")
/bin/echo "$TMP_DIR_T"
/bin/echo "$TMP_FILE_NAME"
TEMP_DIR_PATH="$TMP_DIR_T/TemporaryItems"
/bin/echo "$TEMP_DIR_PATH"
## path to temporary items from user domain
exit 0


|

[applescript]bashでのテンポラリー キャッシュ ゴミ箱 起動時に削除する項目


【スクリプトエディタで開く】|

#!/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

set appFileManager to refMe's NSFileManager's defaultManager()

####テンポラリ
set ocidTemporaryDirPathURL to appFileManager's temporaryDirectory
log ocidTemporaryDirPathURL as text
log className() of ocidTemporaryDirPathURL as text
###起動時に削除する項目
set ocidTemporaryItemsPathURL to ocidTemporaryDirPathURL's URLByAppendingPathExtension:"TemporaryItems"
###キャッシュ
set ocidContainerDirPathURL to ocidTemporaryDirPathURL's URLByDeletingLastPathComponent()
set ocidCachesPathURL to ocidContainerDirPathURL's URLByAppendingPathExtension:"C"

#####キャッシュ
set ocidUserCachesPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSCachesDirectory) inDomains:(refMe's NSUserDomainMask))
log item 1 of ocidUserCachesPathURLArray as alias
log className() of ocidUserCachesPathURLArray as text

####ゴミ箱
set ocidUserTrashPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSTrashDirectory) inDomains:(refMe's NSUserDomainMask))
log item 1 of ocidUserTrashPathURLArray as alias
log className() of ocidUserTrashPathURLArray as text

###旧記述
tell application "Finder"
  set aliasTemporaryItems to path to temporary items from local domain as alias with folder creation
  set aliasTemporaryDirPath to container of aliasTemporaryItems as alias
  
  set aliasTrash to path to trash folder from user domain as alias with folder creation
end tell




|

[Bash]bashでのテンポラリー キャッシュ ゴミ箱 起動時に削除する項目


【スクリプトエディタで開く】|

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe
########################################
# set -x
########################################
ITEM_USER=$(/usr/bin/whoami)

MKTEMP_DIR=$(/usr/bin/sudo -u "$ITEM_USER" /usr/bin/mktemp -d)
TEMP_DIR_T="$(/usr/bin/dirname "$MKTEMP_DIR")"
TEMP_DIR="$(/usr/bin/dirname "$TEMP_DIR_T")"
TEMP_DIR_C="$TEMP_DIR/C"
TEMP_DIR_0="$TEMP_DIR/0"
TEMP_DIR_CleanupAtStartup="$TEMP_DIR/Cleanup At Startup"
TEMP_DIR_TemporaryItems="$TEMP_DIR/TemporaryItems"

CHACHE_DIR="/Users/$ITEM_USER/Library/Caches"
USER_TRASH_DIR="/Users/$ITEM_USER/.Trash"

/usr/bin/sudo -u "$ITEM_USER" /bin/makdir -pm 777 "$TEMP_DIR_CleanupAtStartup"
/usr/bin/sudo -u "$ITEM_USER" /bin/makdir -pm 777 "$TEMP_DIR_TemporaryItems"

/bin/echo "TEMP_DIR:" "$TEMP_DIR"
/bin/echo "TEMP_DIR_C:" "$TEMP_DIR_C"
/bin/echo "TEMP_DIR_T:" "$TEMP_DIR_T"
/bin/echo "TEMP_DIR_0:" "$TEMP_DIR_0"
/bin/echo "TEMP_DIR_Cleanup:" "$TEMP_DIR_CleanupAtStartup"
/bin/echo "TEMP_DIR_TemporaryItems:" "$TEMP_DIR_TemporaryItems"

/bin/echo "CHACHE_DIR:" "$CHACHE_DIR"
/bin/echo "USER_TRASH_DIR:" "$USER_TRASH_DIR"

|

[bash]全てのローカルアカウントのゴミ箱を空にする 少し修正

ゴミ箱の中身をチェックするようにした

ダウンロード - 全ユーザーゴミ箱空に.zip


【スクリプトエディタで開く】|

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe

########################################
###管理者インストールしているか?チェック
USER_WHOAMI=$(/usr/bin/whoami)
/bin/echo "実行したユーザーは:$USER_WHOAMI"
if [ "$USER_WHOAMI" != "root" ]; then
  /bin/echo "このスクリプトを実行するには管理者権限が必要です。"
  /bin/echo "sudo で実行してください"
  exit 1
else
  ###実行しているユーザー名
  SUDO_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
  /bin/echo "実行ユーザー:" "$SUDO_USER"
fi

###コンソールユーザーにのみ処理する
CONSOLE_USER=$(/bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }')
/bin/echo "CONSOLE_USER:$CONSOLE_USER"
if [ -z "$CONSOLE_USER" ]; then
  /bin/echo "コンソールユーザーが無い=電源入れてログインウィンドウ状態"
else
  /bin/echo "コンソールユーザーに対しての処理"
  ###エラーよけにゴミ箱に削除する物を作っておく
  /usr/bin/sudo -u "$CONSOLE_USER" /usr/bin/touch "/Users/$CONSOLE_USER/.Trash/ForEmptyTrash.tmp"
  ##この1秒がないとゴミ箱がすでに空の場合にエラーが出る
  /bin/sleep 1
  ###ゴミ箱からに
  /usr/bin/sudo -u "$CONSOLE_USER" /usr/bin/osascript -e "tell application id \"com.apple.Finder\" to empty the trash"
fi

########################################
###ローカルのユーザーアカウントを取得
TEXT_RESULT=$(/usr/bin/dscl localhost -list /Local/Default/Users PrimaryGroupID | /usr/bin/awk '$2 == 20 { print $1 }')
###リストにする
read -d '\\n' -r -a LIST_USER <<<"$TEXT_RESULT"
###リスト内の項目数
NUM_CNT=${#LIST_USER[@]}
/bin/echo "ユーザー数:" "$NUM_CNT"
########################################
##リストの内容を順番に処理
for ITEM_LIST in "${LIST_USER[@]}"; do
  /bin/echo "LIST_USER:ユーザー名:" "${ITEM_LIST}"
  if [ "$SUDO_USER" == "root" ]; then
    ###ユーザーチェンジして
    /usr/bin/su - "${ITEM_LIST}"
  fi
  ###ゴミ箱パス
  TRASH_PATH="/Users/${ITEM_LIST}/.Trash"
  TRASH_CNT=$(/usr/bin/sudo -u "${ITEM_LIST}" /bin/ls "$TRASH_PATH" | wc -l)
  /bin/echo "削除前ゴミ箱の内包数:" "$TRASH_CNT"
  ##ゴミ箱の中を空にする
  /usr/bin/sudo -u "${ITEM_LIST}" /usr/bin/find "/Users/${ITEM_LIST}/.Trash" -mindepth 1 -delete
  TRASH_CNT=$(/usr/bin/sudo -u "${ITEM_LIST}" /bin/ls "$TRASH_PATH" | wc -l)
  /bin/echo "削除後ゴミ箱の内包数:" "$TRASH_CNT"
  ##ルートに戻す
  if [ "$SUDO_USER" == "root" ]; then
    ###ユーザーチェンジして
    /usr/bin/su - root
  fi

done

/bin/echo "処理終了しました"

exit 0


|

[trashItemAtURL]ゴミ箱に入れる 修正その2


【スクリプトエディタで開く】|

###################################
########処理 ゴミ箱に入れる
###################################

to doMoveToTrash(argFilePath)
  ###ファイルマネジャー初期化
  set appFileManager to refMe's NSFileManager's defaultManager()
  #########################################
  ###渡された値のClassを調べてとりあえずNSURLにする
  set refClass to class of argFilePath
  if refClass is list then
return "エラー:リストは処理しません"
  else if refClass is text then
    log "テキストパスです"
    set ocidArgFilePathStr to (refMe's NSString's stringWithString:argFilePath)
    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
  else if refClass is alias then
    log "エイリアスパスです"
    set strArgFilePath to (POSIX path of argFilePath) as text
    set ocidArgFilePathStr to (refMe's NSString's stringWithString:strArgFilePath)
    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
  else
    set refClass to (className() of argFilePath) as text
    if refClass contains "NSPathStore2" then
      log "NSPathStore2です"
      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:argFilePath)
    else if refClass contains "NSCFString" then
      log "NSCFStringです"
      set ocidArgFilePath to argFilePath's stringByStandardizingPath
      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
    else if refClass contains "NSURL" then
      set ocidArgFilePathURL to argFilePath
      log "NSURLです"
    end if
  end if
  #########################################
  ###
  -->false
  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
  -->true
  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
  #########################################
  ###NSURLがエイリアス実在するか?
  set ocidArgFilePath to ocidArgFilePathURL's |path|()
  set boolFileAlias to appFileManager's fileExistsAtPath:(ocidArgFilePath)
  ###パス先が実在しないなら処理はここまで
  if boolFileAlias = false then
    log ocidArgFilePath as text
    log "処理中止 パス先が実在しない"
return false
  end if
  #########################################
  ###NSURLがディレクトリなのか?ファイルなのか?
  set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference)
  # log (item 1 of listBoolDir)
  # log (item 2 of listBoolDir)
  # log (item 3 of listBoolDir)
  if (item 2 of listBoolDir) = ocidTrue then
    #########################################
    log "ディレクトリです"
    log ocidArgFilePathURL's |path| as text
    ##内包リスト
    set listResult to appFileManager's contentsOfDirectoryAtURL:ocidArgFilePathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:0 |error|:(reference)
    ###結果
    set ocidContentsPathURLArray to item 1 of listResult
    ###リストの数だけ繰り返し
    repeat with itemContentsPathURL in ocidContentsPathURLArray
      ###ゴミ箱に入れる
      set listResult to (appFileManager's trashItemAtURL:itemContentsPathURL resultingItemURL:(missing value) |error|:(reference))
    end repeat
  else
    #########################################
    log "ファイルです"
    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error|:(reference)
    if (item 2 of listBoolDir) = ocidTrue then
      log "エイリアスは処理しません"
return false
    end if
    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error|:(reference)
    if (item 2 of listBoolDir) = ocidTrue then
      log "シンボリックリンクは処理しません"
return false
    end if
    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSystemImmutableKey) |error|:(reference)
    if (item 2 of listBoolDir) = ocidTrue then
      log "システムファイルは処理しません"
return false
    end if
    ###ファイルをゴミ箱に入れる
    set listResult to (appFileManager's trashItemAtURL:ocidArgFilePathURL resultingItemURL:(missing value) |error|:(reference))
  end if
return true
end doMoveToTrash



|

ゴミ箱に入れる ちょっと修正


【スクリプトエディタで開く】|

to doMoveToTrash(argFilePath)
  ###ファイルマネジャー初期化
  set appFileManager to refMe's NSFileManager's defaultManager()
  #########################################
  ###渡された値のClassを調べてとりあえずNSURLにする
  set refClass to class of argFilePath
  if refClass is list then
return "エラーリストは処理しません"
  else if refClass is text then
    log "テキストパスです"
    set ocidArgFilePathStr to (refMe's NSString's stringWithString:argFilePath)
    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
  else if refClass is alias then
    log "エイリアスパスです"
    set strArgFilePath to (POSIX path of argFilePath) as text
    set ocidArgFilePathStr to (refMe's NSString's stringWithString:strArgFilePath)
    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
  else
    set refClass to (className() of argFilePath) as text
    if refClass contains "NSPathStore2" then
      log "NSPathStore2です"
      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:argFilePath)
    else if refClass contains "NSCFString" then
      log "NSCFStringです"
      set ocidArgFilePath to argFilePath's stringByStandardizingPath
      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
    else if refClass contains "NSURL" then
      set ocidArgFilePathURL to argFilePath
      log "NSURLです"
    end if
  end if
  #########################################
  ###
  -->false
  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
  -->true
  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
  #########################################
  ###NSURLがエイリアス実在するか?
  set ocidArgFilePath to ocidArgFilePathURL's |path|()
  set boolFileAlias to appFileManager's fileExistsAtPath:(ocidArgFilePath)
  ###パス先が実在しないなら処理はここまで
  if boolFileAlias = false then
    log ocidArgFilePath as text
    log "処理中止"
return false
  end if
  #########################################
  ###NSURLがディレクトリなのか?ファイルなのか?
  set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference)
  if (item 2 of listBoolDir) = ocidTrue then
    #########################################
    log "ディレクトリです"
    ##内包リスト
    set listResult to appFileManager's contentsOfDirectoryAtURL:ocidArgFilePathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:0 |error|:(reference)
    ###結果
    set ocidContentsPathURLArray to item 1 of listResult
    ###リストの数だけ繰り返し
    repeat with itemContentsPathURL in ocidContentsPathURLArray
      ###ゴミ箱に入れる
      set listResult to (appFileManager's trashItemAtURL:itemContentsPathURL resultingItemURL:(missing value) |error|:(reference))
    end repeat
  else
    #########################################
    log "ファイルです"
    ###念のためFilderでも調べる
    tell application "Finder"
      set aliasArgFilePath to ocidArgFilePathURL as alias
      set strPathKind to (kind of aliasArgFilePath) as text
    end tell
    ###/tmpのような特殊フォルダの場合は処理しない
    if strPathKind is "フォルダ" then
return "特殊ディレクトリかシンボリックリンクなので処理しません"
    else
      ###ファイルをゴミ箱に入れる
      set listResult to (appFileManager's trashItemAtURL:ocidArgFilePathURL resultingItemURL:(missing value) |error|:(reference))
    end if
  end if
return true
end doMoveToTrash

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom