Get rid of desktop icons

Hi,

I’m using a short script which activates Toast & burns DVD’s for me . . . I have two problems which are posing small problems, so as a novice AppleScripter, I’d like to ask for some help with these individually.

The first issue relates to icons which Toast seems to leave behind.

After burning several DVD’s I notice that (sometimes) Toast leaves several “Untitled DVD” icons on the desktop. I’d like to ensure that if these are present, the script checks for these and removes them on each script activation.

I’ve been checking these forums for something like:


Tell finder
find files of type (.xxx) on (path to desktop)
erase & empty trash
end tell

Please excuse the scripting, as I don’t have my reference book with me while writing this post!

Any help with this first issue would be very much appreciated.

Many thanks in advance.

Try this…

set desktopPath to path to desktop folder

tell application "Finder"
	set dItems to items of desktopPath
	repeat with anItem in dItems
		if name of anItem contains "Untitled DVD" then
			move anItem to trash
		end if
	end repeat
end tell

display dialog "Do you want me to empty the trash for you?" buttons {"Cancel", "Empty Trash"} default button 1 with icon 2
if button returned of result is "Empty Trash" then
	tell application "Finder" to empty trash
end if

Hi,

Wow that was a quick response, and the script works perfectly!

Many thanks.

Just had a look at your site, some very interesting software there, I’ll be giving some if it a go in the coming weeks.

Thanks again.

The Finder, of course, has its own ‘desktop’ keyword and can do things in bulk:


tell application "Finder" to delete (items of desktop whose name contains "Untitled DVD")

In fact, the desktop’s the default location for Finder items so you could leave the word out:


tell application "Finder" to delete (items whose name contains "Untitled DVD")

. but it’s probably better practice to be explicit.

Thanks Nigel,

I’d like to ensure the end script is as streamlined as possible, and will try to integrate your idea’s into it.
Much better to have less than more I suspect, but at this stage I’m a novice and am experimenting with things to see how the jigsaw fits together.

One issue I’m coming across, and have yet to work out, is the path problem, where I can get my script to work (partially at this stage) on my own machine, but I’m not understanding the scripting sufficiently enough to work out how to make the script work on any machine (though this may work to my advantage I guess)!

Thanks for the input, it’s fascinating trying to get this to work!

Cheers

That’s why I wrote my script the way I did. For beginners I like to show a step at a time so they can learn techniques that will work in almost all situations. For Nigel I certainly would have written the script differently. The desktop path I showed will work in any application. The repeat loop too although it’s even safer to use “repeat with i from 1 to (count of something)” so I probably should have shown that instead. When I began “whose” filters were hard for me to get the hang of because they worked in some applications and not others. Actually I still struggle with them sometimes.

Hi, Steevg.

The Finder has the keywords ‘startup disk’, ‘home’, and ‘desktop’, which refer respectively to the startup disk of the machine on which the script’s running, the home folder of the user in which the script’s running, and the user’s desktop folder.

Hank used ‘path to’, which is a command from the StandardAdditions OSAX (part of the standard AppleScript installation). That has a lot more options, on which I won’t give a full tutorial here! :wink:

Both save the scripter having to know the names of the startup disk and user in advance. Feel free to ask again when you get to that point. :slight_smile:

Hank, Nigel,

Thanks so much, using these forums & posts are a whole lot more intuative than ploughing through my Applescript book.

Most of this makes sense, I just need to do some testing to get the hang of things.

Thanks a bunch guys, you’ve both been very helpful!
I’ll post back here if I have any further questions.

Cheers
Steve

The fastest will of course be with a shell script:

(Save and chmod 0755 it)
Move items to trash:

[code]#!/bin/sh

mv ~/Desktop/Untitled\ DVD ~/.trash[/code]
Delete immediately:

[code]#!/bin/sh

rm ~/Desktop/Untitled\ DVD[/code]
Of course this is already a little bit advanced

Hope it helps,
ief2

ief2,

Thanks for the input . . . . as a novice I’m aware of chmod, but have no idea how I’d implement this into a script at this stage of my learning I’m afraid.

Would you be able to go through the steps I need to take to insert the shell scripts you describe.

Cheers,
Steve

Well, the paths contain stars, so that wouldn’t work in AppleScript’s do shell script.
But you can list a folder’s contents without the help of any other application, and process the list

on run
	set desktopFolder to path to desktop folder
	set desktopContents to list folder desktopFolder
	
	repeat with i in desktopContents
		if i contains "Untitled DVD" then
			set fullItemPath to (desktopFolder as text) & i
			do shell script "mv " & quoted form of POSIX path of fullItemPath & " ~/.trash" -- move to trash
			--do shell script "rm " & quoted form of POSIX path of fullItemPath -- delete immediately
		end if
	end repeat
end run

Hope it helps,
ief2

PS: In the script, unquote one of the do shell script-lines.

ief2

Great, thanks for the info . . . . will try to break it down & understand what’s going on first!

Are you able to offer any help with my other post?
http://macscripter.net/viewtopic.php?id=33873

Thanks for your help.