Applescript Monday: Make a Clear Desktop Script

by Clay Moore on March 15, 2010

in AppleScript Mondays,Computer Language,programming

Often times with programming training it takes for ever for the course to get somewhere where we can do something practical. That’s why I like to do something practical, as soon as possible. So with what we know so far what can we do? I think we need a script to clear our desktop of all the files that collect there. So. after the break, we will make a clear desktop script.

There’s a couple of Applescript concepts I want to introduce. The first is object types. These are the words in Applescript that are the large type words like file and folder. File and folder in Applescript represent all the files and all the folders. All files in any folder can be considered to be generic files. The same can be said of the generic object folder which represents all folders.

The next concept is that of enumerations. Enumerations are words that represent quantities. Right now I am interested in the word that represent all of something. In Applescript this word is every. If I want to work with all the files or folders I use every in front of the object type. To select all files I would use the phrase every file or every folder.

Now I need the verb that will move the files from the desktop to a folder of my choice. I could probably guess it, but last week I showed you how to find the verbs that an app exposes to Applescript. So, Startup Applescript editor and take a gander at the Finder dictionary. This is the link to the post which showed you how to do that. Look inside the standard suite for the command. Looks like move is the verb I want.

Screenshot2010-03-14at5.51.25PM.MLIKu65jvFJD.jpg

So our command is “move every file.” That’s still not enough we have to have a from and a to. We can use the prepositional phrase “of whatever” To indicate our from. We are moving from Desktop to some other location. The other location needs to be in your user’s folder, the one with your user’s name on it. Create a folder there called From Desktop. We now have our destination.

The full phrase looks something like “move every file of desktop to the folder “From Desktop” of home.” Notice that we used words like Desktop and home. These are the actual containers we are using. Desktop is that screen, and home is the folder with your name on it. Since we also want to make this script work with the folders we will need a second command to work on the folders in the desktop. Fortunately it is the same command as for the files but with the word folders instead of files.

Screenshot2010-03-14at5.36.52PM.uBB2M0tZnBBj.jpg

Because we want the Finder to execute the commands we will tell the Finder to do it. And both command inside of the tell block. Like the screen shot above.

Next week we will make the above script even more useful. Try this out and then go to Library>>Scripts and save this script there as Clear Desktop. That command should appear in the general script menu. Now you can use it all the time.

Share

Related posts:

  1. AppleScript Mondays: Extending the Clear Desktop Script
  2. Very Basic AppleScript
  3. AppleScript Mondays: Let’s Open a dictionary
  4. How to make a blog ideas database with EagleFiler
  5. Desktop Computers: A Reevaluation

{ 2 comments… read them below or add one }

Perry Faciana February 24, 2011 at 2:32 pm

Great post, but I’m having a different issue with select all. I’m a photographer and am trying to write a script to manipulate images through a process. I’m stuck at a point where I can manually make the script react, but like it to automatically react when a new image is written to a specific folder. Here is what I have so far:

set this_file to choose file without invisibles
set the target_length to 800
try
tell application “Image Events”
– start the Image Events application
launch
– open the image file
set this_image to open this_file
– perform action
scale this_image to size target_length
– save the changes
save this_image with icon
– purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try

I think I’m close if I can get the first line of code corrected. Any assistance would be appreciated

Perry

Clay Moore March 30, 2011 at 8:28 am

To make a script react when a new file is added to a folder you would need to make a folder action and run your script as a script action in the workflow. Hope this helps

Leave a Comment

*

{ 2 trackbacks }

Previous post:

Next post:

<