Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Why does this script return inconsistent answers?

Why does this script return inconsistent answers?
Thread Tools
Michelle Steiner
Fresh-Faced Recruit
Join Date: Nov 1999
Status: Offline
Reply With Quote
Nov 28, 1999, 05:43 PM
 
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
     
MacCanada
Junior Member
Join Date: Oct 1999
Location: Edmonton, Alberta, Canada
Status: Offline
Reply With Quote
Nov 30, 1999, 04:33 AM
 
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 --
     
Michelle Steiner  (op)
Fresh-Faced Recruit
Join Date: Nov 1999
Status: Offline
Reply With Quote
Nov 30, 1999, 10:55 AM
 
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
Guest
Status:
Reply With Quote
Nov 30, 1999, 08:24 PM
 
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.
     
Michelle Steiner  (op)
Fresh-Faced Recruit
Join Date: Nov 1999
Status: Offline
Reply With Quote
Nov 30, 1999, 08:41 PM
 
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
     
Paul Crawford
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Dec 11, 1999, 05:12 PM
 
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).]
     
Michelle Steiner  (op)
Fresh-Faced Recruit
Join Date: Nov 1999
Status: Offline
Reply With Quote
Dec 11, 1999, 09:17 PM
 
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).]
     
Paul Crawford
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Dec 15, 1999, 12:52 AM
 
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!
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:32 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,