20260510

[AppleScript] リンク切れになった『エイリアス』『シンボリックリンク』を探す

[AppleScript] リンク切れになった『エイリアス』『シンボリックリンク』を探す

NOTE記事一覧ですnote.com

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

エイリアスのリンク切れを探す.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006エイリアス・シンボリックリンクの参照先を解決できない
007孤立してしまったファイルを探します。
008クラッシックなAs記法
009
010判定に『エラー』を使っているので
011あまり良い方法とは言えないけど
012判定の材料としては利用可能
013
014
015
016com.cocolog-nifty.quicktimer.icefloe *)
017----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
018use AppleScript version "2.8"
019use scripting additions
020
021set dateNow to current date
022
023
024set listBrokenLink to {} as list
025
026tell application "SystemUIServer"
027   activate
028   set aliasChooleDirPath to (choose folder without multiple selections allowed) as alias
029end tell
030
031
032tell application "Finder"
033   set listAliasFilePath to (every alias file of (entire contents of aliasChooleDirPath)) as alias list
034end tell
035
036#Count alias list
037set numCntList to (count of listAliasFilePath) as integer
038
039#roop list
040repeat with itemNo from 1 to (numCntList) by 1
041   
042   #get alias path
043   try
044      set aliasItemFilePath to (item itemNo of listAliasFilePath) as alias
045      #Alias info
046      set recordInfoFor to (info for aliasItemFilePath) as record
047      #IS ALIAS
048      set boolIsAlias to (alias of recordInfoFor) as boolean
049      #
050      if boolIsAlias is true then
051         tell application "Finder"
052            tell file aliasItemFilePath
053               set recordFileProperties to (properties of aliasItemFilePath) as record
054               set refOrgFile to (original item of recordFileProperties)
055            end tell
056         end tell
057         #オリジナルが探せない場合
058         #alias file Broken
059         if refOrgFile = (missing value) then
060            set end of listBrokenLink to (POSIX path of aliasItemFilePath)
061         end if
062         
063         #種類別のリストにしたい場合
064         set strFileType to (file type of recordFileProperties) as text
065         #シンボリックリンクの場合
066         if strFileType is "fdrp" then
067            --> Sym link or Hard Link
068            
069            #エイリアスの場合
070         else if strFileType is "alis" then
071            -->Alias
072         end if
073      end if
074      
075   on error
076      #シンボリックリンクの場合、解決不能だとas aliasでエラーになるのを利用
077      #In the case of symbolic links, if it cannot be solved, it will cause an error in as alias.
078      #Sym link Broken
079      set end of listBrokenLink to (POSIX path of (item itemNo of listAliasFilePath))
080   end try
081   
082end repeat
083
084
085set dateDone to current date
086
087log dateDone - dateNow
088log (count of listBrokenLink)
089return listBrokenLink
AppleScriptで生成しました