Skip navigation

Simple Applescript help... maybe.

713 Views 13 Replies Latest reply: Sep 25, 2011 4:37 PM by Trey RSS
Trey Calculating status...
Currently Being Moderated
Aug 26, 2011 12:50 PM

Ok, I've got this little Applescript that I use on the comptuers in my shop to clean off the desktop at login:

 

tell application "Finder" to move (items of desktop whose size is less than 3.0E+9) to trash

 

It works great! The only thing is that if there's a CD or flash drive mounted (I let those show up on the desktop) the script will fail with an error that the volume couldn't be deleted.

 

Is there a way I can have the script ignore the volumes?

 

In addition, how would I get the script to move the items to another volume instead of the trash? I've tried doing it as "Volumes:destination" but it doesn't seem to like that.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 26, 2011 1:30 PM (in response to Trey)

    change 'items' to 'files':

     

    tell application "Finder" to move (files of desktop whose size is less than 3.0E+9) to trash

     

    if you want to delete folders as well, you can do that in a separate step.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 26, 2011 2:04 PM (in response to Trey)

    yes, that should delete everything except disks.  If you want to be fancy about it, you could do this:

     

    tell application "Finder" to delete (items of desktop whose size is less than 3.0E+9 and class is not disk)

     

    Note: I haven't tested that (for obvious reasons).

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 26, 2011 9:01 PM (in response to Trey)

    The Finder is a bit picky about paths - you can't use POSIX paths, for instance.  the following ought to work (assuming the volume is mounted on your machine):

     

    tell application "Finder" to move (items of desktop whose size is less than 3.0E+9 and class is not disk) to (folder "X" of disk "Q")

     

    In the future, it's easier to debug things if you show what you did.  Saying "just change the "trash" part to the path of the volume" isn't too helpful, because there are a number of subtle errors you could make specifying the path, and I can't tell if they are happening if I can't see your actual code.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 30, 2011 11:10 AM (in response to Trey)

    Are you working in Lion or an earlier OS? It may be different in Lion (I'll have to check) but I think when you are doing bulk tranfers like this you only have the option of replacing the files or aborting the move.  if you want fine-grained control then you need to write a more complex script (one which goes though all the files on the main folder one-by-one and sees if there are conflicting file names in the destination folder).  Not difficult, just more work.

     

    If you want to replace duplicate files, add 'with replacing' to the end of the move command. if you want the command simply to abort, put the move command in a try block:

     

    try

        move ...

    on error

        return

    end try

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 31, 2011 8:37 AM (in response to Trey)

    to your first point, yes.

     

    To your second point: I assume from what you've said that you've saved this script as an application? If so, this is probably normal first-run behavior; any application that is not built on the machine itself will be treated as suspicious and generate that "are you sure?" message.  run it, click OK; run it again, should be seamless. If that's the case, then there's no getting around it.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Aug 31, 2011 10:49 AM (in response to Trey)

    somehow you managed to save it with the 'Startup Screen' checkbox checked (perhaps that's on by default in Lion).  open it in Lion's applescript editor, and resave it making sure the startup screen checkbox is off.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.