PathConverter Reference

PathConverter

What ?

PathConverter is an AppleScript module to convert a path between a relative path and an absolute path each other.

An absolute path means a file path which describes complete location from a top level of a disk as follows.

Macintosh HD:Users:hello:document1.text

To talk about a relative path, consider one more absolute path.

Macintosh HD:Users:hey:document2.text

When the origin of the path is 'document1.text', the location of 'document2.text' can be expressed relatively as follows.

::hey:document2.text

This is a relative path.

Paths in above examples are HFS styles. PathConverter can also deal with POSIX styles as follows.

/Users/hello/document1.text

../hey/document2.text

Usage

The standard usage of PathConverter is :

  1. At first, make an instance of PathConverter with make_with passing a base path into the argument. The style (HFS or POSIX) of path which the instance can deal with is determined with the one of the base path.
  2. Convert paths between a relative path and an absolute path using relative_path and absolute_path handles of the instance.
use PathConverter : script "PathConverter"

-- HFS Path
set pathconv to PathConverter's make_with("Macintosh HD:Users:tkurita:Documents:hey.txt")
log (relative_path of pathconv for "Macintosh HD:Users:tkurita:articles:hello.txt")
(* ::articles:hello.txt *)

log (absolute_path of pathconv for "::hello.txt")
(* Macintosh HD:Users:tkurita:hello.txt *)

-- POSIX path
set pathconv to PathConverter's make_with("/Users/tkurita/Documents/hey.txt")
log (relative_path of pathconv for "/Users/tkurita/articles/hello.txt")
(* ../articles/hello.txt *)

log (relative_path of pathconv for "/Users/tkurita/Documents/")
(*./*)

log (absolute_path of pathconv for "../articles/hello.txt")
(* /Users/tkurita/articles/hello.txt *)

An extra feature of PathConverter is converting a POSIX path into a HFS path with only parsing paths. Mainly it is supposed conversion of a relative path between POSIX style and HFS style.

In the case of absolute paths, conversion each other is easy using 'POSIX file' and 'POSIX path of' commands. On the other hand, direct conversion of relative paths is not straightforward. hfs_from_posix handler can help to solve such problems.

use PathConverter : script "PathConverter"

tell PathConverter
log (hfs_from_posix("../articles/hello.txt"))
(* ::articles:hello.txt *)
end tell
PathConverter Reference