指定のPOSIX pathを元に相対パスを計算するAppleScriptです。
Unix shellであれば、相対パスについては「../../../」などと表現できるわけですが、シェルを介さずに計算する方法が見つからなかったので、必要に迫られて作っておきました。
AppleScript名:上位パスを相対的に求める.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/06/14 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set aFile to "/Users/me/Documents/1/2/3" set aNum to 1 set bFile to getUpperDir(aFile, aNum) of me –> "/Users/me/Documents/1/2" set aNum to 2 set cFile to getUpperDir(aFile, aNum) of me –>"/Users/me/Documents/1" set aNum to 3 set dFile to getUpperDir(aFile, aNum) of me –> "/Users/me/Documents" –与えられたパスから指定階層「上」のパスを求める on getUpperDir(aPOSIXpath as string, aNum as integer) if aNum < 1 then return aPOSIXpath set pathString to current application’s NSString’s stringWithString:aPOSIXpath repeat aNum times set pathString to pathString’s stringByDeletingLastPathComponent() end repeat return pathString as string end getUpperDir |
AppleScript名:下位パスを配列で追加して求める.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/06/14 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set aPOSIXpath to "/Users/me/Documents/" –AppleScriptのPOSIX path表現(フォルダを表現するさい、末尾に「/」) set catDirList to {"1", "2", "3"} set bPath to getChildPathByAppendingDirsByArray(aPOSIXpath, catDirList) of me –> /Users/me/Documents/1/2/3 set aPOSIXpath to "/Users/me/Documents" –CocoaのPOSIX path表現(フォルダを表現する場合でも、末尾に「/」入らず) set catDirList to {"3", "2", "1"} set cPath to getChildPathByAppendingDirsByArray(aPOSIXpath, catDirList) of me –> "/Users/me/Documents/3/2/1" on getChildPathByAppendingDirsByArray(aPOSIXpath, catDirList) set pathString to current application’s NSString’s stringWithString:aPOSIXpath set aList to (pathString’s pathComponents()) as list set aList to aList & catDirList set bPath to current application’s NSString’s pathWithComponents:aList return bPath as string end getChildPathByAppendingDirsByArray |
More from my site
(Visited 127 times, 1 visits today)