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

时间:2016-08-23 16:40:33

标签: arrays sorting applescript posix nsurl

我正在尝试创建一个脚本或一系列脚本来完成一个半复杂的任务。我已经完成了第一步,即自动打开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

这很好用,除了将文件路径返回为“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”但是我很困惑从哪里开始。

阅读本文

1 个答案:

答案 0 :(得分:0)

脚本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不能编写脚本。