【Apple Script】パス文字列からディレクトリ名、ファイル名、拡張子を取得する

Apple Script から shell コマンドを実行して、

パス文字列からディレクトリ名、ファイル名、拡張子を取得する関数を作った。


on dir(args)
    set {path_:aPath} to ¬
        args & {path_:""}

    return do shell script "
        fpath=" & quoted form of POSIX path of aPath & "
        fdir=\"${fpath%/*}\"
        echo $fdir
        "
end dir

on fileext(args)
    set {path_:aPath} to ¬
        args & {path_:""}

    return do shell script "
        fpath=" & quoted form of POSIX path of aPath & "
        fext=\"${fpath##*.}\"
        echo $fext
        "
end fileext

on filename(args)
    set {path_:aPath, isExt_:aIsExt} to ¬
        args & {path_:"", isExt_:true}

    if aIsExt then
        return do shell script "
        fpath=" & quoted form of POSIX path of aPath & "
        fanameext=\"${fpath##*/}\"
        echo $fanameext
    "
    else
        return do shell script "
        fpath=" & quoted form of POSIX path of aPath & "
        fanameext=\"${fpath##*/}\"
        fname=\"${fanameext%.*}\"
        echo $fname
    "
    end if

end filename

set file_name to my filename({path_:path1, isExt_:false})
set file_name_ext to my filename({path_:path1, isExt_:true})
set ext_name to my fileext({path_:path1})
set dir_name to my dir({path_:path1})

1年前にもやってたけど、やっぱり忘れてる。

参考サイト