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)