Help: Basic Applescript to move files from desktop

Discussion in 'Mac Programming' started by Fluke?, Jun 28, 2010.

  1. Fluke? macrumors newbie

    Joined:
    Feb 11, 2009
    #1
    Hi all,
    An Applescript novice here I'm afraid. I'm looking to create a basic script that I can run at login that move all documents (.pds/jpeg/mov etc etc) from the desktop to a folder within Documents.

    What happens is that the students constantly save work onto the desktop rather than Documents, meaning keeping tabs on thier work, and general state of the macs is a nightmare.

    I have previously tried locking the desktop but this causes problems mounting disks and dvd burning so want to avoid this if possible.

    I have tried the following scipt but get a "Handler can't handle objects of this class" Applescript error.

    PHP:
    tell application "Finder"
        
    move every file of folder "New Mac HD:Users:college:Desktop:mac scripts"
        
    move to folder "New Mac HD:Users:college:Desktop:testmove"
    end tell
    Thanks for any help guys :)
     
  2. andmr macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #2
    You might try this:

    Code:
    tell application "Finder"
    	set a to folder "New Mac HD:Users:college:Desktop:mac scripts"
    	set b to folder "New Mac HD:Users:college:Desktop:testmove"
    	move every file of a to b
    end tell
    Hope this helps.
     
  3. Fluke? thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #3
    Many thanks Andmr, that does work.
    The other couple of bits I would like it to do (and failing on) are as follows:

    1. I have an alias to the Documents folder on the desktop, I would ideally like to add a rule to bypass this folder, or otherwise recreate this alias. I have managed to recreate this folder but if there is a way of not having to move it in the first place that would be great.

    2. I have seen how to create a new dated folder each DAY the script is run. This avoids the error of duplicating the same files to the same folder.
    The problem I have is that often the Macs will be logged into many times in one day and the script errors with "Finder got an error: The operation could not be completed because there is already an item with that name."
    Is there a way of creating a new folder each time this happens, perhaps with the time as well as date, or even with a (1), (2) etc at the end of the folder name?


    This is what I have so far:

    PHP:
    set foldername to ("Desktop " short date string of (current date))
    set docsfolder to ("New Mac HD:Users:college:Documents") as string

    tell application 
    "Finder"
        
    if not (exists folder (docsfolder foldername)) then
            make 
    new folder at docsfolder with properties {name:foldername}
        
    end if
        
    set a to folder "New Mac HD:Users:college:Desktop"
        
    set b to folder "docsfolder"
        
    move every file of a to b
    end tell

    tell application 
    "Finder" to make new alias at "New Mac HD:Users:college:Desktop" to folder "New Mac HD:Users:college:Documents"
    The two problems I have with the above script are (1)The error with creating a new folder if there is already one with the dame date. (2)I get the error but have no idea why
    Code:
    "Finder got an error: Can't get folder "docsfolder"
    To recap for the TL: DR amongst us, what I want to do is as follows :p

    - move all files from the desktop, except the Documents alias
    - create a new dated (and pos time) folder each time the script is run where the files will be moved to.

    That's it!

    Sorry if this post seems a bit dis-jointed, it has been written over a period of a few days at the same time as massively failing at Applescrpt.

    Many many thanks again if anyone as the time to help!
     
  4. Fluke? thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #4
    Hi, I think I have nailed this script in the most part with a little outside help.
    I am only having one problem now and that is duplicate files/file names, where I get the error
    Is there something like "on error resume next" I can use here? Or to be able to add a (1) to the end of the duplicate file and copy that?

    My current script:
    PHP:
    college-- This section deletes the Documents folder from the desktop
    tell application 
    "Finder" to delete alias "New Mac HD:Users:college:Desktop:Documents"

    -- This section creates a new dated folder in Documents and moves all files and folders from the desktop to the new folder
    set foldername to 
    ("Desktop " short date string of (current date))
    set docsfolder to ("New Mac HD:Users:college:Documents") as string

    tell application 
    "Finder"
        
    if not (exists folder (docsfolder ":" foldername)) then
            make 
    new folder at docsfolder with properties {name:foldername}
        
    end if
        
        
    set a to folder "New Mac HD:Users:college:Desktop"
        
    set b to folder (docsfolder ":" foldername)
        
    move every file of a to b
        move every folder of a to b
    end tell

    -- This section adds a new Documents folder to the desktop
    tell application 
    "Finder" to make new alias at "New Mac HD:Users:college:Desktop" to folder "New Mac HD:Users:college:Documents"

    -- This section closes all open windows on the desktop
    tell application 
    "Finder" to close every window

    -- This section reset the desktop background
    tell application 
    "Finder"
        
    set desktop picture to "new mac hd:library:desktop pictures:flow 2.jpg"
    end tell
    Thanks again!
     

Share This Page