The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Delete from Desktop using Applescript (http://hintsforums.macworld.com/showthread.php?t=56371)

rpaege 05-30-2006 03:24 PM

I have a little Applescript app I made that checks for then deletes a file from the Desktop:

Code:

tell application "Finder"
        activate
        if file "Macintosh HD:Users:user:Desktop:filename" exists then
                delete file "Macintosh HD:Users:user:Desktop:filename"
        end if
end tell


This works well however I need it to run in the background and have a quit menu, or automatically quit when i need to log out or restart. Can anybody tell me how to make my script behave that way, or a better way to have a file regularly deleted?

By the way, I tried using this as a Folder Action Script but it doesn't work. I have no idea why.


Thanks

ThreeDee 05-30-2006 04:39 PM

You could have this:

Code:

tell application "Finder"
        activate
                repeat
                        if file "Macintosh HD:Users:user:Desktop:filename" exists then
                                delete file "Macintosh HD:Users:user:Desktop:filename"
                        end if
                delay 5
        end repeat
end tell

It will keep repeating every 5 seconds, and you can use Dockless (http://homepage.mac.com/fahrenba/dockless/dockless.html) to make it a background app.

rpaege 05-30-2006 06:51 PM

Thanks for the reply!

I do use the "do shell script "sleep 30"" command to avoid using excess cpu time, and I save the script as "stay open." But whenever i log out or restart the app won't quit automatically and I have to either force quit it or use command-period.

Thanks for the tip about Dockless! I'll give that a try :)

james-aife 12-16-2008 05:44 PM

try to run applescript to delete user profile
 
I am very new to OSX,

i try these

code:
tell application "Finder"
activate
if file "Macintosh HD:Users:user:Desktop:filename" exists then
delete file "Macintosh HD:Users:user:Desktop:filename"
end if
end tell

but the files on the desktop still desktop, can you advise

asmeurer 12-17-2008 04:21 PM

You might want to try using Lingon to do this if you are running Leopard. You can easily set it to run when a file or folder changes, thereby keeping a load off your cpu. It uses the built-in launchd, which is what the OS uses to manage background processes and other things that open automatically. I don't know how well it would work with an AppleScript. If you know how, I would use a bash script. I only know a little about bash scripts, so maybe someone else can help whip up a simple one for you. Then again, an AppleScript may work just fine.

cwtnospam 12-17-2008 08:06 PM

Are you including the extension in the filename? Just to be clear, "user" should be your short username.

james-aife 12-17-2008 10:55 PM

I just type filename without any extension and "user" I did put the username.

Andreas~ 12-18-2008 11:04 AM

Quote:

Originally Posted by rpaege (Post 297353)
whenever i log out or restart the app won't quit automatically

Two things:

1. It is bad practice to hardwire a path. The script would fail, for instance, if you ran it on a clone. Also, for this purpose you shouldn't need to supply a path at all.

2. Using a repeat loop, you shouldn't have saved as "Stay Open" - the app will stay open as long as the loop doesn't exit. Also, your script contains nothing to tell a "Stay Open" app to close. Rather than complicating the loop, use an idle handler instead. Save the following as a "Stay Open" app...

Code:

on idle
    tell application "Finder" to if (exists file "dummy.rtf") then delete file "dummy.rtf"
    return 5
end idle

You don't need it as the script stands, but if you add anything to your script you might need to add a simple quit handler:
Code:

on quit
    continue quit
end quit


Andreas~ 12-18-2008 01:42 PM

Quote:

Originally Posted by james-aife (Post 508876)

if file "Macintosh HD:Users:user:Desktop:filename" exists then...

You don't need all that, but if you prefer a full path to what I posted earlier, use "path to desktop folder", e.g. try:

tell application "Finder" to if exists file ((path to desktop folder as string) & "dummy.rtf") then beep 3


You can see all the locations you can use with "path to" in Dictionary > Standard Additions.

To know whether you need an extension or not use Finder's 'Get Info' to see the full name. Whether an extension is 'hidden' or not is irrelevant.

james-aife 12-22-2008 06:05 PM

It was my mistakes, i am too influenced by MS DOS, i put the wrong file extension. Sorry guys

It script is works however, how do I select all files and all file extension type like MS dos *.* and folders on the desktop?

Please advice

Andreas~ 12-22-2008 08:05 PM

Quote:

Originally Posted by james-aife (Post 509840)
how do I select all files and all file extension type like MS dos *.* and folders on the desktop?

* is also the Unix wildcard, so you could use it in an AppleScript "do shell script".

In Script Editor you will learn a lot by browsing through dictionaries. A search in the Finder Dictionary for, say, "name", "extension", etc will show you all sorts of variations and coercions, for instance...

tell application "Finder" to get name of every file in desktop

or shortened to
tell application "Finder" to get name of files

will give a list of the names of all files on the desktop whether they have or don't have an extension, showing or hidden.

tell application "Finder" to select every file in desktop

or
tell application "Finder" to select files

will do the obvious. Be aware, though, that selecting an item before applying a command to it is very bad practice - if you can identify the item(s) sufficiently to select it/them you can work on it/them directly without selecting it/them first.

james-aife 12-29-2008 07:09 PM

Thanks guys , I have finally understand how applescript works and I think Apple is better, after learning how to operate Imac and certain modification.

I modify these script and it works perfectly.


tell application "Finder"
try
delete (every item of folder "Desktop" of folder "user" of folder "Users" of folder "Macintosh HD")
end try
try
delete (every item of folder "Documents" of folder "user" of folder "Users" of folder "Macintosh HD")
end try
try
delete (every item of folder "Downloads" of folder "user" of folder "Users" of folder "Macintosh HD")
end try
empty trash
end tell


Andreas~ 12-29-2008 08:24 PM

You're coming along. You didn't notice, though, that in my previous posts I showed you that
Code:

items in desktop  and

every item in desktop

are equivalent to your

every item of folder "Desktop" of folder "user" of folder "Users" of folder "Macintosh HD"

Likewise
Code:

items in (path to documents folder from user domain)    and

items in folder "Users:<shortname>:Documents" of startup disk

are the same as your

every item of folder "Documents" of folder "user" of folder "Users" of folder "Macintosh HD"

(Look in the 'Standard Additions' dictionary for details about "path to").

BUT it's not just a question of being shorter - it's also (as I told you earlier) that "hard wiring" (giving the absolute path) is bad scripting. You should NOT use the name of a volume, or that script will fail if run from elsewhere or will do something you didn't want. ...and you might decide one day that you're tired of that Oh-so-boring name, "Macintosh HD" and change it. Have to modify all your scripts - Oh dear! :)

Study the dictionaries
. Have fun!

james-aife 01-04-2009 10:01 PM

How to faster the OSX logon to Window 2003 domain
 
Anyone of you have any ideal faster your logon into the WIndow 2003 domain.

james-aife 01-06-2009 05:35 PM

Quote:

Originally Posted by james-aife (Post 511442)
Anyone of you have any ideal faster your logon into the WIndow 2003 domain.

We have few IMAC in the organisation with 1600 AD users who needs to use IMAC and a lot user folder will be created and need to find a way to auto delete when the user shut down or logoff IMAC to make sure the disk will not be full of users folders.

On the other hand we need to join the AD domain, I have blind the OSX with AD but I can't logon to AD with my domain user account. Please advise

Andreas~ 01-06-2009 06:12 PM

James

The questions you are now asking bear no relation to the title of this thread. If you want to get the attention of those who may be able to help you, start a new thread.
=-=-=-=-

You didn't respond to my last post, so obviously it was of no use to you. Sorry.

james-aife 01-11-2009 05:27 PM

Sorry Andreas for the previous questions, can you assist me how to write a apple script, if the current user is not equal to admin and certain users in Mac User profile then delete the user's folder (user profile) i wrote a script and obviously it does not work, please bear with me
Quote:

tell application "Finder"
if current user exists
then

try
delete (current user of folder "Users" of folder "Macintosh HD")
end try


empty trash
and if

end tell

Andreas~ 01-13-2009 09:19 AM

James, you won't like my answer...

I will NOT offer you any help with doing what you are trying to do because it is HIGHLY DANGEROUS. You are heading towards locking yourself out of your own Mac. Be thankful that your bit of code is gibberish and cannot work.

Sorry, but I draw the line at helping to assist a suicide! Apple supplies you with System Preferences > Accounts to make alterations to accounts in a SAFE way.

Once again I see that you have taken no notice of my repeated advice NOT to use the name of a volume in a path. James, a good rule to follow is not to ask for advice from anyone whose advice you don't want to take! :)

tlarkin 01-13-2009 10:09 AM

I would do something like this...

Code:

#!/bin/bash

#this will delete all files from the desktop, you must put specific file
#names if you want specific files gone
#if you wish to clear out specific directories please add them to the list

list="$HOME/Desktop/*"
     


for file in $list
do
        if [[ -e $file ]]
        then
                rm -rf $file
        else
        echo "no files found"
        fi
done
exit

I haven't tested it so it may need tweaking, I would replace rm -rf with ls -al at first to test it out before you start deleting things. Then you can make this executable and run it from the GUI if you wanted to.

Also my syntax can be slightly off I am just now starting to teach myself looping.

Also, James, deleting the user's folder does not delete the user, they are still in the directory services BSD database on the machine locally. If you want to script out deleting accounts you would have to use the dscl command.

asmeurer 01-13-2009 01:07 PM

Why don't you just use the guest account. I am assuming that you are using Leopard.

Hal Itosis 01-13-2009 03:56 PM

Quote:

Originally Posted by tlarkin (Post 513093)
Code:

list="$HOME/Desktop/*"
for file in $list
do
        if [[ -e $file ]]
        then
                rm -rf $file
        else
                echo "no files found"
        fi
done

I haven't tested it so it may need tweaking,

Just call me the tweaker!

While that double-bracketed test [[ -e $file ]] eliminates the need to quote the $file variable,
elsewhere in the do loop quotes will likely be needed. So rm -rf $file should be rm -rf "$file"
OTOH, the quotes where list was defined aren't needed: list=$HOME/Desktop/*

Of course, that script (as written anyway) simply deletes everything there,
so it could be shortened by eliminating the looping and testing altogether:

rm -rf ~/Desktop/*

but that won't notify us if the Desktop was empty (of visible items).

tlarkin 01-13-2009 04:07 PM

Quote:

Originally Posted by Hal Itosis (Post 513197)
Just call me the tweaker!

While that double-bracketed test [[ -e $file ]] eliminates the need to quote the $file variable,
elsewhere in the do loop quotes will likely be needed. So rm -rf $file should be rm -rf "$file"
OTOH, the quotes where list was defined aren't needed: list=$HOME/Desktop/*

Of course, that script (as written anyway) simply deletes everything there,
so it could be shortened by eliminating the looping and testing altogether:

rm -rf ~/Desktop/*

but that won't notify us if the Desktop was empty (of visible items).

ok tweaker

Ah you are right the only time I need to quote it is if there are multiple paths to a variable...I think.

Hal Itosis 01-13-2009 11:18 PM

Quote:

Originally Posted by tlarkin (Post 513198)
the only time I need to quote it is if there are multiple paths to a variable...I think.

More along the lines of: "are there spaces in any file's pathname, or not?"
Essentially, we should *always* ensure they are accounted for... somehow.

In the [[ ]] form of test, the shell provides quoting behind the scenes.
Another method is using the ability of some commands to append null
chars (or accept null chars) as the 'filename delimiter'. Most common
being: find blah blah -print0 |xargs -0 blah

We also had a thread somewhere where we removed the 'space' from
the IFS variable (so that newlines became the main delimiter).

fordf1 12-08-2009 04:41 AM

tell application "Finder"

try

delete (every item of folder "Desktop" of folder "user" of folder "Users" of folder "Macintosh HD")

end try

try

delete (every item of folder "Documents" of folder "user" of folder "Users" of folder "Macintosh HD")

end try

try

delete (every item of folder "Downloads" of folder "user" of folder "Users" of folder "Macintosh HD")

end try

empty trash

end tell

i like this script, how can i modify it to select all user except administrator to replace "user" in this scriopt.

fordf1 12-08-2009 05:14 AM

how to select all users except administrator without having to type all the users name under "user" in this script?


All times are GMT -5. The time now is 02:11 PM.

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.