Tuesday, August 11, 2009

Arguing with AppleScript

AppleScript sounds great: it's a high-level language, and the code, done right, can be quite human-readable. That's also the problem ... there are so many cases where "to" and "of" both work, and then you run into a case where it matters. Frustrating! That makes me long for a less-flexible language; if there's only one way to issue the command, look it up, use it, and you're done. Or try to figure out where and why AppleScript is so slippery ...

Anyway, yesterday's quest was to identify special folders, like the Applications folder, without assuming /Applications.

Most of the names of special folders are listed in the AppleScript Language Guide: Commands Reference. Yay! However, some special folders aren't there. You can find that longer list in AppleScript 1-2-3. For instance, I used this line of AppleScript

set removePlugin to (the path to "cmnu" as string) & "SyncpCMPlugin.plugin" as alias

where the path to "cmnu" gives me the path to the user's Contextual Menu Items folder. That's not as readable as I'd like my AppleScript, but there's no human-readable version of the 4-letter code.

Today's challenge is to discover if the Trash is empty or not with AppleScript; I'm not going to cheat and use shell.

I've tried

set trashItems to the count of every item in the trash

set number_of_items to (count (every item of the trash))

set tryThis to count of items in trash

and they don't work.

Oh yeah, here's what I mean about AppleScript being a slippery dog, and natural language failing.

These lines work:

tell application "Finder" to set trashList to (items of trash)

tell application "Finder" to open (the path to the trash folder as alias)

These lines do not work:

tell application "Finder" to set trashList to (items of trash folder) with Can't get every item of trash folder.

tell application "Finder" to open the trash

Since Unix is so good at text manipulation, I find that aspect of AppleScript frustrating, and the Working with Text AppleScript reference useful.

OK, so I have solved today's AppleScript frustration to see if the Trash is empty or full.

set trashList to {""}

set trashFiles to {""}

tell application "Finder" to set trashList to (the items of the trash)

tell application "Finder" to set trashFiles to the name of the items in the trash

set old_delimiters to AppleScript's text item delimiters

set AppleScript's text item delimiters to {", "}

set display_trashFiles to trashFiles as string

set trashCount to the count of trashFiles

if (trashFiles is {}) then

display dialog "There are no items in the Trash." buttons {"OK"} default button 1 with title "Trash Empty" with icon note

else

display dialog "There are items in the Trash." buttons {"OK"} default button "OK" with title "Trash Not Empty" with icon caution

end if

if trashCount > 1 then

display dialog "There are " & trashCount & " items in the Trash." & return & "list: " & (trashFiles as string) buttons {"OK"} default button "OK" with title "Trash Full" with icon stop

else if trashCount = 1 then

display dialog "There is " & trashCount & " item in the Trash." & return & "list: " & display_trashFiles buttons {"OK"} default button "OK" with title "Trash Full" with icon caution

else

display dialog "Empty, " & trashCount & " items in the Trash." & return & "list: " & display_trashFiles

end if

set AppleScript's text item delimiters to old_delimiters

tell application "Finder"

display dialog "What would you like to do?" buttons {"Empty Trash", "View Trash", "OK"} default button "Empty Trash" with icon note

if button returned of result is "Empty Trash" then

empty trash

else if button returned of result is "View Trash" then

open the (path to the trash folder as alias)

end if

end tell

That was harder than I expected, but now it's done.

No comments:

Post a Comment