|
|
Why does this script return inconsistent answers?
|
|
|
|
Fresh-Faced Recruit
Join Date: Nov 1999
Status:
Offline
|
|
property thefilepath : ""
if thefilepath is "" then set thefilepath to choose folder
tell application "Finder"
set foo to (count of (every item in alias thefilepath whose creation date is less than the (current date)))
end tell
display dialog foo
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Oct 1999
Location: Edmonton, Alberta, Canada
Status:
Offline
|
|
because properties are not constant if run from with in the script editor, how ever if you were to save that script as a stand alone app it would be constant....
------------------
PGP key: http://www.geocities.com/maccanada/pgpkey.html
-30-
|
-- An idea is salvation by imagination.
-- Frank Lloyd Wright
-- 30 --
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Nov 1999
Status:
Offline
|
|
After reading your reply, I compiled the script as an applet and still get incorrect and inconsistent results.
I then changed the script so that the folder was no longer a property, and still got inconsistent and incorrect results. <sigh>
--Michelle
|
|
|
|
|
|
|
|
|
Cattywampus
|
|
What kind of results are you getting? Are there inconsistencies with theFilePath or with foo? Have you tried including "display dialog theFilePath" for debugging purposes?
Script properties are fun. When a script is run, if any of its properties are changed during the script's run time, these changes are saved to the script itself. i.e., if I made a script:
property kCounter : 0
on run
set kCounter to kCounter + 1
display dialog kCounter
end run
..and then I ran the script 5 times in a row, it would display 1, 2, 3, 4, and 5. However, when you open a script in the Script Editor, the Script Editor smites all of the saved properties, resetting them to their default values.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Nov 1999
Status:
Offline
|
|
The inconsistent results are foo. Foo gives an accurate count of the items in the returned list, but the list doesn't contain all the entries it should. The filepath is always correct; it's just the contents of the folder that are not returned correctly.
--Michelle
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi Michelle:
I agree that there is a general issue of inconsistent results when querying the Finder's date-related properties. For example, I've occasionally run into this problem with 'modification date' comparisons as well. The problem seems to crop up mostly when date properties are compared *directly*, in conditional or filter expressions.
The AppleScript Sourcebook website also confirms that this has been a known bug in the Finder since Mac OS 8.x; please refer to their notes on The Scriptable Finder.
Instead of using a direct filter expression, try using an indirect loop instead. For each item, save its creation date in a local variable and then use that to compare against the current date -- something like this:
property thefilepath : ""
if thefilepath is "" then set thefilepath to choose folder
tell application "Finder"
set theCurrentDate to (current date)
set theCandidateItems to (every item in thefilepath)
set bar to (count theCandidateItems)
set theMatchingItems to {}
repeat with thisItem in theCandidateItems
set thisItemCreationDate to (creation date of thisItem)
if (thisItemCreationDate is less than theCurrentDate) then
copy (theMatchingItems & thisItem) to theMatchingItems
end if
end repeat
set foo to (count theMatchingItems)
end tell
display dialog "bar = " & bar & "; " & "foo = " & foo
return
Here, 'bar' (the candidate count) and 'foo' (the match count) should now be consistent across runs (and should also be the same as each other, if there aren�t any of today's items in the specified folder). The downside of this workaround is that the loop construct is considerably slower than a filter.
BTW, the path returned by the 'choose folder' command is apparently already an alias, so it's not strictly necessary to coerce it to an alias again.
[This message has been edited by Paul Crawford (edited 12-11-1999).]
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Nov 1999
Status:
Offline
|
|
Thanks Paul. That's essentially what I did. Here's the script that I wound up writing:
(For some reason, the editor software changed a lessthan-or-equal sign to an asterisk in one of the lines. Also, I had a variable named "trash items" as one word without the space, and the editor changed it to "tra** **tems", again without the space. I can understand why it did the latter but not why it changed the sign.)
property thefilepath : " "
--Keep 14 days of digests or 14 digests, whichever is greater.
set daysToKeep to 14
set Maxfiles to daysToKeep
set counter to 0
try
set thefilelist to list folder thefilepath without invisibles
on error
set the thefilepath to choose folder
set thefilelist to list folder thefilepath without invisibles
end try
if (count thefilelist) is greater than Maxfiles then
repeat with thefile in thefilelist
set anotherfilelist to list folder thefilepath without invisibles
if (count anotherfilelist) * Maxfiles then --The asterisk should be a <= sign. The Bulletin board editor changed it.
exit repeat
else
set thetestfile to alias ((thefilepath & thefile) as text)
set thefiledate to the modification date of (info for thetestfile)
set theDateDifferential to (current date) - thefiledate
if theDateDifferential > daysToKeep * days then
tell application "Finder" to delete thetestfile
set counter to counter + 1
end if
end if
end repeat
end if
if counter = 1 then
set numOfDigests to " digest"
else
set numOfDigests to " digests"
end if
set digestsMoved to counter & numOfDigests & " moved to trash."
tell application "Finder"
set Tra****ems to count items of the trash
end tell
set EmptyQuestion to "Do you want to empty the trash now?"
set buttonList to {"Don't Empty", "Empty"}
if Tra****ems = 1 then
set trashMsg to "There is only one item in the trash." & return & EmptyQuestion
else if Tra****ems > 1 then
set trashMsg to "There are a total of " & Tra****ems & " items in the trash." & return & EmptyQuestion
else
set trashMsg to "The trash is empty."
set buttonList to {"OK"}
end if
set dialogResponse to display dialog (digestsMoved & return & trashMsg) as string buttons buttonList default button 1 with icon note giving up after 60
if button returned of dialogResponse is "empty" then tell application "Finder" to empty the trash
--end script
Thanks for pointing out that the filepath is already an alias; I was treating it as a string.
--Michelle
[This message has been edited by Michelle Steiner (edited 12-11-1999).]
[This message has been edited by Michelle Steiner (edited 12-11-1999).]
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Michelle, your script is pretty cool, in spite of the UBB editor's "enhancements" :-) In fact, it looks like a very useful housekeeping tool.
On a somewhat darker note, I really do wish that Apple would set aside some unlucky developer to spend a month or so just fixing all of the *known* bugs in AppleScript. Yes, Apple's main priority must be (and is) to ensure that Mac OS X Client ships on schedule, but AppleScript is also being promoted loudly from the rooftops, despite the fact that it has a few basic flaws. (I mean, even some of Apple's own examples in the AppleScript Language Guide and the AppleScript Finder Guide do not currently work reliably!) OK, I'm down off my soapbox now; sorry for the rant. ;-)
As always, happy scripting!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|