How could I create a script or "something" like this…?

Discussion in 'OS X Mountain Lion (10.8)' started by iRock1, Jun 1, 2013.

  1. iRock1, Jun 1, 2013
    Last edited: Jun 2, 2013

    macrumors 6502a

    iRock1

    Joined:
    Apr 23, 2011
    #1
    So basically the idea is to have a keyboard shortcut to open the latest downloaded file (in the Downloads folder). What's the simplest way of doing that? :confused:

    Thanks in advance!
     
  2. macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #2
    I guess you could do it with Applescript.

    Something like getting the latest downloaded file, opening with the default application and finally assigning a shortcut.
     
  3. macrumors 68000

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #3
    Automator can accomplish this. Just set up the steps in a service, save it, and then assign a key combination to it in the keyboard settings of System Preferences.
     
  4. macrumors P6

    ~Shard~

    Joined:
    Jun 4, 2003
    Location:
    1123.6536.5321
    #4
    Exactly, just play around with Automator to accomplish this - it's not used by many users frequently but is quite powerful in the right circumstances and can make the automation of tasks such as this much easier.
     
  5. thread starter macrumors 6502a

    iRock1

    Joined:
    Apr 23, 2011
    #5
    Sorry but how could I work this out? I don't know anything about programming so I don't know how to put these two texts together.

    Code:
    tell application "Finder"
        set latestFile to item 1 of (sort (get files of (path to downloads folder)) by creation date) as alias
        set fileName to latestFile's name
    end tell
    Code:
    do shell script "open " & dstfile
    In other words, how should the entire text in AppleScript look?

    I know it's a great tool, but honestly sometimes it looks quite difficult to use. For instance in this particular case, which actions would you chose and in which order? :S
     
  6. macrumors 68000

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #6
    It's actually very easy to use but after looking in to it, it looks like Automator won't be able to do this for you. You would use the "Find Finder Items" and "Open Finder Items" actions in that order, but unfortunately the "Find Finder Items" can't find items that are the newest in a folder. It can search by date created, but only with a date. There appears to be no way to tell it to open the most recent file, unless I missed something.

    So it does look like you'd be better off using AppleScript but unfortunately I'm not familiar with it so I won't be able to help you any further. Good luck.
     
  7. macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #7
    I never used AppleScript before, but then I realized it's pretty simplistic, basically, you'd put this into an AppleScript (use built-in OS X Editor an "Run" to test)
    Code:
    tell application "Finder"
    	set latestFile to item 1 of (sort (get files of (path to downloads folder)) by creation date) as alias
    	set fileNameP to POSIX path of latestFile
    end tell
    
    do shell script "open " & fileNameP & ""
    
    And then read the third link I shared and follow instructions to assign a keyboard shortcut.
     
  8. iRock1, Jun 3, 2013
    Last edited: Jun 3, 2013

    thread starter macrumors 6502a

    iRock1

    Joined:
    Apr 23, 2011
    #8
    Thanks, it works great most of the times. The problem is that the script should consider the date when the file was "added", not "created". Otherwise it works randomly.

    Sadly I don't know how to give that instruction to AppleScript (I tried replacing "creation date" by "addition date", lol), and neither it's on the AppleScript Dictionary.

    The only thing that I found was this: http://macscripter.net/viewtopic.php?id=37910. So I learn that there is no simple way of making this particular property accessible from AppleScript. Then I got lost in the middle of the thread. :(

    BTW I've never understood the difference between sorting files by date added and date created…
     
  9. ipsychedelic, Jun 3, 2013
    Last edited: Jun 3, 2013

    macrumors 6502a

    ipsychedelic

    Joined:
    Mar 8, 2012
    Location:
    Cali, Colombia
    #9
    I don't remember quite correctly but that was one of the reasons I ditched Finder completely and found Path Finder.
    Basically, one is the date the file was created on the remote server (i.e where you downloaded the file from), and seems to be the one Finder enjoys sorting by.
    So I had a lot of downloaded files and no way to tell when I exactly downloaded them as they showed dates years ago (when they were created).

    You can see solutions to this problem here and here, or even here.
    So, instead of playing around with the piece of script I pasted you could just "fix" the problem from the root.

    If you wanna play with Applescript like you did, then something like this may help, but it's not for the faint of heart (but in your case, shall you do what's mentioned in the last paragraph, this right here would mean the solution for all files in your download folder, not just the ones you add from now on; now meaning whenever you implement what is instructed in the last paragraph).

    PD:
    In a broad sense, I'd explain what you ask like this:
    I create a file and upload to a server (apple.com/iTunesInstaller.dmg). Creation date is 13-Dec-2012.
    Then you download it today. Modification date is probably 03-Jun-2013, since the download process involved adding some attributes (read, write for the current user), hence the file was modified.
    Added date is I think, where it got added to the filesystem so it should be the same as Modification date.
    When you modify the file tomorrow, then Creation and Added date will stay the same, but Modification date will change.
    There is also Last accessed date, which is where you last opened the file (?).

    Anyway, its confusing and I don't give it too much thought, all I know is that I sort my files by Modification date always.

    Please notice that some servers may or may not return the original attributes of the file (it also depends on how you copied the file to the remote server) so Creation date not always means the real creation date of a given file.
     

Share This Page