按添加的最新文件对文件夹进行排序的脚本,查找路径名并安装 .pkg

Script to sort folder by latest file added, find path name and install .pkg

我正在尝试创建一个脚本或一系列脚本来完成一个半复杂的任务。我已经完成了第一步,自动打开chrome,访问一个站点,输入登录信息并下载一个.pkg文件。我正在寻找的是帮助制作脚本的下一部分,即从我的下载文件夹中打开 .pkg 文件并自动安装它。文件名为 QuickAdd.pkg,但已重命名为 QuickAdd(number).pkg,因此我需要按最近添加的顺序对文件夹进行排序。以下是我迄今为止为获取正确的文件路径而设计的脚本,但它对我来说似乎非常复杂。非常感谢任何帮助,因为我还没有足够的经验来独自完成这项工作。

脚本 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

除了 returns 文件路径为 "file:///Users/wesleycharlap/Downloads/(filename.pkg)" 之外,这很好用,而且我似乎无法获得任何命令来将文件路径识别为 .pkg 并安装它。所以我假设问题是 "file:///" 前缀,因此是我的第二个脚本。

脚本 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:

此版本确实为我提供了正确的文件路径“/Users/wesleycharlap/Downloads/(filename).pkg”,但我对从这里去哪里感到困惑。

感谢阅读

脚本 v1 可以替换为

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

它只考虑 .pkg 文件并打开最新的文件。下一步自动化软件包的安装过程并不简单,因为 Installer.app 不可编写脚本。