Move one file only with Applescript

Discussion in 'Mac Programming' started by vloryan, Oct 1, 2014.

  1. vloryan macrumors member

    Joined:
    Jan 11, 2014
    #1
    Hi,

    I have an folder full of PDFs (located at "/Users/johndoe/Desktop/xyz") and would like to move any PDF (only one file!) to another folder (located at "/Users/johndoe/Desktop/goal").

    Would be great if you could help me here!

    Thanks!
     
  2. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #2
    I'm not sure what you mean by "one file", but this should move all PDFs in that folder.

    do shell script "mv ~/Desktop/xyz/*.pdf ~/Desktop/goal/"

    If there is just one particular PDF you want to move change the * into the file name.
     
  3. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #3
    There's a command line that will do it

    Code:
    mv `ls /Users/johndoe/Desktop/xyz*.pdf | tail -1` /Users/johndoe/Desktop/goal
     
  4. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #4
    With "one file" I mean one random file out of more than fifty pdfs... thanks!
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #5
    Are you asking how to choose a file randomly?

    Or are you asking how to move a single chosen file?
     
  6. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #6
    sorry for beeing not very specific. i'd like to know how to move one file only.
     
  7. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #7
    The command line option I gave is pretty much random and only copies one file. You could mix it up more by changing sort options on the ls command.
     
  8. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #8
    @JustMartin / That works great as long as the filename does not contain an empty space character. Any ideas how to solve this? Thank you so much!
     
  9. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #9
    In the command line when there is an empty space you need to use the escape character.

    So you would enter...

    Code:
    shopping list.pdf
    as

    Code:
    shopping\ list.pdf
     
  10. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #10
    I do not use specific filenames, but "*.pdf" in my command line.
     
  11. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #11
    try putting a " around the phrase in back ticks

    Code:
    mv "`ls /Users/johndoe/Desktop/*.pdf | tail -1`" /Users/johndoe/Desktop/goal
     
  12. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #12
    You should use
    Code:
    set myFile to "any file i want.pdf"
    set src to "/Users/johndoe/xyz/" & myFile
    set src to "/Users/johndoe/goal/" & myFile
    
    do shell script "mv " & (quoted form of src) & " " & (quoted form of trg)
    
     

Share This Page