Would it be possible to select multiple pdfs within a folder instead of an entire folder and limit that to only pdfs? The code is giving me an error Can’t get {} whose name extension = "pdf"." number -1728.
Also, why is it better to use System Events instead of Finder for this?
set source_folder to choose file with prompt "Select folder with pdfs" with multiple selections allowed
tell application "Finder" to set theFiles to (files of source_folder whose name extension is "pdf")
if (count theFiles) is 0 then
display dialog "No pdfs found" buttons "Cancel" default button "Cancel" with icon 0 giving up after 6
error number -128
end if
First of all to select a folder use the choose folder
API. And with the parameter with multiple selections allowed
the result is always a list.
Yes, it’s possible, choose file
has a parameter of type
. It disables files which don’t match the type.
set theFiles to choose file of type "pdf" with prompt "Select PDF files" with multiple selections allowed
3 Likes
Here is something you may be interested in. I wrote this AppleScript code a few years ago and it has been a very useful tool in my arsenal.
In short, it gives you the option to search for single or multiple file extensions within a single folder or recursively through all of the folders of your chosen search folder.
If you choose the “Root Folder” search (single folder), you will then be given the option to reveal and select the resulting files in Finder or to reveal the temporary “Search_Results .txt” file which gets created. However, if you choose the “recursive folder search”, the results will only be logged to the temporary “Search_Results .txt” file which gets created.
set {leftSideOfRegex, rightSideOfRegex} to {"'.*\\.(", ")'"}
set text item delimiters to "|"
activate
set searchCriteria to words of text returned of ¬
(display dialog "Enter file extensions to be searched." & linefeed & ¬
"(extension... followed by a comma and space)" default answer ¬
"png, tif, psd, jpg, psb" buttons {"Cancel", "OK"} default button 2 cancel button 1) as text
activate
set searchFolder to quoted form of POSIX path of ¬
(choose folder with prompt "Select A Folder To Search In")
activate
set searchType to (choose from list {"Root Folder Only Search", "Recursive Folders Search"} ¬
with title "Search Options" with prompt ¬
"Search selected folder only or search all folders within the chosen folder?" OK button name ¬
"OK" cancel button name "Cancel" without empty selection allowed) as text
if searchType = "Root Folder Only Search" then
set shellScript to "find -E " & searchFolder & ¬
" -type f -mindepth 1 -maxdepth 1 -iregex " & ¬
leftSideOfRegex & searchCriteria & rightSideOfRegex & ¬
" |sort -f |tr -s '//' |while read line ;do du -h \"$line\" ;done"
else
set shellScript to "find -E " & searchFolder & ¬
" -type f -mindepth 1 -iregex " & ¬
leftSideOfRegex & searchCriteria & rightSideOfRegex & ¬
" |sort -f |tr -s '//' |while read line ;do du -h \"$line\" ;done"
end if
set text item delimiters to linefeed
set theResults to paragraphs of (do shell script shellScript) as text
set theResults2 to paragraphs of theResults
do shell script "printf " & quoted form of theResults & ¬
" |cut -w -f1- |sed -E 's@^\t@@g' > /private/tmp/Search_Results.txt"
if searchType = "Root Folder Only Search" then
try
activate
set selectFilesInFinder to button returned of ¬
(display dialog "Select and reveal search results in Finder?" buttons ¬
{"Reveal Log Only", "Select Files and Reveal Log"} default button 2)
on error errMsg number errNum
tell application "Finder" to reveal POSIX file "/private/tmp/Search_Results.txt"
return
end try
if selectFilesInFinder = "Select Files and Reveal Log" then
set theFilesAsText to {}
repeat with i from 1 to count of theResults2
try
set theResult to POSIX file (item i of theResults2) as text
set theOffset to offset of "\t" in theResult
set theResult to text (theOffset + 1) thru -1 of theResult
set end of theFilesAsText to theResult as alias
end try
end repeat
frontmostFinder()
tell application "Finder"
reveal POSIX file "/private/tmp/Search_Results.txt"
select theFilesAsText
end tell
else
frontmostFinder()
tell application "Finder" to reveal POSIX file "/private/tmp/Search_Results.txt"
end if
else
frontmostFinder()
tell application "Finder" to reveal POSIX file "/private/tmp/Search_Results.txt"
end if
to frontmostFinder()
tell application "System Events" to tell process "Finder"
set frontmost to true
end tell
end frontmostFinder
1 Like
Yes, it is quite possible. Assuming you have some folder named “PDF files” at Documents folder of home, you can do something like this:
set source_Folder to ((path to documents folder from user domain as text) & "PDF files") as alias
--> alias "Apple HD:Users:123:Documents:PDF files" -- (on my Mac)
-- or:
-- set source_Folder to choose folder with prompt "Select folder with PDFs"
set thePDFs to choose file of type "com.adobe.pdf" with prompt ¬
"Please, select PDFs" default location source_Folder with multiple selections allowed
No need Finder or System Events, because choose file command of Standard Additions is used.
1 Like
When a large number of files are being processed, System Events is faster than Finder (see timing results below). There are other small differences, but they probably don’t apply in your script. Otherwise, the use of one over the other is largely a matter of personal preference. Also, as noted by KniazidisR, there’s no reason to use either System Events or Finder if the choose file command is used.
-- the Following is a folder on an external SSD with 100 TXT files
set theFolder to "Store:Test Folder:"
-- the following took 0.130 second
tell application "System Events"
set theFiles to (files of folder theFolder whose name extension is "txt")
repeat with i from 1 to (count theFiles)
set the name of (item i of theFiles) to "SysEvt File " & i & ".txt"
end repeat
end tell
-- the following took 1.127 second
tell application "Finder"
set theFiles to (files of folder theFolder whose name extension is "txt") as alias list
repeat with i from 1 to (count theFiles)
set the name of (item i of theFiles) to "Finder File " & i & ".txt"
end repeat
end tell
-- test computer is a 2023 Mac mini running Ventura
-- test results are from Script Geek
1 Like
FWIW, a different approach is to select the PDF files in a Finder window. If a selected file is not a PDF, the user is notified and the file is skipped.
tell application "Finder"
set theFiles to selection as alias list
if theFiles = {} then display dialog "No files were selected" buttons {"Cancel"} cancel button 1 default button 1
repeat with aFile in theFiles
if name extension of aFile is "pdf" then
-- do stuff
else
display dialog "The file " & quote & (name of aFile) & quote & " is not a PDF and is being skipped"
end if
end repeat
end tell
1 Like
This is great! Thank you for sharing