Applescript quick question

Discussion in 'Mac Programming' started by Wowzera, Aug 16, 2009.

  1. Wowzera macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #1
    Hello,
    I want to make a script that will prompt the user to select two files and copy them to a pre-created folder on Desktop called TEMP. After it I want them to be renamed to a name that I will define later.

    This is my code until now: (pretty poor I know)

    EDIT:
    Btw, isn't there a way to instead of the user select the files he wants, he just drop them on a window? (whose window will check if the files are ZIP)?
     
  2. Wowzera thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #2
    Ok, I've figured out how to do it.

    Here goes another question:

    How can I rename files?

    I'll always have four files on a folder (specified location, let's say ~/Desktop).
    Two RAR files and two ZIP files. I need to rename ONLY the ZIP files.
    So it will do the following:

    If the files extensions are .ZIP --continue

    The file with the bigger size* get renamed to 'heavy'
    The other file will be renamed to 'thin'.

    *Can be biggest size, alphabetically or anything else, it does not matter, I just need the two ZIP files to be named differently one 'heavy' and the other 'thin'.
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    To change a file name, here's a basic shell:

    Code:
    tell application "Finder"
    	set theFileAlias to (choose file)
    	set the name of theFileAlias to newFileString
    end tell
    
    (This will break because I haven't defined "newFileString"; I'm assuming you have some code already to handle what you want to name the file.)

    Just remember, you can't change the name of a file itself, but you can to an alias; the Finder treats it like a property.

    mt
     
  4. Wowzera thread starter macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #4

    Thanks.
    This is what I did:
    Code:
    set largestFile to do shell script "cd ~/Desktop/Temporary; ls -S `pwd`| head -1"
    	set smallestFile to do shell script "cd ~/Desktop/Temporary; ls -Sr `pwd`| head -1"
    	
    	set filePathLargest to (path to desktop folder as string) & "Temporary:" & largestFile
    	set filePathSmallest to (path to desktop folder as string) & "Temporary:" & smallestFile
    	
    	set name of file (filePathLargest as string) to "regular.zip"
    	set name of file (filePathSmallest as string) to "complex.zip"
    Looks weird but did the job,
    thanks for your help :)
     
  5. pcwiz macrumors member

    Joined:
    May 28, 2008
    #5
    Renaming files can easily be done with do shell script:

    Code:
    do shell script "cd /path/to/files; mv oldfile newfile"
     

Share This Page