AppleScript Library

Files & Folders Library

   Reading/writing data, renaming files, color labels of files and folders, getting information on files and folders.

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
global variables
General
CheckExistence
getParent
getKBSize
getColorLabel
setColorLabel
Files
isBusy
Filename
prefixToFileName
suffixToFileName
replaceInFileName
Read
readFile
readList
readUTF8
readWinFile
Write
writeToFile
writeListToFile
writeUTF8ToFile
writeWinFile

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_file"

global variables

Dependencies on other libraries (some handlers): string

show source AppleScript-Editor open insert append toc
global _string

General

CheckExistence(FileOrFolderToCheckString)

Check if a file or folder exists.

FileOrFolderToCheckStringstringmac-like file or folder path
booleantrue if exists

Written by:found somewhere on the internet

Example:

CheckExistence("Mac:Users:usrName:Desktop:test.txt") 
--> true or false
show source AppleScript Editor open insert append toc
on CheckExistence(FileOrFolderToCheckString)
	try
		alias FileOrFolderToCheckString
		return true
	on error
		return false
	end try
end CheckExistence

getParent(anAlias)

Get parent folder of a file or folder.

anAliasaliasfile or folder
aliasparent folder

Written by:Skeeve (found on hohabadu.de)

Example:

getParent((((path to desktop) as string) & "test.txt") as alias) 
--> alias "Mac:Users:usrName:Desktop:"
show source AppleScript Editor open insert append toc
on getParent(anAlias)
	try
		return POSIX file ((POSIX path of anAlias) & "/..") as alias
	on error eMsg number eNum
		error "Can't getParent: " & eMsg number eNum
	end try
end getParent

getKBSize(posixPath)

Get file or folder size in KB.

posixPathstringfile or folder posix path
integersize in kilobyte

Man pages:  du, awk

Written by:ljr

Example:

getKBSize(POSIX path of (path to desktop)) 
--> 399068
show source AppleScript Editor open insert append toc
on getKBSize(posixPath)
	local posixPath, sizeFolder
	try
		try
			POSIX file posixPath as alias
		on error
			error "The file \"" & posixPath & "\" doesn't exist."
		end try
		set posixPath to quoted form of posixPath
		set sizeFolder to ¬
			(do shell script "du -sk " & posixPath & " | awk '{print $1}' ")
		return sizeFolder as integer
	on error eMsg number eNum
		error "Can't getKBSize: " & eMsg number eNum
	end try
end getKBSize

getColorLabel(theFile)

Return label index (0-7) representing the current color label of a file/folder.

theFilealias or stringfile or folder (Mac-like path)
integersee definition in 'setColorLabel'

Written by:ljr

Example:

getColorLabel(path to desktop) 
--> 0
show source AppleScript Editor open insert append toc
on getColorLabel(theFile)
	local theFile, res
	try
		set theFile to theFile as alias
		tell application "Finder" to set res to label index of theFile
		return res
	on error eMsg number eNum
		error "Can't getColorLabel: " & eMsg number eNum
	end try
end getColorLabel

setColorLabel(theFile, theColor)

Set the color label of a file/folder. (0 = none, 1 = orange, 2 = red, 3 = yellow, 4 = blue, 5 = purple, 6 = green, 7 = gray)

theFilealias or stringfile or folder (Mac-like path)
theColorinteger or stringsee description or source
nothingerror if file doesn't exist or unknown color

Written by:ljr

Example:

setColorLabel(path to desktop, 1) 
--> nothing
show source AppleScript Editor open insert append toc
on setColorLabel(theFile, theColor)
	local theFile, theColor
	try
		set theFile to theFile as alias
		ignoring case
			if theColor is 0 or theColor is in {"no color", "none"} then
				set theColor to 0
			else if theColor is 1 or theColor is "orange" then
				set theColor to 1
			else if theColor is 2 or theColor is "red" then
				set theColor to 2
			else if theColor is 3 or theColor is "yellow" then
				set theColor to 3
			else if theColor is 4 or theColor is "blue" then
				set theColor to 4
			else if theColor is 5 or theColor is "purple" then
				set theColor to 5
			else if theColor is 6 or theColor is "green" then
				set theColor to 6
			else if theColor is 7 or theColor is in {"grey", "gray"} then
				set theColor to 7
			end if
		end ignoring
		tell application "Finder" to set label index of theFile to theColor
	on error eMsg number eNum
		error "Can't setColorLabel: " & eMsg number eNum
	end try
end setColorLabel

Files

isBusy(aFileAlias)

Check if a file is being busy.

aFileAliasaliasfile (mac-like path as string is also possible)
booleantrue if busy

Man pages:  lsof

Written by:ShooTerKo (found on hohabadu.de)

Example:

