TextEdit in 10.4 vs. 10.5

I’m making a script that will be able to create a md5 checksum and then copy the file to a new location. Our systems have both 10.4 and 10.5 running and I’ve come across the problem that in 10.5 my code works and 10.4 it looks like it works however the txt file that should be created doesn’t actually exist where it should. I’ve read that there seemed to be some issues with TextEdit saving in 10.4 or at least its different. This is a snippet of my code.

tell application “TextEdit”
activate
–set the text of document 1 to (allMD5 as string)
set the text of the front document to (allMD5 as string)
–close document 1 saving in checksumSource
close every document saving in checksumSource
quit
end tell

The lines that are rem’d out are the ones I’ve been trying to get to work in 10.4. The ones that are not currently work in 10.5. When I run it in 10.4 even in the Event Log it looks like it is saving correctly and to the correct path, but when I go to open the txt file its not there. Its strange.

Any ideas?

Hi,

It is good practice to first check if a document already exists. If I use the following code I have no problems from Mac OS X 10.4 - 10.6:


tell application "TextEdit"
	activate
	if not (exists document 1) then
		make new document
	end if
	set text of document 1 to ("Test" as string)
	close document 1 saving in "Macintosh HD:Users:martin:Desktop:test.txt"
end tell

Just my 0.2€ :smiley:

I am running 10.4. Both your commented-out and your not commented-out commands work on my system.

If TextEdit is already running and has an open document, your code clobbers the open document.
If TextEdit is already running and it has no open documents, it gets an error “NSReceiverEvaluationScriptError: 4” (because bdocument 1 is an invalid reference).

Both set text of document 1 . and set text of front document to . generate the same events (front document is a synonym for document 1).

Assuming there are no other unsaved documents, both of your close commands also work. The close every document variation seems a little presumptuous though. What is it supposed to do if there is another unsaved document open? Same issue with quit.

One issue is that my TextEdit defaults to Rich Text (it is a preference setting), so the saved file is RTF, despite having an extension of “txt”. There does not seem to be a non-UI way to make sure the document is a plain text document.

Overall, I would say that this code is less presumptuous, but the set text.

tell application "System Events" to set originallyRunning to exists application process "TextEdit"
tell application "TextEdit"
	activate
	if originallyRunning or not (exists document 1) then
		set newDoc to make new document
	else
		set newDoc to document 1
	end if
end tell
tell application "System Events"
	set mpt to a reference to menu item "Make Plain Text" of menu "Format" of menu bar item "Format" of first menu bar of application process "TextEdit"
	if exists mpt then click mpt
end tell
tell application "TextEdit"
	set the text of newDoc to (allMD5 as string)
	close newDoc saving in checksumSource
	if not originallyRunning then quit
end tell

But really, if you just want to write a plain text file, there is no need for TextEdit. The write command from StandardAdditions can do it.

try
	set f to open for access file checksumSource with write permission
	set eof of f to 0
	write allMD5 to f as «class utf8»
	(*
	 * as «class utf8» --> UTF8
	 * as Unicode text --> UTF-16BE
	 * as text --> system's primary encoding (usually MacRoman)
	 *)
	close access f
on error m number n
	error "Unable to write to file: (" & n & ") " & m
end try

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.3 (4531.9)
Operating System: Mac OS X (10.4)

Thanks for the help everyone. I tried the new lines and in the Event Log I this line


close document 1 saving in "/Volumes/172.18.23.167/Temp/ProRes Checksums/[file name]
quit

When I navigate to that path the file does not exist. I’ve tried sending the document just to my local desktop as well and it doesn’t show up. Any thoughts?

also

this line gives me an error. "Expected end of line, etc. but found “«”.

So, you are using a POSIX path? I think you need to use an HFS path instead (Volume Name:Temp:ProRes Checksums:filename). That is what I was using in my testing.

No idea why write . as «class utf8» is not working for you. Are you doing copy-and-paste to retyping it (maybe there is a typo)? The line from your message compiles just fine here.

above in my script i have a variable that uses the alias of the path i need. the path you are seeing is from the event log not my actual script. just had it in here to show that it looks like it goes to the correct place but when i go looking for it the file is not there. again this script works in 10.5 with no issues just has a problem in 10.4.

yeah i did copy paste into my script and the

write allMD5 to f as «class utf8»

did not work. i know this is a dumb question but is there a setting i need to turn on/off in order to use this?


I got the write allMD5 to f as «class utf8» part to work however when the program runs it looks like its looking for an already existing file which is why i thought i needed to use TextEdit. I need to make a new file with the info in side and be able to name it what ever the file is called

