1

This is a script that selects a random pdf from a particular folder and reveals it in Finder. But it only selects a pdf from the top level of the folder. How can I get it to look into subfolders as well?

set j to "Macintosh HD:Users:me:Documents:pdfs"

tell application "System Events"
    set all_pdfs to path of every file in folder j whose visible = true and name extension is "pdf"
end tell

set random_pdf to some item of all_pdfs

# uses an existing window or makes a new window with your default settings
tell application "Finder"
    reopen # makes a new window if there are no open windows
    activate
    set target of window 1 to random_pdf
end tell

note: I'm using System Events because it's much faster than Finder.

5
  • System Events doesn’t have anything like that, so you would need to traverse the folder hierarchy or use a shell utility such as find.
    – red_menace
    Commented Sep 1, 2021 at 23:03
  • @red_menace Are you saying that System Events cannot be used to select files inside subfolders? That's false. System Events can deal with "folders of the_folder". However, I don't know how to adapt the "folders of the_folder" construct to my purpose. Source: macscripter.net/viewtopic.php?pid=195609
    – Runty
    Commented Sep 1, 2021 at 23:24
  • No, I was just saying that System Events doesn’t have anything to look into all the subfolders (such as the Finder's entire contents) without you explicitly doing it, as mentioned in my previous comment and your linked topic.
    – red_menace
    Commented Sep 2, 2021 at 2:49
  • Assuming that your drive is indexed, you may find it faster to use mdfind, with the 'do shell' part looking like this: do shell script "mdfind -onlyin " & "~/Documents/" & " -name '.pdf'". Offhand, I think the 'as alias' may be redundant.
    – Mockman
    Commented Sep 3, 2021 at 10:06
  • "note: I'm using System Events because it's much faster than Finder" You're correct to do so, it's everyone else who defaults to using Findar that should be ones leaving a footnote: "note: I am using Finder because everyone else does it."
    – CJK
    Commented Sep 7, 2021 at 18:16

2 Answers 2

0

Here is an alternative method:

Example AppleScript code:

set pdfFilesList to paragraphs of (do shell script "find ~/Documents -type f -iname '*.pdf'")

set pdfFile to POSIX file (some item of pdfFilesList) as alias

tell application "Finder"
    activate
    reveal pdfFile
end tell
8
  • Thank you! This works, and works quickly. I shouldn't have specified System Events in my title. Alternative ways are fine.
    – Runty
    Commented Sep 2, 2021 at 1:09
  • This solution does not filter the whose visible is true property as requested in the OP
    – wch1zpink
    Commented Sep 2, 2021 at 1:22
  • @wch1zpink, RE: "This solution does not filter the whose visible is true property as requested in the OP" -- Even though the code in the OP was doing that, nonetheless, there was no explicit request made in the text of the question that it must filter on visible PFD files. BTW It also didn't contain pdfs in the path portion of the find command. Note that it's was clearly and plainly stated "Example AppleScript code:", meaning it's just that, an example! Commented Sep 2, 2021 at 1:49
  • @wch1zpink, I don't actually know the significance of whose visible is true. I was just copy-pasting code found by googling. In my use case it probably doesn't matter. Sorry about that!
    – Runty
    Commented Sep 2, 2021 at 3:27
  • @Runty, RE: "I don't actually know the significance of whose visible is true" -- It forces the inclusion of only visible files, or excludes hidden files, which in your use case I doubt you have any hidden PDF files and as such wouldn't be necessary and why I did not include it in my example AppleScript code. Commented Sep 2, 2021 at 3:48
0

Something like this may work for you.

property mainFolder : "Macintosh HD:Users:me:Documents:pdfs"

set theFiles to paragraphs of (do shell script "find " & ¬
    quoted form of POSIX path of mainFolder & " -iname '*.pdf'")
set visiblePDFs to {}

tell application "System Events"
    repeat with thisFile in theFiles
        if visible of file thisFile then set end of visiblePDFs to contents of thisFile
    end repeat
end tell

tell application "Finder"
    reveal some item of visiblePDFs as POSIX file
end tell
1
  • Thank you! This also works, although it's pretty slow with 10 thousand pdfs. (I shouldn't have specified System Events -- alternatives are fine.)
    – Runty
    Commented Sep 2, 2021 at 1:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .