I am attempting to create a script or series of scripts to accomplish a semi-complex task. I have already completed the first step, which is to automatically open chrome, visit a site, enter login information and download a .pkg file. What I am looking for is help on making the next part of the script, which is to open the .pkg file from my Downloads folder and install it autonomously. The file name is QuickAdd.pkg, but is renamed as QuickAdd(number).pkg, and hence I need to sort the folder by most recently added. Below is the script that I have so far devised to get the correct file path, but it seems to be very convoluted to me. Any help is greatly appreciated as I am not nearly experienced enough to complete this on my own.
Script v1:
set sourceFolder to (path to downloads folder)
tell application "Finder"
set fileList to (sort (get files of sourceFolder) by creation date)
-- This raises an error if the folder doesn't contain any files
-- Last File is the Most Recently Created --
set theFile to (last item of fileList) as alias
set thePath to POSIX path of theFile
set oProp to (properties of theFile)
set URLstr to URL of oProp
end tell
return URLstr
This works great except that is returns the file path as "file:///Users/wesleycharlap/Downloads/(filename.pkg)", and I cant seem to get any command to recognize the file path as a .pkg and install it. So I assumed the issue was the "file:///" prefix, and thus my second script.
Script v2:
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (path to downloads folder as text)
set sortedList to its filesIn:thePath sortedBy:
(current application's NSURLAddedToDirectoryDateKey)
on filesIn:folderPOSIXPath sortedBy:sortKey
set keysToRequest to {current application's NSURLPathKey, ¬
current application's NSURLIsPackageKey, ¬
current application's NSURLIsDirectoryKey, ¬
sortKey}
set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath
set theNSFileManager to current application's NSFileManager's defaultManager()
set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
includingPropertiesForKeys:keysToRequest ¬
options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
|error|:(missing value))
set valuesNSArray to current application's NSMutableArray's array()
repeat with oneNSURL in listOfNSURLs
(valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value)))
end repeat
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey)
set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}
return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
end filesIn:sortedBy:
This version does indeed give me the correct file path "/Users/wesleycharlap/Downloads/(filename).pkg" but I am confused on where to go from here.
Thx for reading
Script v1 can be replaced by
set sourceFolder to (path to downloads folder)
tell application "Finder"
set allFiles to files of sourceFolder whose name extension is "pkg"
if (count allFiles) = 0 then return
set latestFile to last item of (sort allFiles by creation date)
open latestFile
end tell
It considers only .pkg files and opens the most recent one. The next step to automate the install process of the package is not trivial because Installer.app is not scriptable.