The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Empty Trash With Applescript (http://hintsforums.macworld.com/showthread.php?t=87276)

duffman33 03-13-2008 08:31 PM

Empty Trash With Applescript
 
Basically, I have this program where the last part is moving several files to the trash and the emptying the trash. About half the time it works absolutely fine. However, the other half it comes up with an error that says, "<File> is in use and could not be deleted" after trying to empty the trash.

While not a big deal, it stops the script with an error and I cannot finish the script, which is a problem. I don't need to empty the trash if theres an error, I can do it myself later, but I do need help scripting around this so if an error does come up, it can close the dialog box and continue on with the script. Thank you.

cwtnospam 03-13-2008 08:38 PM

Code:

try
-- Your
-- Code
-- Here
on error
-- Error
-- Handling
-- Code
-- Here - can be empty!
end try


Red_Menace 03-13-2008 08:39 PM

You can put your empty trash in a try statement to catch the error:

Code:

try
        -- empty trash
on error
        -- nevermind
end try


duffman33 03-13-2008 09:05 PM

Thanks, I already did know that however. My question was a little more specific, as in, how can I tell Finder to ignore the error, or do I need to close the dialog box, etc. Or, put another way, what code will I need to put in the 'on error' portion for my specific example. Thanks.

cwtnospam 03-13-2008 09:32 PM

The 'on error' portion can be blank. The error occurs and sends the code to the on error portion, where the code resumes.

Code:

try
-- code that might cause an error.
on error
display dialog "Oops!"
end try
display dialog "Error, what error?"

In this case, "Oops!" will only be displayed if an error occurs, and it will be the only indication of an error. By the second dialog, there is no longer an error, if there was one to begin with.

tw 03-13-2008 11:04 PM

it would help if we could see your code - there's no telling why you're getting that error otherwise. for instance, are you using the 'move' command to send things to the trash, or the Finder's 'delete' command? are these large files, or are they open when you send them to the trash?

john love 03-14-2008 09:10 AM

Empty the Trash
 
In the process of getting this to work, I tried several approaches which are commented-out below. The remaining active code does work and so does the commented-out code. Have Fun.

Code:

on emptytheTrash()
        (*
                Courtesy of: http://bbs.applescript.net/viewtopic.php?id=699
               
                Will get an error if you use:
                        set locked of every item of entire contents of the trash to false
                while items are in the Trash, especially if the locked items are buried within Folders.
               
                Since the System considers the Trash a different type of container than a regular
                folder, the only consistent way for this method to work is to unlock all items
                before they are moved to the Trash.  This is accomplished within emptyFolder(),
                so we do not need to mess with "Empty Trash Temp" here.
        *)
       
        tell application "Finder"
               
                if length of (items in the trash as string) is 0 then return
               
                (*
                if not (folder "Empty Trash Temp" exists) then
                        set theFold to make folder with properties {name:"Empty Trash Temp"}
                else
                        set theFold to folder "Empty Trash Temp"
                end if
               
                move items in the trash to theFold
               
                -- these will not burrow down into Folder and sub-Folder contents
                -- set locked of items of theFold to false
                -- set locked of every item of theFold to false
                set locked of every item of entire contents of theFold to false
               
                delete theFold
                *)
               
                empty trash
               
                -- Wait until the Trash is empty.
                repeat until (count items of trash) = 0
                        delay 1
                end repeat
               
        end tell -- application "Finder"
       
end emptytheTrash


Mikey-San 03-14-2008 10:23 AM

Quote:

empty trash

-- Wait until the Trash is empty.
repeat until (count items of trash) = 0
delay 1
end repeat
I'm pretty sure "empty trash" is a synchronous blocking operation. Did you actually encounter a need for the repeat/wait loop?

raymondlewisjone 03-14-2008 11:07 AM

try this in your applescript: of course you will need to know your POSIX paths

do shell script "rm -Rf /path/to/the/file"

This will actually just delete the file from your machine without having to move it to the trash.

you could run this for every file you wish to be deleted.

duffman33 03-14-2008 11:22 AM

OK, I guess I need to be more specific. The exact problem I'm having is when the error comes up, it freezes everything until continue or stop is pressed on the dialog box. If I add the on error code, once I click one of the choices, it at least completes to script, but I still need to automatically get past this point if and when an error comes up.

About my code though: Basically I'm moving video files to the trash to delete, but occasionally the program using them hangs up and doesn't quit fast enough. I already have a delay, but adding in a ten minute delay is not practical as it only happens about half the time.

tw 03-14-2008 04:10 PM

Quote:

Originally Posted by duffman33 (Post 458037)
OK, I guess I need to be more specific. The exact problem I'm having is when the error comes up, it freezes everything until continue or stop is pressed on the dialog box. If I add the on error code, once I click one of the choices, it at least completes to script, but I still need to automatically get past this point if and when an error comes up.

About my code though: Basically I'm moving video files to the trash to delete, but occasionally the program using them hangs up and doesn't quit fast enough. I already have a delay, but adding in a ten minute delay is not practical as it only happens about half the time.

modal dialogs like that are a pain in the ykw. you really need to catch the problem before those things pop up. one suggestion might be to add a check to make sure the program(s) you use have quit before you empty the trash - something like:

Code:

tell application "System Events"
        repeat 600 times
                set proclist to every application process whose name is "name of app 1" or name is "name of app 2" or name is ...
                if (count of proclist) is 0 then exit repeat
                delay 1
        end repeat
end tell


duffman33 03-14-2008 10:02 PM

I have that, as more of a:

Code:

tell app "finder"
repeat while ((get name of every process) contains <app>)
end repeat
end tell

However, the problem with the video files is, even after the program is closed, it for some reason says they are still in use even though they are not. It happens seemingly without reason and the only thing I can think of is to somehow get the finder to empty the trash asynchronously. Other than that, I'm stumped.

tw 03-14-2008 11:36 PM

Quote:

Originally Posted by duffman33 (Post 458190)
I have that, as more of a:
Code:

tell app "finder"
repeat while ((get name of every process) contains <app>)
end repeat
end tell


wow - that will fry the Finder but good, as written. it's pretty much the equivalent of standing right behind the Finder and saying "AreYouDoneYet?AreYouDoneYet?AreYouDoneYet?..." as fast as you can while it's trying to close down your apps. throw in a delay command, and use system events rather than the finder, and I'll bet your problem goes away.

duffman33 03-15-2008 01:31 AM

The fact of the matter is is that that code works fine for my purposes, but like I said, it still comes up with the error for no reason. I still believe I need to figure out a way to asynchronously empty trash so upon an error, I can gui close the dialog box.

Mikey-San 03-15-2008 01:44 AM

Quote:

Originally Posted by duffman33 (Post 458230)
The fact of the matter is is that that code works fine for my purposes, but like I said, it still comes up with the error for no reason. I still believe I need to figure out a way to asynchronously empty trash so upon an error, I can gui close the dialog box.

Doesn't sound like it's working fine to me, if it's erroring and behaving unpredictably. The bit that tw quoted, for instance, basically chews your CPU like a muffin. Have you tried to implement what he said to see if your issue is resolved?

I don't understand why you aren't avoiding all of this by using a shell script to delete the items directly, like someone else mentioned. Is there a reason? If so, someone could likely help you get past whatever it might be and solve this problem with a bullet.

duffman33 03-15-2008 02:47 PM

I have tried to put in a delay and it doesn't affect it and it really shouldn't seeing as how the problems are unrelated since I just put in that code in the past day or two and actually haven't had any problems yet with it but until it happens I won't know for sure.

As far as shell script I put in what someone else put but it comes up with an error and stops the script. Here's what I have:

Code:

do shell script "rm -Rf " & fileloc
fileloc is the location of the folder to be deleted as a posix path. Is this wrong or what is going on here?

Mikey-San 03-15-2008 07:52 PM

My point was that you were still having problems and really burning the CPU so it didn't "work fine", not that tw's suggestion would work. I don't know if it would, but I didn't see a reason not to try it.

Quote:

As far as shell script I put in what someone else put but it comes up with an error and stops the script.
What is the error? No one here is psychic.

As for the fileloc variable, you need to protect the fileloc variable in case it contains spaces or characters that affect the shell interpreter. I also recommend using a /full/path to the shell command itself when using do shell script.

Code:

do shell script "/bin/rm -rf" & space & quoted form of fileloc
That is equivalent to:

Code:

you$ /bin/rm -rf '/some/file path/you want/to/delete'
Anyone using do shell script should read this document:

http://developer.apple.com/technotes/tn2002/tn2065.html

tw 03-16-2008 03:36 PM

Quote:

Originally Posted by duffman33 (Post 458310)
I have tried to put in a delay and it doesn't affect it and it really shouldn't seeing as how the problems are unrelated since I just put in that code in the past day or two and actually haven't had any problems yet with it but until it happens I won't know for sure.

duffman, part of the problem here is tracing down the problem. what "should" and "shouldn't" affect things has very little relation to what "does" and "doesn't" affect things in the real world, so it pays to think broadly.

the Finder has always had certain issues with applescript, particularly where applescript is trying to get it to do two things at once (you see this frequently in folder action scripts that contain Finder tells, for instance). all I was suggesting was that you take pressure off of the Finder by asking system events instead, and to throw in the delay loop to minimize cpu usage - that gives the Finder maximal resources to complete its tasks in time. it still may not work, yes, but it's worth a try. and if it doesn't work, we know the problem lies somewhere else.

duffman33 03-16-2008 06:41 PM

I understand, but I'm just frustrated because I have been troubleshooting this endlessly for over 2 months, and I think I understand the issue and it is a separate one from what is being discussed. I apologize for not being clearer, but I still think that unless the finder can be told to empty the trash ansynchronously, there is no fix.

In the meantime, I have been attempting to add shell scripting and I think I may have it working but I have had no time to test it out as of yet. I will keep you all posted.

Mikey-San 03-16-2008 07:12 PM

Well, it seems (to me) that you've been analyzing your problem incorrectly. Furthermore, the way in which you've mishandled the thread (no offense, honest) has only complicated your problem.

Quote:

I understand, but I'm just frustrated because I have been troubleshooting this endlessly for over 2 months
Doesn't this indicate to you that you've gotten yourself in the weeds? Why did it take you so long to ask for help?

Quote:

and I think I understand the issue
Two months of troubleshooting and the course of this thread indicate otherwise.

Quote:

and it is a separate one from what is being discussed. I apologize for not being clearer, but I still think that unless the finder can be told to empty the trash ansynchronously, there is no fix.
To be perfectly blunt, the focus of the thread has been all over the place because it's taken many posts to get to the crux of your problem. Don't blame the posters in this thread for not being able to read your mind or see your code.

Stop focusing on Finder. We are telling you to bypass the Finder completely for several reasons (performance of Finder, your attempted workaround's poor performance, unpredictability of Finder, etc). You can delete files very easily with System Events or a simple shell command.

duffman33 03-22-2008 03:23 PM

OK, finally I reproduced the error with the shell command I was told to use. Here is both the command and the exact error I encountered:

Code:

do shell script "/bin/rm -rf '/Users/bduffey2/Desktop/I_AM_LEGEND/'"
"Finder got an error: rm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/config.ini: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/discnav.ico: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/display.ini: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/dialog_bg.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/dialog_close.gif: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/dvdmainmenu.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/dvdmainmenu_down.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/dvdmainmenu_over.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_01.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_02.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_04.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_05.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_06.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_08.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/install_09.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/specialfeatures.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/specialfeatures_down.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images/specialfeatures_over.jpg: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/images: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config/install.htm: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//config: Directory not empty\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//JACKET_P/J00___5L.MP2: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//JACKET_P/J00___5M.MP2: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//JACKET_P/J00___5S.MP2: Permission denied\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND//JACKET_P: Directory not empty\rrm: /Users/bduffey2/Desktop/I_AM_LEGEND/: Directory not empty"

raymondlewisjone 03-28-2008 04:27 PM

try

do shell script "rm -Rf /Users/bduffey2/Desktop/I_AM_LEGEND/" with administrator privileges

this will prompt you for a username and password.

or

try

do shell script "rm -Rf /Users/bduffey2/Desktop/I_AM_LEGEND/" username 'bduffey2' password 'yourpassword' with administrator privileges

this will bypass the password dialog

you generally don't need to use the full path like "/bin/rm", the sbin and bin are the first places that executables are looked for. If "rm" was somewhere else, then you would need to.

raymondlewisjone 03-28-2008 04:28 PM

Oh, you should remove the "/" at the end of the path to the folder "I_AM_LEGEND"

duffman33 03-28-2008 06:22 PM

Thank you. Since I am using a changing location, however, i have the first part and then 'quoted form of loc' to represent my location to the folder. How would I put in the username and password part with that? I tried just adding it to the end of the line but Script Editor comes up with an error.


All times are GMT -5. The time now is 05:56 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.