![]() |
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. |
Code:
try |
You can put your empty trash in a try statement to catch the error:
Code:
try |
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.
|
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 |
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?
|
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() |
Quote:
|
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. |
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. |
Quote:
Code:
tell application "System Events" |
I have that, as more of a:
Code:
tell app "finder" |
Quote:
|
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.
|
Quote:
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. |
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 |
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 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 filelocCode:
you$ /bin/rm -rf '/some/file path/you want/to/delete'http://developer.apple.com/technotes/tn2002/tn2065.html |
Quote:
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. |
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. |
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:
Quote:
Quote:
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. |
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/'" |
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. |
Oh, you should remove the "/" at the end of the path to the folder "I_AM_LEGEND"
|
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.