Why isn't my AppleScript Working?

Discussion in 'Mac Programming' started by cbrain, Mar 11, 2007.

  1. macrumors 65816

    cbrain

    Joined:
    Dec 9, 2006
    Location:
    North-East, UK
    #1
    Hi.

    I am trying to write an AppleScript that empties my downloads folder. However, whenever I run the script, I get the dialog box, but then I get this error: "Finder got an error: Can't get folder "Downloads" of folder "Home".

    The script is as follows:

    Code:
    set userResponse to the button returned of (display dialog "Are you sure you want to empty your downloads folder?" buttons {"No", "Yes"} default button 2 with icon caution)
    if userResponse is "Yes" then
    	tell application "Finder"
    		empty folder "Downloads" of the folder "Home"
    	end tell
    	
    end if
    There is defiantly a folder called "Downloads" in my home folder. Is there something wrong with my code?

    Thanks.
     
  2. Administrator/Editor

    WildCowboy

    Staff Member

    Joined:
    Jan 20, 2005
    #2
    I'm not an AppleScript expert, but don't you have to further define the location of the Downloads folder? Something like this:

    Code:
    empty folder "Downloads" of folder "Home" of folder "Users" of startup disk
     
  3. thread starter macrumors 65816

    cbrain

    Joined:
    Dec 9, 2006
    Location:
    North-East, UK
    #3
    I've just tried that, but this time I get: "Finder got an error: Can't get folder "Downloads" of folder "Home" of folder "Users" of startup disk."
     
  4. macrumors 6502a

    Joined:
    Dec 12, 2005
    Location:
    Adelaide
    #4
    You could use a do shell script to do it

    Code:
    do shell script "rm -Rf ~/Downloads/"
    
    im not sure if thats the right command but you can do something like that
     
  5. macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #5
    Where is the folder "Downloads"?
    Is it "Macintosh HD:Users:your username:Downloads". Or something else?
     
  6. Administrator emeritus

    xUKHCx

    Joined:
    Jan 15, 2006
    Location:
    The Kop
    #6
    You do not need to put home in quotes also the empty contents command wont work. I would do it this way:

    set userResponse to the button returned of (display dialog "Are you sure you want to empty your downloads folder?" buttons {"No", "Yes"} default button 2 with icon caution)
    if userResponse is "Yes" then
    tell application "Finder"
    if the folder "Downloads" of home exists then
    delete entire contents of folder "Downloads" of home
    else
    display dialog "Folder not found"

    end if
    end tell
    end if


    This is actually delete the folder "Downloads" which would cause an error the next time the application is run. Also it is not the safest method as it actually deletes the files right there and then while the above method just moves them to the trash so then can be recovered incase the files were deleted accidentally.
     
  7. thread starter macrumors 65816

    cbrain

    Joined:
    Dec 9, 2006
    Location:
    North-East, UK
    #7
    Thank You! That Worked!
     

Share This Page