File Paths

Any Text object (eg: string) can be treated as if it were a path to a file on disk, eg: ‘/path/to/file.txt’. Each of the folder and file names in the path is called a component. The following Text object commands all work on file paths:

Text Object Commands (path additions)

.filePathComponents v1.1

Returns an array of all components in the path, eg: the path ‘/path/to/file.txt’ would return (‘/’, ‘path’, ‘to’, ‘file.txt’).

.lastFilePathComponent v1.1

The name of the last component in the path, eg: the path ‘/path/to/file.txt’ would return ‘file.txt’.

.lastFilePathComponentWithoutExtension v1.1

The name of the last component in the path, with the extension removed, if it had one, eg: the path ‘/path/to/file.txt’ would return ‘file’.

.filePathExtension v1.1

The file extension of the last path component, if it had one, eg: the path ‘/path/to/file.txt’ would return ‘txt’.

.abbreviatedFilePath v1.1

The full path, but with the tilde character (“~”) used to abbreviate the user’s home folder, eg: the path ‘/Users/SeniorOso/file.txt’ would return ‘~/file.txt’.

.expandedFilePath v1.1

The full path, but with the tilde character (“~”) expanded to be the user’s home folder, eg: the path ‘~/file.txt’ would return ‘/Users/SeniorOso/file.txt’.

.filePathByAppendingComponent name v1.1

Returns a new file path that is formed by appending the given component to the already existing path. This method takes care of adding the separator as necessary.

.filePathByRemovingLastComponent v1.1

Returns a new path with the last component of the existing path removed, eg: the path ‘/path/to/file.txt’ would return ‘/path/to’.

.filePathByChangingLastComponent component v2.0

Returns a new file path that is formed by deleting the last file component (file extension and all), and then appending a new one (which may or may not have an extension).

.filePathByChangingLastComponentName name v2.0

Returns a new file path that is formed by replacing the existing last file component’s name with the one given, leaving the original file extension intact.

.filePathByAppendingExtension extension v1.1

Returns a new file path that is formed by appending the given file extension to the already existing path. This method takes care of adding the separator as necessary.

.filePathByAppendingComponentNameSuffix suffix v2.0

Returns a new file path that is formed by appending the given suffix to the end of the last component’s name (inserting the give suffix before the existing extension, if one exists). 

For example: can easily change “path/name.txt” to “path/name-suffix.txt”.

.filePathByChangingExtension extension v1.2

Returns a new file path that is formed by removing the existing file extension and then appending a the new one, eg:

$name = 'Idée Fixe.txt'

$name = $name.filePathByChangingExtension('rtf')

# $name is now 'Idée Fixe.rtf'

File Path Example

The following code saves a copy of the active document to the same folder as the original, but with a “*” in the file name:

# Make sure there is an active document that has been saved once

$doc = Document.active

$hasDoc = Defined $doc

If ! $hasDoc

Prompt "There is no active document to backup."

Exit

End

$path = $doc.filePath

$hasPath = Defined $path

If ! $hasPath

Prompt "Cannot backup an unsaved document."

Exit

End


# construct the path to backup to

$currentName = $path.lastFilePathComponentWithoutExtension

$extension = $path.filePathExtension

$backupName = "$currentName*.$extension"


$folder = $path.filePathByRemovingLastComponent

$backupPath = $folder.filePathByAppendingComponent($backupName)

# save the document

$isSaved = Save To $backupPath

If ! $isSaved

Prompt "Failed to backup the document!"

End


Previous Chapter
Files
<<  index  >>
 
Next Chapter
File Object