PDA

View Full Version : How could I create a script or "something" like this…?




iRock1
Jun 1, 2013, 07:32 PM
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!



ipsychedelic
Jun 1, 2013, 09:06 PM
I guess you could do it with Applescript.

Something like getting (http://stackoverflow.com/questions/12973026/how-to-get-the-latest-downloaded-file-name-with-applescript-programatically) the latest downloaded file, opening (http://stackoverflow.com/questions/10126661/applescript-how-to-open-a-file-with-the-default-program) with the default application and finally assigning (http://veritrope.com/tech/the-basics-using-keyboard-shortcuts-with-applescripts/) a shortcut.

ideal.dreams
Jun 2, 2013, 12:26 AM
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.

~Shard~
Jun 2, 2013, 01:01 AM
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.

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.

iRock1
Jun 2, 2013, 08:09 PM
I guess you could do it with Applescript.

Something like getting (http://stackoverflow.com/questions/12973026/how-to-get-the-latest-downloaded-file-name-with-applescript-programatically) the latest downloaded file, opening (http://stackoverflow.com/questions/10126661/applescript-how-to-open-a-file-with-the-default-program) with the default application and finally assigning (http://veritrope.com/tech/the-basics-using-keyboard-shortcuts-with-applescripts/) a shortcut.

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.

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

do shell script "open " & dstfile

In other words, how should the entire text in AppleScript look?

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.

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.

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

ideal.dreams
Jun 2, 2013, 10:16 PM
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

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.

ipsychedelic
Jun 2, 2013, 10:43 PM
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.
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)

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.

iRock1
Jun 3, 2013, 06:58 PM
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)

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.

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…

ipsychedelic
Jun 3, 2013, 10:09 PM
BTW I've never understood the difference between sorting files by date added and date created…

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 (http://www.macworld.com/article/1151498/sort_downloads.html) and here (http://macosx.com/forums/mac-os-x-system-mac-software/309451-organize-downloads-folder-date-downloaded.html), or even here (http://hints.macworld.com/article.php?story=20031103145431293).
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 (http://hintsforums.macworld.com/showthread.php?t=84254) 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.