I would like to know how do I open a file with a specific extension Inside the downloads folder. I try this:
set downloads to "~/Downloads"
set fileExtension to ("pkg")
open every file of downloads whose name extension is fileExtension
I know I could use:
do shell script "open ~/Downloads/blabla.pkg"
and this would work fine but with every update the file changes its name and this would not be interesting to me. thanks advance!
While do shell script
can use a string for a path, AppleScript itself has limited file handling abilities, so you need to tell it to use something that does, such as the Finder or System Events. A file specifier (such as alias
, file
, or application scripting terms such as folder
or disk item
) also needs to be used to differentiate a file item from a regular string (such as a POSIX path). Depending on the application used, there may be other differences from the shell, such as tilde expansion, so the StandardAdditions scripting addition provides paths to common locations:
set fileExtension to ("pkg")
tell application "System Events"
repeat with anItem in (get every file of (path to downloads folder) whose name extension is fileExtension)
open anItem
end repeat
end tell
Also note that using the filter reference form “whose” only works with application objects, and not regular lists or records. AppleScriptObjC also has access to Cocoa methods, so you can use the NSWorkSpace or NSFileManager classes, although going that route tends to get a bit more verbose.