How to get the latest downloaded file name with applescript programatically? - applescript

I need to get the file name programatically on Mac, am using Selenium to download the file and from downloads folder i need to pick the same file to install programatically, am using Applescript to do the same. I am stuck in getting the file name in runtime, also my download page url doesnot contain full name of the download file. Pls suggest..

This will give you the latest file (sorted by creation date) of your downloads folder:
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

Yes Mavericks does open the oldest, but this seams to work by specifying the last item.
tell application "Finder"
set latestFile to last item of (sort (get files of (path to downloads folder)) by creation date) as alias
set fileName to latestFile's name
end tell

Related

How do I get the path of the container folder of a file?

I want to pass the path of the containing folder of a file to a command make new file at path. I know how to get the path of the currently open file (with tell application "TexShop" to set thePath to get path of document 1), but I don't know how to get the path of the containing folder.
I tried using container of, like this:
tell application "TeXShop"
set thePath to get container of path of document 1
end tell.
But I get this error:
TeXShop got an error: Can’t make container of path of document 1 into type reference.
Is that because while the container command is allowed in the application Finder it is not for the application TeXShop?
This following AppleScript code will get the path to the front most document in the front most application and the path to its containing folder. This will also create the new file in the containing folder.
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
tell application "Finder" to make new file at containingFolder

Applescript to rename a file in the applications folder

I'm trying to use a script to rename a text file in the applications folder. I'm trying the following
tell application "Finder"
set name of file "File.txt" of applications folder to "File.txt.OFF"
end tell
But this gives an error:
Can’t get file "Text.txt" of applications folder.
Its definitely in there and called that (it's a copy and paste). I then tried removing the folder bit:
set name of file "File.txt" of applications to "File.txt.OFF"
But got another error:
Finder got an error: Can’t set every application to "Text.txt.OFF".
Any help would be much appreciated.
Cheers
applications folder in Finder terminology does not point to the standard folder /Applications. It seems to be a legacy reference to some pre OS X item.
Try this, but you might not have permission to change the name and you are discouraged anyway from putting arbitrary data like text files into /Applications
set applicationsFolder to path to applications folder
tell application "Finder"
set name of file "File.txt" of applicationsFolder to "File.txt.OFF"
end tell
or use System Events
tell application "System Events"
set name of file "File.txt" of applications folder of local domain to "File.txt.OFF"
end tell

AppleScript - Get the name of the only file in a folder?

The following AppleScript code gets the file that was added last,
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
Source: How to get the latest downloaded file name with applescript programatically?
But this fails if the folder has only one file. How to fix this?
The script is supposed to work with one file. But it throws an error if there is no file.
You can ignore the error with a try block
tell application "Finder"
try
set latestFile to item 1 of (sort (get document files of (path to downloads folder)) by creation date) as alias
set fileName to latestFile's name
end try
end tell

Applescript to copy each file/folder into new created folder

I have an Applescript to copy files from one directory to another destination automatically. As I am new in Apple Script and I am facing issue while copying file/folder into newly created folder.
As my script is like this
set dt to (current date) as Unicode text
tell application "Finder"
make new folder at alias "Macintosh HD:Users:XYZ:" with properties {name:dt}
//In above code, new folder create automatically with system date and time on specified location like Tuesday, October 1, 2015 at 12/02/48 PM
copy folder "Macintosh HD:Users:XYZ:Desktop:ABC:" to folder "Macintosh HD:Users:XYZ:"
//In above code, I am facing issue that I am not able to copy file into created newly system date and time folder. How can I copy folder into newly created system date and time folder?
end tell
Can any one please help me?
Thanks in advance.
First of all, the Finder command to copy items is duplicate.
make new folder returns a reference to the new folder. Use this reference as destination.
set dt to (current date) as Unicode text
tell application "Finder"
set newFolder to make new folder at alias "Macintosh HD:Users:XYZ:" with properties {name:dt}
duplicate folder "Macintosh HD:Users:XYZ:Desktop:ABC:" to newFolder
end tell

Accessing folders in AppleScript

Sorry, complete beginner here with AppleScripting.
I'm trying to do a very simple thing, move files from one folder to another.
tell application "Finder"
set this_folder to "Users:chris.nicol:Movies:SmartConverter:"
set this_list to every file of this_folder
repeat with i in this_list
--if i does not start with "x" then
move i to "users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
--end if
end repeat
end tell
However I keep getting an error:
I've tried different commands (count, parentContainer, etc.) on the folder, but I get the same type of error. I've tried different formatting for the folder ...
Users/chris.nicol/Movies/SmartConverter/
Macintosh HD:Users:chris.nicol:Movies:SmartConverter
etc.
Any ideas on what I'm doing wrong?
Thanks in advance,
Chris
Simple tip... if you want to find the proper path you should use try this and look in the results field for the proper path. You'll see that the name of the hard drive, Macintosh HD, is required. Note you could use "choose file" as well if you wanted the path to a file.
(choose folder) as text
Next, the path that you will see is a string. Applescript sees it as a string not a path to a file or folder. As such, when you want to use the string as a path then you must put the word "file" or "folder" in front of it as appropriate to make applescript use it properly. Therefore your script should look like this. Note that the move command can handle a list of files so the repeat loop isn't needed.
set this_folder to "Macintosh HD:Users:chris.nicol:Movies:SmartConverter:"
tell application "Finder"
set this_list to every file of folder this_folder
move this_list to folder "Macintosh HD:Users:chris.nicol:music:itunes:itunes media:automatically add to itunes:"
end tell
Try:
set thisFolder to (path to movies folder as text) & "SmartConverter"
set thatFolder to (path to music folder as text) & "itunes:itunes media:automatically add to itunes"
tell application "Finder" to move files of folder thisFolder to thatFolder
You must have some invisible or otherwise uncopyable files in one of the folders. Either add something like without invisibles to the end of your set this_folder line, or get rid of your loop altogether and simply call
move files of entire contents of this_folder to (the Add Automatically folder)

Resources