(*

FOLDER OBJECT 1.0.1


A wrapper script object for encapsulating folder-oriented properties and behaviours.

© 1996 Phil Hudson. May be freely used and distributed. Comments and bug reports please to: Released 22 Oct 1996.

Revision history:

1.0:
Initial release.
21 Oct 96.
1.0.1:
Fixed bug in setFolder() which incorrectly prevented assignment of a valid alias.
22 Oct 96.

*)
-- Running this script initializes and returns an "intelligent folder" script object.
-- Pass an alias to a folder, or a folder's pathname.

on run (fldrAlias) -- to create a folder script object, pass in an alias to a folder.
	-- Passing either a reference to a folder or a folder's pathname (string) will also work
	
	--
	-- Script object provides common attributes and behaviours for interacting with file
	--   system folders.
	--
	-- Define a script object which encapsulates the following attributes:
	--   * The folder [alias]
	-- and the following behaviours:
	--   * Initialisation, taking the alias or the pathname of the folder. [setFolder(aFldr)]
	--   * Listing of folder contents. [listFldr given invisFlag -> list]
	--   * Folder existence testing. [fldrExists() -> boolean]
	--   * Initialisation with user selection of folder. [chooseFldr given aPrompt]
	--   * Determination of path to folder, pathname of folder, name of folder,
	--     name of volume containing folder. [path()/pathName()/fldrName()/diskName(), all -> string]
	--   * Creation of an alias object pointing to any file or folder within the folder's top level.
	--     [fileAlias given fName -> alias]
	--   * Moving or copying a file into the folder, with or without replacing of
	--     identically-named files. [moveFileInto/copyFileInto given fAlias, repFlag -> int]
	--
	script newFolderObj
		
		property fldrAlias : "" -- alias to the folder
		
		
		on wakeUp() -- utility routine
			activate of me
			beep
		end wakeUp
		
		
		on stopAlrt(promptStr) -- utility routine
			wakeUp()
			display dialog promptStr buttons OK default button "OK" with icon stop
		end stopAlrt
		
		
		on listFldr given invisibles:invisFlag
			list folder my fldrAlias invisibles invisFlag
		end listFldr
		
		
		on fldrExists()
			tell application "Finder" to return (my fldrAlias exists)
		end fldrExists
		
		
		on setFolder(aFldr) -- massage argument into a valid alias if possible, then assign to script property.
			if ((class of aFldr) = reference) then set aFldr to (contents of aFldr)
			try
				if ((class of aFldr) = list) then
					if ((length of aFldr) = 1) then
						set aFldr to (item 1 of aFldr)
					else
						error "Input is a list, not a folder."
					end if
				end if
			
				if (((class of aFldr) = string) or ((class of aFldr) = folder) or ((class of aFldr) = text)) then
					set aFldr to (alias aFldr)
				else if ((class of aFldr) = alias) then
					set fldr to (alias (aFldr as string)) -- is a compiled alias still valid?
				else
					error "Input is not a folder."
				end if
				set my fldrAlias to aFldr
			on error whatever
				my stopAlrt("setFolder: " & whatever)
			end try
		end setFolder
		
		
		on chooseFldr given prompt:aPrompt -- user selection of folder to encapsulate
			my setFolder(choose folder with prompt aPrompt)
		end chooseFldr
		
		
		on path() -- the disk and/or folder names of this folder's container (excluding this folder)
			set pName to my pathName()
			set colOff to (offset of ":" in ((reverse of (characters of pName)) as string))
			if (colOff = 0) then return "" -- folder=volume
			return (text from beginning to -colOff of pName)
		end path
		
		
		on pathName() -- this folder's full pathname string (including this folder)
			return (my fldrAlias as text)
		end pathName
		
		
		on fldrName() -- this folder's short name
			return (name of (info for my fldrAlias))
		end fldrName
		
		
		on diskName() -- name of volume containing this folder
			set pName to my pathName()
			return (text 1 thru (offset of ":" in pName) of pName)
		end diskName
		
		
		on fileAlias given fileName:fName -- return an alias to a file in this folder
			set pName to (my pathName())
			tell application "Finder" to set fExist to (alias (pName & fName)) exists
			if fExist then return (alias (pName & fName))
		end fileAlias
		
		
		on moveFileInto given fileAlias:fAlias, replacing:repFlag -- returns file system error number or 0
			--**
			--
			-- NB: when calling, include true around alias
			--
			-- e.g.: moveFileInto of my fldrObj without replacing given fileAlias:[Macro error: Can't evaluate the expression because the name "myFileAlias" hasn't been defined.]

			--
			--**
			try
				tell application "Finder" to move fAlias to my fldrAlias replacing repFlag
				return 0
			on error number errNum
				return errNum
			end try
		end moveFileInto
		
		
		on copyFileInto given fileAlias:fAlias, replacing:repFlag -- returns file system error number or 0
			try
				tell application "Finder" to duplicate fAlias to my fldrAlias replacing repFlag
				return 0
			on error number errNum
				return errNum
			end try
		end copyFileInto
		
	end script
	
	newFolderObj's setFolder(fldrAlias)
	return newFolderObj
	
end run



(*

······· EXAMPLES ·······

--   Initialize a script property to a script object which returns a new
--   "intelligent folder" script object when run:

property folderMaker : alias (((path to extensions folder) as text) & "Scripting Additions:Folder object")


--   Create and store a new folder object:

property inFolder : ""
property IFalias : "My HD:Consultancy work:"
if ((class of my inFolder) is not script) then set my inFolder to (run script folderMaker with parameters my IFalias)


--  Check a folder's validity:

on locateFolders()
	if not my inFolder's fldrExists() then chooseFldr of (my inFolder) given prompt:"Incoming files:"
end locateFolders



-- Enclosed file aliasing example:

property fAlias : "" -- alias to file in requests folder
property msgText : "" -- body text of request message

on readIncomingFile(fName)
	set my fAlias to (fileAlias of (my inFolder) given fileName:fName)
	set fNum to (open for access my fAlias without write permission)
	set my msgText to (read fNum)
	close access fNum
end readIncomingFile
	


*)



Top of page
Cool IT in Africa Site design by Phil Hudson. Comments? Email phil.hudson@iname.com.
Top of page