XFile Reference

XFile provides unified object oriented interface to file operation (moving, removing, geting attributes and so on).Functions of file operations of AppleScript world is given by Scripting Additions, Finder, System Events, shell commnads, AppleScriptObjC. It is cumbersome to find a proper way, because implemeted locations of functions are distributed.XFile wraps these components and introduces a file object for unified object oriented interface.

Example

use XFile : script "XFile"
use scripting additions

(* Make a New Instance *)
set a_xfile to XFile's make_with("/Users")
set home_folder to XFile's make_with(path to home folder)

(* Obtain File Infomation *)
log a_xfile's type_identifier() -- "public.folder"
log a_xfile's is_folder() -- true
log a_xfile's is_package() -- false
log a_xfile's item_name() -- "Users"

(* Obtain Parent and Child *)
log home_folder's parent_folder()'s posix_path()
-- "/Users"
log home_folder's child("Documents")'s posix_path()
-- "/Users/yourhome/Documents"
log home_folder's child("Library/Scripts")'s posix_path()
-- "/Users/yourhome/Library/Scripts"
log home_folder's unique_child("Documents")'s posix_path()
--"/Users/yourhome/Documents 2"

(* Read and Write *)
set test_file to home_folder's child("testfile")
test_file's write_as_utf8("new data")
log test_file's read_as_utf8() -- "new data"

(* File Manipulations *)
log test_file's item_exists() --true

set test_file2 to test_file's copy_to(home_folder's child("testfile2"))
log test_file2's posix_path() -- "/Users/yourhome/testfile2"

test_file2's move_to(home_folder's child("Documents"))
log test_file2's posix_path() -- "/Users/yourhome/Documents/testfile2"

test_file2's into_trash()
log test_file2's posix_path() -- "/Users/yourhome/.Trash/testfile2"
test_file's into_trash()

(* Working with Shell Commands *)
log test_file's perform_shell("cat %s") -- "new data"

Constractors

Making a new instance with a reference to a file from the class object.

make_with

Make a XFile instance with a file reference

make_with_pathinfo

Make a new instance with a PathInfo instance.

Make a New Instance with Changing Contents of an Instance

Making a new XFile instance with changing a part of the exisiting instance.

change_name

Make a new instance with changing file name of the instance

change_folder

Make a new instance with changing folder of the instance

change_path_extension

Make a new instance with changing path extension of the instance

Getting a File Reference form an Instance

Obtain a reference to a file which XFile reference to.

as_alias

Obtain a reference to a file as alias class

as_furl

Obtain a reference to a file as File URL class

hfs_path

Obtain HFS path

posix_path

Obtain POSIX path

normalized_posix_path

Obtain POSIX path which does not end with "/".

Working with Attributes

type_identifier

Obtain uniform type identifier (UTI) of the item

is_folder

Check whether the item is a folder or not.

is_package

Check whether the item is a package or not.

is_alias

Check whether the item is an alias file or not.

Check whether the item is a symbolic link or not.

is_executable

Check whether the operating system appears able to execute a specified file.

is_visible

Check whether the item is visible or not.

set_types

Set creator code and type code to the item.

info

Obtain file information.

info_with_size

Obtain file information including its size.

re_info

Obtain file information again.

Obtain Path Infomation

item_name

Obtain the name of the item referenced by the instance

basename

Obtain the base name (a name which does not include path extension) of the item referenced by the instance.

path_extension

Obtain path extension of the item referenced by the instance.

volume_name

Obtain the volume name of the item referenced by the instance.

Working with a Bundle

bundle_resource

Obtain an item which is in the bundle resource folder.

bundle_InfoPlist

Obtain a reference to Info.plist of the bundle.

File Manipulations

item_exists

Check whether the item referenced by the instance exists or not.

item_exists_without_update

Check whether the item referenced by the instance exists or not.

exists

Synonym of item_exists. Check whether the item referenced by the instance exists or not.

rename_to

Rename an item which referenced by the XFile instance.

copy_to

Copy the item to specified location

copy_with_replacing

Copy the item to specified location with replacing the destination.

copy_with_opts

Copy the item to specified location with options.

move_to

Move the item referenced by the instance to specified location.

move_with_replacing

Move the item referenced by the instance to specified location with replacing the destination.

resolve_alias

Resolving original item of a alias file.

into_trash

Put into trash.

remove

Remove the item referd from the target XFile instance.

Making subfolders

make_folder

Make a sub folder.

make_path

Make folders which indicating the path of the XFile instance.

Reading and Writing File Contents

read_as_utf8

Read file contents as UTF-8 encoded text.

write_as_utf8

Write data into the file as UTF-8 encoded text.

Parent and Children

parent_folder

Obtain a folder containing the item referenced by the XFile instance.

child

Obtain an item in the folder with specifying a sub path from the target.

unique_child

Obtain a XFile instance reference unique name item in the folder.

each

Call do handler of given script object with each item in the folder as an argument.

Working with Shell Commands

perform_shell

Run passed shell command taking the target as an argument.

shell_test

Run test command taking the target as an argument.