Hello.
Remember we talked about reading and writing lists from and to disk?
I’m happy to announce that it now does so.
The downside is that since you are using Tiger, you have to download and install Smile in order to use it.
As a matter of fact, as long as you get your hands on the Satimage.osax for Tiger and put it in the ScriptingAdditons folder. (Meaning you can ditch the rest of the installation.
Maybe you must adjust the path to checkModifierKeys, then you should have the possibility to edit lists, and read them in and write them out to disk.
When you switch to Leopard/SnowLeopard you will experience that your lists are fully compatible there, as they are saved with utf-8 encoding.
I hope you will try it, and reports “every aspect” of how it was running, speed etc. 
Best Regards
McUsr
-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
” macscripter.net/viewtopic.php?pid=130529#p130529
(*
TERMS OF USE.
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission.
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.
Credit for having contributed to your product would in all cases be nice!
If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.
The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.
I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code.
The above paragraphs are also valid if you violate any of my terms.
If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
property MaintainCommands : {"Display List", "Add Item", "Delete Item", "Edit Item", "Capitalize", "Save List Internally and Quit", "Open List from Disk", "Save List to Disk"}
property SearchStr : {}
property scriptTitle : "Tex-Plus Capitalize"
property Major : 0
property FilePath : {}
property curFpath : {}
set Major to ((system attribute "sysv") mod 256 div 16) ” Thanks again to Nigel Garvey
if SearchStr is {} then
set FilePath to (smartPathParadigm's initiateSmartPath({}, (path to "docs" as text)))
-- populate it with factory settings ....
set SearchStr to {"I Like Dogs", "I Like Cats", "I Like Nothing", "I Like People"}
end if
if (do shell script "/usr/local/opt/checkModifierKeys control") is "1" or true then
tell me
activate
set madeChanges to false
repeat
set choice to (choose from list MaintainCommands with prompt "Words to Capitalize Maintainance ." OK button name {"Ok"})
if choice is false then
if madeChanges is true then
try
display dialog "Sure you want to cancel your editing -- changes will be lost" buttons {"Yes", "Save and Quit"} cancel button {"Yes"} default button {"Save and Quit"}
set choice to {"Save List and Quit"}
on error
error number -128
end try
else
error number -128
end if
end if
if choice is {"Capitalize"} then exit repeat
if choice is {"Save List and Quit"} then return 0
if choice is {"Open List from Disk"} then
set curFpath to FilePath's getPrevPath()
if madeChanges is true then
try
display dialog "Sure you want to cancel your editing -- changes will be lost" buttons {"Yes", "Save and Quit"} cancel button {"Yes"} default button {"Save and Quit"}
return "" -- save changes and quit
end try
end if
repeat
set aHfsFnameAsText to openFile(my scriptTitle, POSIX path of (FilePath's getPrevPath()), true)
-- what to do if it is null???
if aHfsFnameAsText is not null then
FilePath's updatePrevPath(aHfsFnameAsText)
-- open the file
set tmpList to {}
set tmpList to my utf8List's readUtf8IntoList(aHfsFnameAsText, my scriptTitle, SearchStr, false, my Major)
-- if the SearchStr is null, then either the list was empty or there were some other trouble.
if tmpList is null then -- major error error message have been shown.
tell me
return " " -- Saving any properties from the script. And exits the script.
end tell
else if tmpList is not false then
copy tmpList to SearchStr
set tmpList to missing value
exit repeat
end if
else
return "" -- Save properties on exit
end if
end repeat
else if choice is {"Save List to Disk"} then
set aHfsFnameAsText to SaveAs(my scriptTitle, POSIX path of (FilePath's getPrevPath()), true)
-- what to do if it is null???
if aHfsFnameAsText is not null then
FilePath's updatePrevPath(aHfsFnameAsText)
-- open the file
set tmpList to {}
set tmpList to my utf8List's writeUtf8FromList(aHfsFnameAsText, my scriptTitle, SearchStr, my Major)
-- if the SearchStr is null, then either the list was empty or there were some other trouble.
if tmpList is null then -- major error error message have been shown.
tell me
return " " -- Saving any properties from the script. And exits the script.
end tell
else
copy tmpList to SearchStr
set madeChanges to false
set tmpList to missing value
end if
end if
else
set SearchStr to my editList(choice, SearchStr, a reference to madeChanges)
end if
end repeat
end tell
end if
tell application "Tex-Edit Plus"
activate
repeat with srch from 1 to count of items in SearchStr
set thisPhrase to item srch of SearchStr
replace window 1 looking for thisPhrase replacing with thisPhrase without cases matching
end repeat
end tell
on editList(whatToDo, wordList, didChange)
local itemToDelete, theResult, dummy
if whatToDo is {"Delete Item"} then
repeat
tell me
activate
set itemToDelete to (choose from list wordList with prompt "Choose Item to DELETE" OK button name {"Ok"})
end tell
if itemToDelete is not false then
set ctr to 1
set theCount to count of wordList
repeat with anElm in wordList
if contents of anElm is equal to itemToDelete as text then
if ctr = 1 then
set wordList to items 2 thru -1 of wordList
else if ctr is not theCount then
set wordList to items 1 thru (ctr - 1) of wordList & items (ctr + 1) thru -1 of wordList
else
set wordList to items 1 thru -2 of wordList
end if
set contents of didChange to true
exit repeat
else
set ctr to ctr + 1
end if
end repeat
else
return wordList
end if
end repeat
else if whatToDo is {"Add Item"} then
repeat
tell me
activate
try
set theResult to text returned of (display dialog "Enter the item to ADD" default answer "" buttons {"Cancel", "Ok"})
on error
exit repeat
end try
end tell
if not theResult is "" then
set contents of didChange to true
set end of wordList to theResult
end if
end repeat
return wordList
else if whatToDo is {"Edit Item"} then
repeat
tell me
activate
set itemToEdit to (choose from list wordList with prompt "Choose Item to EDIT" OK button name {"Ok"})
end tell
if itemToEdit is not false then
set ctr to 1
set theCount to count of wordList
repeat with anElm in wordList
if contents of anElm is equal to itemToEdit as text then
exit repeat
else
set ctr to ctr + 1
end if
end repeat
try
tell me
activate
set theResult to text returned of (display dialog ("Edit " & itemToEdit as text) & " to something else " default answer itemToEdit as text buttons {"Cancel", "Ok"})
end tell
on error
exit repeat
end try
if not theResult is "" then
set contents of didChange to true
set item ctr of wordList to theResult
end if
else
return wordList
end if
end repeat
else if whatToDo is {"Display List"} then
tell me
activate
set dummy to (choose from list wordList with prompt "The list is currently holding:" OK button name {"Ok"})
end tell
return wordList
end if
end editList
script utf8List
-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33529
on readUtf8IntoList(hfsTargetPathAsText, txtAppTitle, theListToReturn, blnAcceptEmpty, majorNumber)
-- PARAMETERS
-- hfsTargetPathAsText
-- : Hfs pathname of target file to write as text.
-- txtAppTitle
-- : A string or text with the title of the main script.
--theListToReturn
-- : the list to return
-- blnAcceptEmpty
-- : whether reading a list from an empty file is acceptable or not.
-- majorNumber
-- : major revision number of Mac Os X: Tiger yields 4 Leopard yields 5 and so on.
-- RETURNS: a list, false or null
-- if it returns false then something just mildy failed.
-- if it returns null, then there is serious problems.
local theFname, tedim, encodingResult, theRes, tempFileName, infFname, endLineCounter, theLimit, pxFilenNameAsText
script theError
property errval : 0
end script
script o
property l : {}
end script
script fileReader -- convoluted handler to read an utf8 file which is specialiced, -keeps it in its scope of useability.
on readutf8File(hfsTargetPathAsText, refvarStatus, aMajorNumber)
local fp, theContents
try
hfsTargetPathAsText as alias
on error e number n
set contents of refvarStatus to n -- error code for no file found.
return false
end try
try
set fp to open for access alias hfsTargetPathAsText
on error e number n
set contents of refvarStatus to n -- error code for bad access.
return false
end try
try
set theContents to read fp as «class utf8»
on error e number n
try
close access fp
on error e number n
set contents of refvarStatus to n
return false
end try
if not n = -39 then
set contents of refvarStatus to 4000 -- error code for no utf8
else
set contents of refvarStatus to -39
end if
return false
end try
try
close access fp
on error
set contents of refvarStatus to n -- error code for close error.
return false
end try
if aMajorNumber < 5 then --Satimage.osax 3.3.1 block BEGINS
try
set theContents to readtext alias hfsTargetPathAsText encoding "MACINTOSH" -- *untested*
on error e number n
set contents of refvarStatus to 6000 -- error for Satimage.osax not installed
end try
end if --Satimage.osax 3.3.1 block ENDS
return theContents -- as Unicode text
end readutf8File
end script
try
set tedim to text item delimiters -- we are checking that we are actually getting a file, just in case.
set text item delimiters to ":"
set theFname to text item -1 of (hfsTargetPathAsText as alias as text)
set text item delimiters to tedim
if theFname is "" then -- bundle / app or directory!
error number 5000
end if
-- we know we have something that can be a file
set theRes to fileReader's readutf8File(hfsTargetPathAsText, a reference to theError's errval, majorNumber) -- trying to read an utf8 file.
if theRes is false then -- guess what - it wasn't or it was som other misheap that just happened.
set pxFilenNameAsText to quoted form of POSIX path of hfsTargetPathAsText
if theError's errval is 4000 then -- it is an encoding error
try -- figuring out which encoding the file was encoded with.
set encodingResult to do shell script "/usr/bin/file " & pxFilenNameAsText
on error e number n partial result p from f to t
error e number n partial result p from f to t
end try
-- extracts the name of the encodding
set text item delimiters to " "
set theRes to text item 3 of encodingResult
set text item delimiters to tedim
if theRes is in {"UTF-16", "extended-ASCII"} then
set tempFileName to quoted form of POSIX path of ((path to temporary items as text) & theFname)
try
set tempFileName to do shell script "/usr/bin/mktemp -t readUtf8IntoList"
on error e number n partial result p from f to t
error e number n partial result p from f to t
end try
set infFname to pxFilenNameAsText
if theRes is "extended-ASCII" then
set theRes to "MACROMAN"
end if
try
do shell script "iconv -f " & theRes & " -t UTF-8 " & infFname & " >" & tempFileName
do shell script "mv -f " & tempFileName & " " & infFname
on error e number n partial result p from f to t
error e number n partial result p from f to t
end try
set theRes to fileReader's readutf8File(hfsTargetPathAsText, a reference to theError's errval, majorNumber)
if theRes is false then
error number theError's errval
end if
else
-- can't do anything about it
error number 3000
end if
else -- something fatal
error number theError's errval
end if
end if
set theListToReturn to every paragraph of theRes
if not theListToReturn is {} then -- shaves off any empty lines at the end of the file.
set endLineCounter to -1
set theLimit to ((count theListToReturn) * (-1))
set o's l to theListToReturn
if last item of o's l is "" then
repeat while item endLineCounter of o's l is ""
set item endLineCounter of o's l to missing value
if endLineCounter > theLimit then
set endLineCounter to endLineCounter - 1
else
exit repeat
end if
end repeat
end if
set theListToReturn to theListToReturn's strings
end if
if blnAcceptEmpty is false then
if (count of theListToReturn) is 0 then return false
end if
return theListToReturn
on error e number n
if n = -39 then -- empty file
if blnAcceptEmpty is false then
tell me to display alert (txtAppTitle & ":
The file : " & hfsTargetPathAsText & " is empty!")
return false
else
return {}
end if
else if n = 3000 then
tell me to display alert (txtAppTitle & ":
The file : " & hfsTargetPathAsText & " was not encoded with utf8, utf16 or MacRoman encoding.
I can't read in such a file into a list. Check it out in an editor.")
return false
else if n = 4000 then
tell me to display alert (txtAppTitle & ":
The file : " & hfsTargetPathAsText & " has some troubles in it please check it in an editor.")
return false
else if n = 5000 then
tell me to display alert (txtAppTitle & ":
" & hfsTargetPathAsText & " is not a file that can be read into a list. Choose a proper file.")
return false
else if n = 6000 then --Satimage.osax 3.3.1 block BEGINS
tell me to display alert (txtAppTitle & ":
You need to install Satimage.osax in order to run this script under Mac Os X Tiger and earlier: Download and install the right version of of Smile (3.3.1 Regular Editon
from: http://www.satimage.fr/software/en/downloads/downloads_old_smile.html
If not: just rip the 2 blocks marked Satimage.osx out of the handler: readUtf8IntoList() and its internal readutf8File() handler.")
return null --Satimage.osax 3.3.1 block ENDS
else -- fatal errors goes here!
tell me to display alert (txtAppTitle & ":
The file : " & hfsTargetPathAsText & " got the error :
" & e & number & " : " & n)
return null
end if
end try
end readUtf8IntoList
on writeUtf8FromList(hfsTargetPathAsText, txtAppTitle, theListToWrite, majorNumber)
-- PARAMETERS
-- hfsTargetPathAsText
-- : Hfs pathname of target file to write as text.
-- txtAppTitle
-- : A string or text with the title of the main script.
--theListToWrite
-- : the list to write
-- majorNumber
-- : major revision number of Mac Os X: Tiger yields 4 Leopard yields 5 and so on.
-- RETURNS: a list or null
-- if it returns null, then there is serious problems.
-- you must use the returned list for further work.
script o
property l : theListToWrite
end script
script theError
property errval : 0
end script
local theResult
script fileWriter
on writeutf8File(hfsTargetPathAsText, theList, refvarStatus)
local fRef, theText, astid
-- insert an ending empty element at the end if not present.
if item -1 of theList is not "" then set end of theList to "" -- for ending linefeed.
set astid to text item delimiters
set text item delimiters to (run script "\"\\n\"") -- linefeed Thanks! to Nigel Garvey
set theText to "" & theList -- internal representation Tiger/Leopard
set text item delimiters to astid
try
set fRef to (open for access file hfsTargetPathAsText with write permission)
on error e number n
set contents of item -1 of theList to missing value -- removes empty item
set contents of refvarStatus to n -- some errorcode
return false
end try
try
set eof fRef to 0
write «data rdatEFBBBF» to fRef -- BOM Thanks! to Nigel Garvey
write theText to fRef as «class utf8»
on error e number n
set contents of item -1 of theList to missing value -- removes empty item
set contents of refvarStatus to n -- some errorcode
try
close access fRef
on error e number n
close access fRef
end try
return false
end try
try
close access fRef
on error e number n
set contents of item -1 of theList to missing value -- removes empty item
set contents of refvarStatus to n -- some errorcode
close access fRef
return false
end try
set text item delimiters to astid
set item -1 of theList to missing value -- removes empty item
return true
end writeutf8File
end script
if majorNumber < 4 then
tell me
activate
display alert (txtAppTitle & ":
Versions of Mac Os X earlier than 10.4.0 is unsupported: your version is 10." & Major & "xx")
end tell
return null
end if
set theResult to fileWriter's writeutf8File(hfsTargetPathAsText, o's l, a reference to theError's errval)
if theResult is false then
-- should have localization here!
tell me
activate
display alert (txtAppTitle & ":
The file : " & hfsTargetPathAsText & " got the error number : " & theError's errval)
end tell
return null
end if
return o's l's strings
end writeUtf8FromList
end script
-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33546
-- its a generic handler for saving a file which can ask if your really want to abort, the save operation or just returns from the handler otherwise.
-- PARAMETERS:
-- txtTitle
-- The title you want to show up in the SaveAs dialog.
-- pxStartPathAsText
-- The initial Posix path you want to show up.
-- blnAskToConfirm
-- if you want the user to confirm the cancel, the dialog will only show up once, next time straight out.
-- RETURNS
-- The hfs Path as text upon sucess, null otherwise.
on SaveAs(txtTitle, pxStartPathAsText, blnAskToConfirm)
local new_path, blnUserQuit, iConfirmTimes
set blnUserQuit to false
set iConfirmTimes to 0
tell me
activate
repeat while true
tell application "System Events"
tell application process "Finder"
activate
try
set new_path to (choose file name with prompt txtTitle default location (POSIX file pxStartPathAsText))
exit repeat
on error e number n
-- display dialog "err: " & e & " : " & n
if n is -128 then
set iConfirmTimes to iConfirmTimes + 1
try
if blnAskToConfirm is true and iConfirmTimes < 2 then
set theButton to the button returned of (display dialog "Do you want to try again?" with title txtTitle with icon 2)
-- falls through the end of the repeat loop and repeat ourselves.
else -- not talking back
set blnUserQuit to true
exit repeat
end if
on error -- user hit "cancel" or equiv in the dialog.
set blnUserQuit to true
exit repeat
end try
end if
end try
end tell
end tell
end repeat
end tell
if not blnUserQuit then
try
set fp to open for access new_path with write permission
on error e number n
display alert (txtTitle & " an error occured: " & e & " : " & n)
return null
end try
try
close access fp
on error e number n
try
close access fp
on error
display alert (txtTitle & " an error occured: " & e & " : " & n)
return null
end try
end try
set new_path to new_path as alias as Unicode text
return new_path
else
return null
end if
end SaveAs
-- its a generic handler for opening a file which can ask if your really want to abort the open operation or just returns from the handler otherwise.
-- PARAMETERS:
-- txtTitle
-- The title you want to show up in the SaveAs dialog.
-- pxStartPathAsText
-- The initial Posix path you want to show up.
-- blnAskToConfirm
-- if you want the user to confirm the cancel, the dialog will only show up once, next time straight out.
-- RETURNS
-- The hfs Path as text upon sucess, null otherwise.
-- openFile("Open a File", "/", true)
on openFile(txtTitle, pxStartPathAsText, blnAskToConfirm)
local new_path, blnUserQuit, iConfirmTimes
set blnUserQuit to false
set iConfirmTimes to 0
tell me
activate
repeat while true
tell application "System Events"
tell application process "Finder"
activate
try
set new_path to (choose file with prompt txtTitle default location (POSIX file pxStartPathAsText) of type {"TEXT"})
exit repeat
on error e number n
-- display dialog "err: " & e & " : " & n
if n is -128 then
set iConfirmTimes to iConfirmTimes + 1
try
if blnAskToConfirm is true and iConfirmTimes < 2 then
set theButton to the button returned of (display dialog "Do you want to try again?" with title txtTitle with icon 2)
-- falls through the end of the repeat loop and repeat ourselves.
else -- not talking back
set blnUserQuit to true
exit repeat
end if
on error -- user hit "cancel" or equiv in the dialog.
set blnUserQuit to true
exit repeat
end try
end if
end try
end tell
end tell
end repeat
end tell
if not blnUserQuit then
return new_path as Unicode text
else
return null
end if
end openFile
-- See macscripter.net/viewtopic.php?pid=129895#p129895 For Documentation
-- © McUsr 2010 and put in the public domain.
script smartPathParadigm
on makeSmartPath(hfsInitialPathAsText)
script o
property initialPath : "" & hfsInitialPathAsText -- doesn't harm
property prevPath : {}
on getPrevPath()
if prevPath is {} then
initialPath
else
prevPath
end if
end getPrevPath
on updatePrevPath(hfsReturnedPathAsText)
local newPath
set newPath to smartPathParadigm's getPathToParentFolder(hfsReturnedPathAsText)
if prevPath is not newPath then
set prevPath to newPath
end if
end updatePrevPath
on withinRealm(hfsFilePathAsText)
if hfsFilePathAsText contains initialPath then
return true
else
return false
end if
end withinRealm
on setPrevPath(hfsNewPrevPathAsText)
set hfsNewPrevPathAsText to "" & hfsNewPrevPathAsText -- doesn't harm
set prevPath to hfsNewPrevPathAsText
end setPrevPath
end script
o
end makeSmartPath
on initiateSmartPath(scoForPath, hfsIntialPathAsText)
script o
property pathobj : scoForPath
end script
if scoForPath is {} then
set o's pathobj to smartPathParadigm's makeSmartPath(hfsIntialPathAsText)
end if
o's pathobj
end initiateSmartPath
on getPathToParentFolder(hfsFileOrFolderPathAsText)
local tids, theFile
set hfsFileOrFolderPathAsText to "" & hfsFileOrFolderPathAsText -- doesn't harm.
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
if character (length of hfsFileOrFolderPathAsText) of hfsFileOrFolderPathAsText is ":" then
set hfsFileOrFolderPathAsText to text items 1 thru -3 of hfsFileOrFolderPathAsText
else
set hfsFileOrFolderPathAsText to text items 1 thru -2 of hfsFileOrFolderPathAsText
end if
set hfsFileOrFolderPathAsText to "" & hfsFileOrFolderPathAsText -- necessary here.
set AppleScript's text item delimiters to tids
return hfsFileOrFolderPathAsText
end getPathToParentFolder
end script