2012年11月29日木曜日

【AppleScript】 指定した階層数分上がった階層を返す(UNIX/POSIX両用)

指定した階層数を上がった階層を返すハンドラです。
もっと簡単に書けそうだけど、取り急ぎ必要だったから気にしなかった&結局他の方法をとったから無駄になったので、半ば腹いせです。

UNIX PATHとPOSIX PATH、どちらも返します。
「/」もしくは「:」で区切られたパスならなんでもOK。
ただ、返される値はテキストなので注意。(個人的にはそのほうが使いやすい)

今思うと、URLでも使えますね。それなら少しは使いまわせそうかな・・・

set thePath to path to me
set upCount to 1 --上がりたい階層数を指定
set upPath to my ClassUp(thePath, upCount)

on ClassUp(thePath, upCount)
if thePath contains "/" then
set newDel to "/"
else
set newDel to ":"
end if
set thePath to thePath as text
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to newDel
set myList to text items of thePath
if last item of myList = "" then
set upCount to upCount + 1
end if
set myList to items 1 thru ((count of myList) - upCount) of myList
set myText to myList as string
set AppleScript's text item delimiters to oldDel
return myText & newDel
end ClassUp

0 件のコメント:

コメントを投稿