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

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

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

How to run Applescript Photoshop Automate Batch of an Action

I am trying to make an Applescript that will launch Photoshop and call the Automate Batch function to run a specific Action. I have zero experience doing this and have only gotten snippets of code from my searching. I was wondering if someone could help me on this.. in particular if it is at all possible to pass a source folder to the batch call and then how to make the Batch call.
In particular, I am having issues trying to figure out how to:
Pass the source folder into the Batch Options
Call a specific Action in the Batch_Options from my Photoshop
Run the batch call with these options
I've updated with the latest code that is partially there...
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "Save as Photoshop PDF"
activate
try
set Batch_Options to {class:batch options, destination:save and close, error file:Error_File, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
batch "Save" from files folderList from action_set_name with options Batch_Options
end try
end tell
output:
"File Names are: Macintosh HD:Users:Joe:Desktop:Temp:Creature01_CO_v003.psdMacintosh HD:Users:Joe:Desktop:Temp:SecretLaboratory.psd"
Photoshop opens, and then nothing happens...
The following seems to work in the Automator:
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
#Alias list needs to be converted to a string for it to work in Photoshop batch call
set FileNames to {}
repeat with i from 1 to the count of folderList
set current_file to item i of folderList
copy current_file as text to the end of FileNames
end repeat
#Setup error log
set err_log to (sourceFolder as string) & "Error_Log.txt"
if not (exists file err_log) then
make new file at sourceFolder with properties {name:"Error_Log.txt"}
end if
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "SetA"
activate
set Batch_Options to {class:batch options, destination:save and close, error file:err_log, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
#First arg is the Action Name, second is the list of files, third is the Action Set Name
batch "ActionA" from files FileNames from "SetA" with options Batch_Options
end tell

How do I get the Path to the latest created file in a folder with apple script?

I want to get the path to the last file created in folder test
set p to "/Users/palmglow/Documents/googledrive/orders/IFTTT/test"
set a to POSIX file "/Users/palmglow/Documents/googledrive/orders/IFTTT/test/"
set latestFile to last item of (sort (get files of (POSIX file "/Users/palmglow/Documents/googledrive/orders/IFTTT/test")) by creation date) as alias
Try:
set myFolder to "/Users/palmglow/Documents/googledrive/orders/IFTTT/test"
tell application "Finder" to set latestFile to item 1 of (sort files of (POSIX file myFolder as alias) by creation date) as alias

Applescript error when trying to get file list from folder

I'm having trouble getting AppleScript to read a list of files from a folder without receiving the error message "Can't get every file from folder XXX"
set targetfolder to ("DICTAPHONE:DSS_FLDA:")
tell application "Finder"
set fileselection to every file in targetfolder
endtell
This worked flawlessly before upgrading to Mavericks. Paths are correct. I tried it with a different folder on my startup disk and got the same result.
Try using folder targetfolder instead of targetfolder:
set targetfolder to ("Macintosh HD:Library:Desktop Pictures")
tell application "Finder"
files of (folder targetfolder)
end tell
Try this to get the information:
set targetfolder to (path to desktop as alias)
tell application "Finder"
set fileselection to get the name of every file of targetfolder
end tell

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

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

Resources