Here is a sample from the program. It works fine in 10.5 but 10.4 again will not save correctly.


	--Creates list of files to be processed
 	set selectedFiles to (choose file with prompt "Select Files To Be Copied/MD5 Created" with multiple selections allowed)
 	 	--This is where the MD5 text file will be saved to
 	set md5Destination to "BluearcNFS:Temp:ProRes Checksums:"
 	 	--Delimit selected file to extract just the file name with out the extention
 	set stringFileName to first item of selectedFiles as string 	set AppleScript's text item delimiters to {":"}
 	set delimitedList to every text item of stringFileName
 	set AppleScript's text item delimiters to ""
 	set saveFileName to last item of delimitedList as string
 	set AppleScript's text item delimiters to {"."}
 	set newDelimitedList to every text item of saveFileName
 	set AppleScript's text item delimiters to ""
 	set saveName to first item of newDelimitedList
 	 	--Creates posix path to where the md5 txt file is to be saved adding the file name and type
 	set checksumSource to (POSIX path of md5Destination) & saveName & " - " & theDate & ".txt"
 	 	--This is where the feature file will be saved to
 	set fileDestination to (choose folder with prompt "Selected Where the file is to be copied")
 	set folderPOSIXpath to quoted form of POSIX path of fileDestination
 	 	--Count how many files to process
 	set fileCount to (count every item in selectedFiles)
 	 	--variable to increase the item number
 	set upCounter to 1
 	 	--set up a blank list to have all md5's apended to
 	set allMD5 to {}
 	 	--Repeats the POSIX convertion for mulitple files
 	repeat fileCount times
		--Allows program to work on one file at a time
 		set currentItem to item (fileCount - fileCount + upCounter) of selectedFiles
 		--To find the file size of the current file
 		set fileSize to currentItem
 		set dataSize to size of (info for fileSize) as string
 		--Convert to POSIX path in order to run in shell script
 		set SourceFile to POSIX path of currentItem
 		--Creates an MD5 checksum of files
 		set fileMD5 to do shell script "md5 \"" & SourceFile & "\""
 		--Tells program to wait until the variable fileMD5 has been created before moving on since large files take a long time to create an MD5
 		repeat until fileMD5 is not ""
			delay 2
		end repeat 		
--Adds the results a single variable in order to output to a text file
  		set the end of allMD5 to (fileMD5 & return & "File Size: " & dataSize & return & return) 		--Count up 1 to move on to next file
 		set upCounter to (upCounter + 1)
	end repeat
 	 	--Quits TextEdit if it is open  	
        if appIsRunning("TextEdit") then
		tell application "TextEdit"
			quit saving no
		end tell
	end if
 	 	 	--Creates a txt document with path of file and md5 checksum
 	tell application "TextEdit"
		activate
 		--set the text of document 1 to (allMD5 as string)
 		set the text of the front document to (allMD5 as string)
 		--save document 1 in checksumSource
 		close every document saving in checksumSource
 		quit
	end tell 	
------------------------------------------------------------------------------------------------- 	
--Copy Part of Program 	
repeat with selectedFile in selectedFiles
		tell application "Finder" to set filename to (name of selectedFile)
 		set filePOSIXpath to quoted form of POSIX path of selectedFile
 		do shell script ("cp " & filePOSIXpath & " " & folderPOSIXpath)
	end repeat
 	 	--Dialog to let user know the program is done
 	display dialog "MD5 and Copy process is done!" buttons {"Ok"} default button 1

The open for access and set eof stuff in my example of write were there to deal with creating a new file and truncating it to make sure no old data was kept if new, shorter data was written to an existing file.

In your larger, partial script it certainly looks like you are passing a POSIX path to TextEdit’s close command as the file to save in. On my 10.4 system, that seems to make TextEdit try to save the file to the root of my startup disk (to which I do not have write access, since I do most stuff as a non-admin user). It seems that Tiger’s TextEdit only expects HFS-style paths, not POSIX-style paths.

If theDate does not have any colons (the script does not show how you set its value), try doing set checksumSource to md5Destination & saveName & " - " & theDate & “.txt”.

Or, another approach is to use an object to encapsulate the path instead of a plain string path. Try it again with set checksumSource to POSIX file ((POSIX path of md5Destination) & saveName & " - " & theDate & “.txt”).

Thank you so much! I just needed some other eyes I guess to see that I had the checksumSource being set to the POSIX path. I had that set up earlier in a previous version and forgot to change it. I just removed the to (POSIX path of) section and it works for both OS’s thanks again!