Aliases to dropped files, opening for access

I’m trying to make a very basic time-sheet calculator for freelance jobs. It’s a droplet which, when I drop a .txt file on it, will append either “Start: (current time)” or “Stop: (current time)” to the end of the file, depending on what’s already in the file (this is also kind of a learning project for scripting). I’ve got everything working more or less the way I want it to, except that I don’t know how to test if the file is empty to start with. That problem has led me back to other, more fundamental problems, which I hope someone will be kind enough to help me with. Here’s what I’ve got so far:


on open dropped
	tell application "Finder" to set drop to (item 1 of dropped)
	-- 	only allow a single .txt file to be used with this droplet
	if length of dropped is not 1 or folder of (info for drop) or name extension of (info for drop) is not equal to "txt" then
		display dialog "This droplet only accepts a single text file!" buttons {"Sorry"} default button 1
		return
	end if
	-- 	write time stamp lines to file
	set content to every paragraph of (read drop)
	if length of last item of content = 0 or first word of last item of content is not "Start" then
		write (return & "Start: " & ((current date) as string)) to drop starting at eof
		display dialog "Starting..." buttons {"Okay"} default button 1 giving up after 1
	else
		write (return & "Stop: " & ((current date) as string)) to drop starting at eof
		display dialog "Stopping..." buttons {"Okay"} default button 1 giving up after 1
	end if
end open

This, such as it is, works fine, unless the file is empty, in which case I get an “end of file error”. This makes sense, I guess, as I’m trying to set content to every paragraph of an empty file. But I’m foiled in every attempt to test if the file is empty before I get on with the rest of it. I tried if size of (info for drop) = 0 and various permutations, and that didn’t work; neither did a try block with error -39. I also tried doing an open for access so that I could test if get eof was equal to zero, but couldn’t make the open for access code work.

So that led me to a deeper problem. All attempts to make open for access work (and I tried a million permutations in defining the variable drop at the top of the script) result in a variation of the “Can’t make file (alias “Mac:encoded:path:to:myfile”) into type file specification” error. What is it I’m doing wrong?

All I really need is to check if the file is empty, and then write a “Start: (current date)” line to it. But if someone could explain what I’m not understanding about file aliases, particularly in a list resulting from an on open, I’d be very grateful (and would be less likely to keep asking for help!).

Thanks in advance,
Eric

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

Hi Eric,

welcome to MacScripter

Here one suggestion:

on open dropped
	set drop to item 1 of dropped
	tell (info for drop) to if its folder or its name extension is not "txt" then
		display dialog "This droplet only accepts a single text file!" buttons {"Sorry"} default button 1
		return
	end if
	set ff to open for access drop with write permission
	try
		set theText to last paragraph of (read ff)
		if (count theText) is 0 or first word of theText is not "Start" then error
		write_data(ff, "Stop", "p")
	on error
		write_data(ff, "Start", "")
	end try
	try
		close access drop
	end try
end open

on write_data(f, prompt, p)
	write (return & prompt & ": " & ((current date) as string)) to f starting at eof
	display dialog prompt & p & "ing..." buttons {"Okay"} default button 1 giving up after 1
	try
		close access f
	end try
end write_data

it traps the error and uses the common read/write commands of AppleScript.
The open for access syntax is
either

open for access file "path:tofile" -- path string

or

tell application "Finder" to set f to file 1 of desktop
open for access file f -- file specifier

or

tell application "Finder" to set f to (file 1 of desktop) as alias
open for access f -- alias

Hi Stefan,

Thanks for the swift (and very helpful) reply!

I guess my problem was trying to use ‘open for access file’ with an alias, which is apparently the only syntax that doesn’t work. This has been a long, voodoo-filled process, and apparently my lucky scripting underwear wasn’t doing the trick.

Thanks again for your re-write, there’s a lot of good things in there besides the main fix.

Yrs,
Eric

The only time “open for access alias pathToFile” doesn’t work is if the file doesn’t exist. If it does this works perfectly:

set F to (choose file) -- returns an alias to the file chosen
set ff to open for access F -- opens the file
set R to read ff -- reads it into a variable
close access ff

– close it again

I guess, he tried open for access file (alias pathToFile) :wink:

Missed that Stephan. It’s file OR alias but not both.

Hi Eric,

You can find if a file is empty with:


set f to choose file
if (get eof f) is 0 then
	display dialog "The file is empty."
end if

gl,

No kidding, that does work. I thought I’d tried it and it told me I didn’t have permission to get eof, but I guess that was due to some other unnecessary crud I’d stuck in there. It’s been an unlucky round of trial-and-error…

Thanks to all!