isBusy(((path to desktop) as string) & "test.txt") 
--> false
show source AppleScript Editor open insert append toc
on isBusy(aFileAlias)
	try
		-- result of lsof is a list of opened files (--> see also "man lsof" in terminal) 
		do shell script "lsof " & quoted form of (POSIX path of aFileAlias)
		return true -- if busy
	end try
	return false -- if idle
end isBusy

Filename

prefixToFileName(posixPath, prefix)

Add prefix to a file name (doesn't rename the file yet, use e.g. 'mv').

posixPathposix path
prefixtextthe prefix to add
text

Dependencies: _string's rightStringFromRight

Written by:ljr

Example:

prefixToFileName("~/test.pdf", "new_") 
--> "~/new_test.pdf"
show source AppleScript Editor open insert append toc
on prefixToFileName(posixPath, prefix)
	local posixPath, prefix, fn
	try
		set fn to _string's rightStringFromRight(posixPath, "/")
		set len to count fn
		set posixPath to posixPath's text 1 thru -(len + 1)
		return posixPath & prefix & fn
	on error eMsg number eNum
		error "Can't prefixToFileName: " & eMsg number eNum
	end try
end prefixToFileName

suffixToFileName(posixPath, suffix)

Add suffix to a file name (doesn't rename the file yet, use e.g. 'mv').

posixPathposix path
suffixtextthe suffix to add
text

Dependencies: _string's rightStringFromRight

Written by:ljr

Example:

suffixToFileName("~/test.pdf", "_old") 
--> "~/test_old.pdf"
show source AppleScript Editor open insert append toc
on suffixToFileName(posixPath, suffix)
	local posixPath, suffix, ext, fn
	try
		set fn to _string's rightStringFromRight(posixPath, "/")
		set ext to ""
		set len to 0
		if fn contains "." then
			set ext to "." & _string's rightStringFromRight(fn, ".")
			set len to count ext
		end if
		set posixPath to posixPath's text 1 thru -(len + 1)
		return posixPath & suffix & ext
	on error eMsg number eNum
		error "Can't suffixToFileName: " & eMsg number eNum
	end try
end suffixToFileName

replaceInFileName(posixPath, oldStr, newStr)

Replace text in a file name (doesn't rename the file yet, use e.g. 'mv').

posixPathposix path
oldStrtextstring to replace
newStrtextreplacement string
text

Dependencies: _string's rightStringFromRight, _string's replaceString, _string's explode, _string's implode

Written by:ljr

Example:

replaceInFileName("~/test_old.pdf", "_old", "_new") 
--> "~/test_new.pdf"
show source AppleScript Editor open insert append toc
on replaceInFileName(posixPath, oldStr, newStr)
	local posixPath, oldStr, newStr, fn, ext
	try
		set fn to _string's rightStringFromRight(posixPath, "/")
		set len to count fn
		set ext to ""
		if fn contains "." then
			set fn to _string's explode(".", fn)
			set ext to fn's item -1
			set fn to _string's implode(".", fn's items 1 thru -2)
			set ext to "." & ext
		end if
		set fn to _string's replaceString(fn, oldStr, newStr)
		set posixPath to posixPath's text 1 thru -(len + 1)
		return posixPath & fn & ext
	on error eMsg number eNum
		error "Can't replaceInFileName: " & eMsg number eNum
	end try
end replaceInFileName

Read

readFile(theFile)

Read data from a file.

theFilestring or alias
text

Written by:ljr

Example:

readFile((path to desktop as string) & "test.txt") 
--> "test - test"
show source AppleScript Editor open insert append toc
on readFile(theFile)
	local theFile, theText
	try
		set theFile to theFile as alias
		set theText to read theFile
		return theText
	on error eMsg number eNum
		error "Can't readFile: " & eMsg number eNum
	end try
end readFile

readList(theFile)

Read an AppleScript list from a file.

theFilestring or alias
list

Written by:ljr

Example:

readList((path to desktop as string) & "test.list") 
--> {1, 2, 3}
show source AppleScript Editor open insert append toc
on readList(theFile)
	local theFile, theList
	try
		set theFile to theFile as alias
		set theList to read theFile as list
		return theList
	on error eMsg number eNum
		error "Can't readList: " & eMsg number eNum
	end try
end readList

readUTF8(theFile)

Read text from a file using utf-8 encoding.

theFilestring or alias
string

Written by:ljr

Example:

readUTF8((path to desktop as string) & "test.txt") 
--> "test-test"
show source AppleScript Editor open insert append toc
on readUTF8(theFile)
	local theFile, theText
	try
		set theFile to theFile as alias
		set theText to read theFile as «class utf8»
		return theText
	on error eMsg number eNum
		error "Can't readUTF8: " & eMsg number eNum
	end try
end readUTF8

readWinFile(theFile)

Read text from a file using ISO Latin 1 encoding (MS Windows).

theFilemac-like string or alias
string

Man pages:  iconv

Written by:ljr

Example:

readWinFile((path to desktop as string) & "test.csv") 
--> "test;test2;test3"
show source AppleScript Editor open insert append toc
on readWinFile(theFile)
	local theFile, theText
	try
		set theFile to theFile as alias
		set theFile to quoted form of (POSIX path of theFile)
		set theText to (do shell script "iconv -f L1 -t UTF8 " & theFile)
		return theText
	on error eMsg number eNum
		error "Can't readWinFile: " & eMsg number eNum
	end try
end readWinFile

Write

writeToFile(theText, theFile, boolAppend)

Write text to a file.

theTextstring
theFilestring or alias
boolAppendbooleanappend?
nothingraises error if problems

Written by:ljr

Example:

writeToFile("test - test", (path to desktop as string) & "test.txt", false) 
--> error if problems
show source AppleScript Editor open insert append toc
on writeToFile(theText, theFile, boolAppend)
	local theText, theFile, boolAppend, otf
	try
		set theFile to theFile as string
		set otf to open for access file theFile with write permission
		if boolAppend is false then set eof of otf to 0
		write theText to otf starting at eof
		close access otf
	on error eMsg number eNum
		try
			close access file otf
		end try
		error "Can't writeToFile: " & eMsg number eNum
	end try
end writeToFile

writeListToFile(theList, theFile)

Write an AppleScript list to a file.

theListlist
theFilestring or alias
nothingraises error if problems

Written by:ljr

Example:

writeListToFile({1, 2, 3}, (path to desktop as text) & "test.list") 
--> error if problems
show source AppleScript Editor open insert append toc
on writeListToFile(theList, theFile)
	local theList, theFile, otf
	try
		set theFile to theFile as text
		set otf to open for access file theFile with write permission
		set eof of otf to 0
		write theList to otf starting at eof as list
		close access otf
	on error eMsg number eNum
		try
			close access otf
		end try
		error "Can't writeListToFile: " & eMsg number eNum
	end try
end writeListToFile

writeUTF8ToFile(theText, theFile, boolAppend)

Write text to a file using utf-8 encoding.

theTexttext
theFilestring or alias
boolAppendbooleanappend?
nothingraises error if problems

Written by:ljr

Example:

writeUTF8ToFile("test-test", (path to desktop as string) & "test.txt", true) 
--> error if problems
show source AppleScript Editor open insert append toc
on writeUTF8ToFile(theText, theFile, boolAppend)
	local theText, theFile, boolAppend, otf
	try
		set theFile to theFile as string
		set otf to open for access theFile with write permission
		if boolAppend is false then set eof of otf to 0
		write (theText) to otf starting at eof as «class utf8»
		close access otf
	on error eMsg number eNum
		try
			close access otf
		end try
		error "Can't writeUTF8ToFile: " & eMsg number eNum
	end try
end writeUTF8ToFile

writeWinFile(theText, theFile, boolAppend)

Write text to a file using ISO Latin-1 encoding and CRLF line endings (MS Windows). A CRLF is automatically appended to the output.

theTexttext
theFilestring or alias
boolAppendbooleanappend?
nothingraises error if problems

Man pages:  echo, iconv

Written by:ljr

Example:

writeWinFile("ÄÖÜ\räöüß", (path to desktop as string) & "test.txt", false) 
--> error if problems
show source AppleScript Editor open insert append toc
on writeWinFile(theText, theFile, boolAppend)
	local theText, theFile, ASTID, boolAppend, reDir
	set ASTID to AppleScript's text item delimiters
	try
		set theText to paragraphs of theText
		set AppleScript's text item delimiters to "\r\n"
		set theText to (theText as text) & "\r" -- \n added by shell
		set AppleScript's text item delimiters to ASTID
		set reDir to ">"
		if boolAppend then set reDir to ">>"
		set theFile to quoted form of (POSIX path of theFile)
		set theText to quoted form of theText
		do shell script "/bin/bash -c " & quoted form of ("echo " & theText) & ¬
			"|iconv -f UTF8 -t L1" & reDir & theFile
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't writeWinFile: " & eMsg number eNum
	end try
end writeWinFile

Copyright

© 2008 ljr (http://applescript.bratis-lover.net)
getKBSize, readFile, readList, readUTF8, writeToFile, writeListToFile, writeUTF8ToFile, getColorLabel, setColorLabel, replaceInFileName, suffixToFileName, prefixToFileName

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


© 2008 Skeeve, ShooTerKo (found on http://hohabadu.de/?APPLESCRIPT)
getParent, isBusy

Please refer to the authors' websites for copyright information.


© ???? Unknown
CheckExistence