How to change file name every time a script is run?

Discussion in 'Mac Apps and Mac App Store' started by celticpride678, Jan 17, 2010.

  1. celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #1
    I am trying to setup a script where a screenshot is performed. However, every single time the script is run, the file name is the same, not allowing it to save. How can I change file name every time the script is run? Thanks

    Current Script: do shell script "screencapture -i -s -tjpeg ~/Desktop/1.jpg"
     
  2. Zyniker macrumors 6502

    Joined:
    Feb 14, 2008
    #2
    Any particular reason you're forcing the script to require you to interact with the screen capture?
     
  3. Zyniker macrumors 6502

    Joined:
    Feb 14, 2008
    #3
    You need to define a variable and then use it as your filename:

    CAPTURE_FILE_NAME=`date "+%Y_%m_%d_%H%M.jpg"`
    /usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME

    Should work. Feel free to remove /usr/sbin/...it should still work without it.
     
  4. celticpride678 thread starter Guest

    celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #4
    I am getting a "Expected expression found but found unknown token" message on the symbol, '
     
  5. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #5
    Are you getting the error punching the lines into Terminal, or from AppleScript? It looks like AS is burping on the quote marks. I believe this works:

    Code:
    set littleBit to quote & "+%Y_%m_%d_%H%M.jpg" & quote
    
    set biggerBit to "CAPTURE_FILE_NAME=`date " & littleBit & "`; /usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME"
    
    do shell script biggerBit
    mt
     
  6. celticpride678 thread starter Guest

    celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #6
    It is working, but not changing the file name still. It uses the same file name, but changes the screenshot taken. I am using AppleScript
     
  7. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #7
    What do you want the screenshots to be named?

    The script should provide different names, if only because one second passes between each screen shot. But if you want a different name, we'll work on that.

    Also, your original script seemed to do command-shift-4, where you can select a region of the screen for the screen shot. My script, based on Zyniker's, does a simpler, command-shift-3. Do you want a specific region or the whole screen?

    mt
     
  8. celticpride678 thread starter Guest

    celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #8
    I'd be OK with the date and time of the screenshot as the name. I would like a different name. I'd also like a specific region (but if needed, I can code that part). Thanks for the help so far.
     
  9. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #9
    This might be more what you're after.

    Code:
    set any_old_string to the text returned of (display dialog "Please enter any old string for me" default answer "" default button 2)
    
    set littleBit to quote & any_old_string & ".jpg" & quote
    
    set biggerBit to "/usr/sbin/screencapture ~/Desktop/" & littleBit
    
    do shell script biggerBit
    The display dialog line is merely to provide a string. You can create one in your script, maybe through a repeat loop or some other way, put quotes around it then pass it to "screencapture" with a proper path.

    mt
     
  10. celticpride678 thread starter Guest

    celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #10
    THANK YOU! That is EXACTLY what I'm looking for! Thanks for all the help.
     

Share This Page