Shell functions

(*
This is a library of functions that use bash shell scripts to accomplish 
tasks as fast as possible.  Most of these functions can be used by themselves 
or be combined with others.  
The reason many of them have the runBool parameter (where you can decide
whether or not to have the function run the shell script inside it) is for situations
where you may want to concatenate the functions into a single shell script.
Running them together as a single script is faster than running them 
separately, one after the other.  For this situation, set runBool to false.
When runBool is set to true, this means the function is intended
to run by itself.
*)



--If runBool is true, this function runs the shell script and returns
--theFile's contents as a string.
--If false, it only returns a shell string following this format: "cat filename" .
on fileContents(theFile, runBool) --theFile must be a colon-delimited path string. 
	--get file name:
	set theName to itemName(theFile)
	set shellString to (itemLocation(theFile) & " cat " & (quoted form of theName) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end fileContents


--Reads a file and returns only the unique lines.
on uniqueLines(theFile, runBool)
	set shellString to (fileContents(theFile, false) & "| sort | uniq \n ")
	if runBool is false then return shellString
	do shell script shellString
end uniqueLines


--Obviously this writes to a file.  if overwriteBool is true, theFile's contents will be
--erased by theString.  If false, theString will be added to the end of theFile's contents.
--If runBool is true, the function will run the shell script.
--If false, the function will only return a shell script string.
on writeInFile(theFile, theString, overwriteBool, runBool)
	if overwriteBool is true then
		set writeMethod to " > "
	else
		set writeMethod to " >> "
	end if
	set shellString to (itemLocation(theItem) & "echo " & (quoted form of theString) & ¬
		writeMethod & (quoted form of itemName(theItem)) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end writeInFile



--theItem must be a colon-delimited path string. 
--Example: "Macintosh HD:Users:Username:Desktop:Filename.txt"
--Returns theItem's name (without the full path) as string.
on itemName(theItem)
	set theParts to explode(theItem, ":")
	if theParts ends with "" then set theParts to LI({1, -2}, theParts)
	return LI(count of theParts, theParts) as text
end itemName


--Returns a shell string changing the working directory to the one 
--containing theItem.  theItem must be a colon-delimited path string. 
--Example: "Macintosh HD:Users:Username:Desktop:Filename.txt"
on itemLocation(theItem)
	set theItem to POSIX path of theItem
	if theItem ends with "/" then set theItem to (characters 1 thru -2 of theItem)
	set theParts to explode(theItem, "/")
	set theDirectory to (implode(LI({1, -2}, theParts), "/"))
	return "cd " & quoted form of theDirectory & " \n "
end itemLocation



--theItem and newLocationName must both be colon-delimited path strings.
on moveItem(theItem, newLocationName, runBool)
	set newLocationName to POSIX path of newLocationName
	if newLocationName ends with "/" then ¬
		set newLocationName to (characters 1 thru -2 of newLocationName)
	set theName to itemName(theItem)
	set shellString to (itemLocation(theItem) & "mv " & (quoted form of theName) & ¬
		" " & (quoted form of (newLocationName)) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end moveItem


--theItem must be colon-delimited path string.
on deleteItem(theItem, runBool)
	set theItem to POSIX path of theItem
	set shellString to ("rm " & (quoted form of theItem) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end deleteItem


--theItem and newLocationName must both be colon-delimited path strings.
on copyItem(theItem, newLocationName, runBool)
	set theName to itemName(theItem)
	set theItem to POSIX path of theItem
	set newLocationName to POSIX path of newLocationName
	set shellString to ("cp " & (quoted form of theItem) & ¬
		" " & (quoted form of newLocationName) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end copyItem


--theItem must be colon-delimited path to theItem.  newName is just the name without the path.
on renameItem(theItem, newName, runBool)
	set theName to itemName(theItem)
	set shellString to (itemLocation(theItem) & "mv " & (quoted form of theName) & " " & ¬
		(quoted form of newName) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end renameItem


--newFolderPath must be entire string path of the folder you want to make.
on makeFolder(newFolderPath, runBool)
	set newFolderPath to POSIX path of newFolderPath
	set shellString to ("mkdir " & (quoted form of newFolderPath) & " ")
	if runBool is false then return shellString
	do shell script shellString
end makeFolder


--fileName must be the item name without the full path.  saveLocation must be a path.
--fileType can be "jpg", "png", or "tiff".  The default is "jpg".
on takeScreenshot(fileName, fileType, saveLocation, runBool)
	if (fileType is false) or (fileType is "") then set fileType to "jpg"
	set saveLocation to POSIX path of saveLocation
	if saveLocation does not end with "/" then set saveLocation to (saveLocation & "/")
	set shellString to ("screencapture -t " & fileType & " " & (quoted form of (saveLocation & ¬
		fileName & "." & fileType)) & " \n ")
	if runBool is false then return shellString
	do shell script shellString
end takeScreenshot


--Using perl, this searches for a searchString inside oldFile and replaces it
--with replaceString, writing all the new changes to newFile.
on perlSubstitution(searchString, replaceString, oldFile, newFile, caseBool, runBool)
	set {oldFileName, newFileName, perlCase} to {itemName(oldFile), itemName(newFile), "i"}
	if caseBool is true then set perlCase to ""
	set shellString to itemLocation(oldFile) & "perl -pe " & (quoted form of ("s/" & ¬
		searchString & "/" & replaceString & "/" & perlCase & "g")) & " " & ¬
		(quoted form of oldFileName) & " > " & (quoted form of newFileName) & " \n "
	if runBool is false then return shellString
	do shell script shellString
end perlSubstitution



-- This function separates pieces of a string into list items, using theDelimit
-- as the separator:
on explode(theString, theDelimit)
	set origDelimit to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimit
	set theResult to every text item of theString
	set AppleScript's text item delimiters to origDelimit
	return theResult
end explode

--This function re-assembles a list of strings into a single string,
--using theDelimit as glue to reconnect each string.
on implode(textList, theDelimit)
	set origDelimit to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimit
	set theString to (textList as string)
	set AppleScript's text item delimiters to origDelimit
	return theString
end implode


--This function is just for creating a short-hand way of accessing a list item.
--ItemNum can be a single integer, or a list of two integers for accessing a range of items:
on LI(itemNum, theList)
	if class of itemNum is integer then
		return (item itemNum of theList)
	else if class of itemNum is list then
		return (items (item 1 of itemNum as integer) thru ¬
			(item 2 of itemNum as integer) of theList)
	end if
end LI

--This function is for assigning a value to a list item:
on setLI(itemNum, theList, theValue)
	set item itemNum of theList to theValue
end setLI


Model: late 2009 mac mini
AppleScript: 2.3.2
Browser: Safari 537.86.3
Operating System: Mac OS X (10.9)